HTTP is a stateless protocol. So, how to maintain state between requests in J2ee ?
State can be maintained in following ways:
Hidden fields on the page can maintain state and they aren’t visible on the browser. The downside is they may expose sensitive information.
In URL rewriting, sensitive information is appended to the URL. Since it shows up in the URL, it can’t be used for sensitive information.
Cookies are stored as text in name-value pair that the webserver can store on the client and retrieve later. The cookie size is limited and the browser may choose to turn off cookies.
HttpSession is the preferred way to maintain state. A session can identify requests generating from same browser during a conversation time period. The session object can be used by multiple servlets. When a new session is created on the server, the SESSIONID can be passed to the client using cookies or url rewriting.
Explain life cycle of Servlet
A web container is responsible for maintaining servlet life cycle.
The container instantiates a server and calls init() method.
Once init() completes, the servlet is ready to receive requests from clients. For each request, the container calls the service method by creating a new thread.
Before destroying the servlet instance, the container calls the destroy() method in the servlet. At this point the servlet is eligible for garbage collection.
Difference between request forward and redirect(sendRedirect)
Forward action takes place within the server without the browser knowing about it.
SendRedirect sends a header back to the browser containing redirecting information. The browser makes a new request to the resource mentioned in the header information. The overhead is the extra trip, but the advantage is it can access any resource in same domain or a different domain.
What are ServletContext and ServletConfig objects ?
ServletConfig keeps configuration parameters for a particular servlet. The parameters are specified under <servlet> tag in web.xml file
ServletContext object holds paramters for the entire web application. The parameters are specified under the <context-param> tag in the web.xml
How to make a servlet ThreadSafe ?
Typically a web-container creates a single instance of the servlet and creates multiple threads to handle the service method for multiple requests.
There are 2 ways for thread safety:
The servlet can implement the marker interface SingleThreadModel. This will affect performance though.
The other approach is to access the shared resources in a synchronized manner. This is the preferred approach. We can use shared resources in a read only manner or synchronize access to them.
What are filters ?
Fliters dynamically intercept request or response to transform or use it some way.
It’s an implementation of Chain of Responsibility pattern where each filter does some task and forwards to the next filter.
Filters improve reusability.
Common use for filters includes common tasks like logging, authentication, auditing etc.
Filters are configured through the web.xml file using the <filter> and <filter-mapping> tags.
What is a RequestDispatcher ?
RequestDispatcher is obtained from the ServletContext. It forwards the request to a servlet/jsp to get the response.
© 2015 – 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post