How do you communicate between the servlets?
Servlets can communicate using the request, session and context scope objects by setAttribute() and getAttribute() method. A servlet can placed the information in one of the above scope object and the other can find it easily if it has the reefrence to that object.Reply
When init() and Distroy() will be called.
Both init() and destroy() method called only once within its lifecycle.
The init() method will be called after instantaition of servlet, i.e. after the constructor calling. The destroy() method will be called when servlet is removed from the server, i.e. when server shuts down.Reply
What is life cycle of servlet?
A web container manages the life cycle of the servlet :
* Loading and Inatantiation: container load the servlet class at the first request to that servlet. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. Instantiation is done by calling the default constructor.
* Initialization: calling the init() method.
* Servicing the Request: calling the service() method and pass the HttpServletRequest and HttpServletResponse object as the parameters.
* Destroying the Servlet: calling the destroy() method.Reply
What is the difference between setting the session time out in deployment descriptor and setting the time out programmatically?
Setting the time out programmatically means calling setMaxInactiveInterval(int seconds) from your servlet.
Setting the session time out in deployment descriptor means declaratively setting the session timeout. The following setting in the deployment descriptor causes the session timeout to be set to 10 minutes:
Setting the session time out in deployment descriptor allows you to modify the time easily, hard code it into the program requires recompilation overhead.Reply
How to make a context thread safe?
To make the context scope thread safe, you need a lock on context object. To synchronize the context attribute is to synchronize on context object. If a thread gets a lock on context object, then you are guaranteed that only one thread at a time can be getting or setting the context attribute. It only works if all of the other code that manipulate the context attribuet also synchronizes on the servlet context.Reply
What is the difference between an attribute and a parameter?
Request parameters are the name/value pairs, and the result of submitting an HTML form. The name and the values are always strings. For example, when you do a post from html, data can be automatically retrieved by using request.getParameter() at the server side. Parameters are Strings, and generally can be retrieved, but not set.
Attributes are objects, and can be placed in the request, session, or context objects. Because they can be any object, not just a String, they are much more flexible. You can also set attributes programatically using setAttribute() method, and retrieve them later using getAttribute() method.Reply
What are the different ways for getting a servlet context?
The different ways for getting a servlet context :
* GenericServlet implements ServletConfig, so HttpServlets all have a getServletContext() method
* In a Filter you have access to the FilterConfig which is set in the init(FilterConfig fc) callback. You can use FilterConfig.getServletContext().
* In a ServletContextListener or a ServletContextAttributeListener, the event passed to the listener methods has a getServletContext() method. Reply
What is the difference between Context init parameter and Servlet init parameter?
Context init parameter is accessible through out the web application and declared outside the <servlet>...</servlet> element tag in the deployment descriptor(DD) . Servlet init parameters are declared inside the <servlet>...</servlet> element and only accessible only to the that specific servlet. You can access the context init parameters using the getServletContext() method and Servlet init parameters using the getServletConfig() method.
Declaring the Servlet init parameter in DD :
Declaring the Context init parameter in DD :
Why in Servlet 2.4 specification SingleThreadModel has been deprecated?
Tere is no practical implementation to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.Reply
What is a output comment?
A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as uninterpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page
source from your Web browser.
<!-- comment [ <%= expression %> ] -->
<!-- This is a commnet sent to client on
<%= (new java.util.Date()).toLocaleString() %>