What is pre initialization of a servlet?
Servlets are loaded and initialized at the first request come to the server and stay loaded until server shuts down. However there is another way you can initialized your servlet before any request come for that servlet by saying <load-on-startup>1</load-on-startup> in the deployment descriptor. You can specify <load-on-startup>1</load-on-startup> in between the <servlet></servlet> tag.Reply
Explain the directory structure of a web application?
The directory structure of a web application consists of two parts.
A private directory called WEB-INF
A public resource directory which contains public resource folder.
WEB-INF folder consists of
1. web.xml file that consist of deployment information.
2. classes directory cosists of business logic.
3. lib directory consists of jar files.Reply
How do servlets handle multiple simultaneous requests?
Servlets are under the control of web container. When a request comes for the servlet, the web container find out the correct servelt based on the URL, and create a separeate thread for each request. In this way each request is processed by the different thread simultaneously. Reply
What is Servlet chaining?
Servlet Chaining is a phenomenon wherein response Object (Output) from first Servlet is sent as request Object (input) to next servlet and so on. The response from the last Servlet is sent back to the client browser.
In Servlets, there are two ways to achieve servlet chaining using javax.servlet.RequestDispatcher:
RequestDispatcher rd = req.getRequestDispatcher("Servlet2");
2. Forward, where req is HttpServletRequest and resp is HttpServletResponse:
RequestDispatcher rd = req.getRequestDispatcher("Servlet3");
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