[go: up one dir, main page]

0% found this document useful (0 votes)
89 views2 pages

Q5. What Is The Life-Cycle of A Servlet?

The document discusses the lifecycle of a servlet, how cookies work in servlets, and the differences between ServletContext and ServletConfig. It describes the 5 stages in a servlet's lifecycle: 1) loading, 2) instantiation, 3) initialization, 4) servicing requests, and 5) destruction. It explains how cookies are implemented using the Cookie class and how they are sent and retrieved via requests and responses. Finally, it outlines the key differences between ServletContext and ServletConfig, noting that ServletContext represents the entire application while ServletConfig is specific to a servlet.

Uploaded by

sachin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views2 pages

Q5. What Is The Life-Cycle of A Servlet?

The document discusses the lifecycle of a servlet, how cookies work in servlets, and the differences between ServletContext and ServletConfig. It describes the 5 stages in a servlet's lifecycle: 1) loading, 2) instantiation, 3) initialization, 4) servicing requests, and 5) destruction. It explains how cookies are implemented using the Cookie class and how they are sent and retrieved via requests and responses. Finally, it outlines the key differences between ServletContext and ServletConfig, noting that ServletContext represents the entire application while ServletConfig is specific to a servlet.

Uploaded by

sachin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q5. What is the life-cycle of a servlet?

There are 5 stages in the lifecycle of a servlet:

1. Servlet is loaded
2. Servlet is instantiated
3. Servlet is initialized
4. Service the request
5. Servlet is destroyed

Q6. How does cookies work in Servlets?

 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.

Q7. What are the differences between ServletContext vs


ServletConfig?

The difference between ServletContext and ServletConfig in Servlets JSP is in below


tabular format.

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.

Q8. What are the different methods of session management in


servlets?

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.

Some of the common ways of session management in servlets are:

1. User Authentication
2. HTML Hidden Field
3. Cookies
4. URL Rewriting
5. Session Management API

You might also like