Java Servlet
Java Servlet
Servlet class is loaded.
Servlet instance is created.
init method is invoked.
service method is invoked.
destroy method is invoked.
Servlet Interface
Servlet interface needs to be implemented for creating any
servlet (either directly or indirectly).
It provides 3 life cycle methods that are used to initialize
the servlet, to service the requests, and to destroy.
GenericServlet class
GenericServlet class implements Servlet, ServletConfig,
and Serializable interface.
It provides the implementation of all the methods of these
interfaces except the service method.
GenericServlet class can handle any type of request so it is
protocol-independent.
HttpServlet class
The HttpServlet class extends the GenericServlet class and
implements Serializable interface.
It provides http specific methods such as doGet, doPost,
doHead, doTrace etc.
ServletRequest Interface
An object of ServletRequest is used to provide the client
request information to a servlet such as content type,
content length, parameter names and values, header
information's, attributes etc.
Example of ServletRequest
Index.html
Syntax:
RequestDispatcher rd=request.getRequestDispatcher("servlet2");
rd.forward(request, response);//method may be include or forward
Or
rd.include(request, response);
Cont.
Cookies in Servlet
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user
closes the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user
closes the browser. It is removed only if user logout or signout.
Cont.
Advantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.
How to create Cookie?