Q5. What Is The Life-Cycle of A Servlet?
Q5. What Is The Life-Cycle of A Servlet?
1. Servlet is loaded
2. Servlet is instantiated
3. Servlet is initialized
4. Service the request
5. Servlet is destroyed
Cookies are text data sent by server to the client and it gets saved at the
client local machine.
Servlet API provides cookies support through javax.servlet.http.Cookie class
that implements Serializable and Cloneable interfaces.
HttpServletRequest getCookies() method is provided to get the array of
Cookies from request, since there is no point of adding Cookie to request,
there are no methods to set or add cookie to request.
Similarly HttpServletResponse addCookie(Cookie c) method is provided to
attach cookie in response header, there are no getter methods for cookie.
ServletConfig ServletContext
It represent whole web application running on particular JVM and common for
Servlet config object represent single servlet
servlet
Its like local parameter associated with particular servlet Its like global parameter associated with whole application
It’s a name value pair defined inside the servlet section of web.xml file so it has ServletContext has application wide scope so define outside of servlet tag in we
servlet wide scope file.
getServletConfig() method is used to get the config object getServletContext() method is used to get the context object.
for example shopping cart of a user is a specific to particular user so here we can To get the MIME type of a file or application session related information is stor
use servlet config servlet context object.
Session is a conversational state between client and server and it can consists of
multiple request and response between client and server. Since HTTP and Web
Server both are stateless, the only way to maintain a session is when some unique
information about the session (session id) is passed between server and client in
every request and response.
1. User Authentication
2. HTML Hidden Field
3. Cookies
4. URL Rewriting
5. Session Management API