Servlet in Java

Servlet in Java : Servlet is a server side technology used to work internally on client request and provide the support to the dynamic pages to show response on screen.

What are the Common tasks performed by servlet container?
Create and maintaining life cycle of an object.
Provide JSP support over client request.
Multithreading support.
Take care of memory leakages, securities and the garbage collection.

What are the difference between Web Server and Application Server?

Servlet Lifecycle :

Servlet instantiation:
Servlet instantiation will take place :
1) At the time of first request if servlet is not loaded already
2) If servlet is already loaded then at the time of server startup


Initialization [ init() ] :
Typically initialization is performed only once.
In init() method we generally initialize data member, open database connection, open files, etc.
Public void Init() {
// Initialization code
}


Request Processing [ Service() ] :
To process the client request service() method gets invoked.
Service() method will get called for every new request.
For every request new thread will get spawn.
Public void Service(HttpServletRequest req, HttpServletResponse res)
{
// Service handling code
}


Servlet Cleanup [ Destroy() ] :
This is the last phase of Servlet life cycle where servlet is unloaded from the memory.
Usually we perform the cleanup action in destroy() method like closing database connection, closing open files, etc.
Public void Destroy() {
// Cleanup code
}

Servlet in java

What are the differences between Servlet Config and Servlet Context?

What is request Dispatcher?
Request dispatcher is an interface which is used to forward the request to the JSP, HTML or another servlet in the same application [forward() ] or to include the contents of JSP, HTML, Servlet in the response [ include() ].
It has two methods:
1) Void forward(Servletrequest req , ServletResponse res)
2) Void include(Servletrequest req , ServletResponse res)

What are the differences between doGet() and doPost() method?

What are the Generic servlet and Http servlet?

Servlet in java

What is session and what are different ways of session management in servlet?
Session is a conversional state between client and the server.
It can be consist of multiple request and multiple response between client and server.
Since HTTP and web server are stateless, only way to maintain session is by session management.
Ways of session management in servlet :
 URL re-writing
 Cookies
 User authentication
 Hidden form field

Servlet in java

How does Cookies work in servlet?
Cookies are nothing but the textual data sent by server to the client and that data gets stored at client machine.
HttpServlet getCookies() method is provided to get array of cookies from request.

Types of Cookies:
1) Non-persistent cookies:
It is valid for single session only.
It gets removed each time when user closes the browser.


2) Persistent cookies:
It is valid for multiple sessions.
It does not get removed even though user closes the browser, but when you logged out or sign out then cookies gets removed.


Advantages of Cookies:

Simplest way of maintaining state.
Cookies are maintained at client side.


Disadvantages of Cookies:
Only textual information will get stored.
It will not work if cookie is disable in browser.


Method to get cookie name:
Public String getName()


Method to set cookie name:
Public String setName()


Method to get array of cookies:
Public Cookie[] getCookies()


Method to add cookie:
Public void addCookie(Cookie ck)

What is Servlet filters?
Filters is an object that invokes at Pre-processing and Post-processing of the request.
It is pluggable (we can enable/disable it from web.xml configuration file).
doFilter(ServletRequest, ServletResponse, FilterChain) method invokes every time when user request to any resource, to which the filter is mapped and it is used to do filtering tasks.
Uses of Filtering:
Recording all incoming requests.
Logs the IP address of system from which the request triggered
Encryption and Decryption
Input validation

Defining filter in web.xml :

Thanks for visiting Ajitation.com, Ajitation is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.