You are on page 1of 2

Servlets Notes

- It is a server side technology from Sun


- It is also called as Server Side Applet which do not have any appearance
- The browser sends a request to the server
o A server is a software that resides on a machine to handle the requests
o For Servlets there are many servers
 JWS (Java Web Server)
 JSDK (Java Servlet Development Toolkit)
 Tomcat
 J2EE Server
 Web Logic
 Web Sphere
Etc.
Working of Servlet
A client sends the request to for the Servlet (a class file)
The request is picked up by the server, and loads the class file into its container called
Servlet Container and converts into native code for first time only.
The server generates the HTML code using the native code and sends the response to the
client (browser)
When server is shutdown all native code get removed from the Servlet container.

Requirements
- Java provides javax.servlet and javax.servlet.http APIs from Servlets
- These APIs are provided in servlet.jar file
- A program to be called as Servlet must be inherited from GenericServlet or HttpServlet
class
- GenericServlet provide three main methods
o init()
public void init()
o service()
public void service(ServletRequest req, ServletResponse res) throws
ServletException, IOException
o destroy()
public void destroy()
- HttpServlet provides some specific methods for operations
public void init()
public void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
public void destroy()
Receiving Data into servlets
- Use getParameter() method of HttpServletRequest object to read the data
- If data is coming from Get methods (QueryString) use doGet() or service()
- If data is coming from Post method use doPost() of service() method
- If you want that data should be accepted irrespective of method, override one method and
call it into another method

Difference between Get and Post


1. To send data with Post you must have a form, for Get we can send data directly using
querystring
2. The size of data send using get method depends of Address bar URL which can be 1024
character or 2048 character, so data send using Get method cannot be beyond 1000 or
2000 character depending on browser. In post method data is not dependent on address
bar URL. Unlimited data can be send using Post method
3. In post method data is not visible while in Get data is visible in querystring. Hence for
secured information Post is used.
The speed of Get is faster than Post

You might also like