Always prefer to use GET (As because GET is faster than POST), except mentioned in the following reason:
* If data is sensitive
* Date is greater than 1024 characters
* If your application don't need bookmarks.
17.How do I support both GET and POST from the same Servlet?
The easy way is, just support POST, then have your doGet method call your doPost method:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
18.Should I override the service() method?
We never override the service method, since the HTTP Servlets have already taken care of it . The default service function invokes the doXXX() method corresponding to the method of the HTTP request.For example, if the HTTP request method is GET, doGet() method is called by default. A servlet should override the doXXX() method for the HTTP methods that servlet supports. Because HTTP service method check the request method and calls the appropriate handler method, it is not necessary to override the service method itself. Only override the appropriate doXXX() method.
19.How the typical servlet code look like ?
20.What is a servlet context object?
A servlet context object contains the information about the Web application of which the servlet is a part. It also provides access to the resources common to all the servlets in the application. Each Web application in a container has a single servlet context associated with it.
21.What are the differences between the ServletConfig interface and the ServletContext interface?
|The ServletConfig interface is implemented by the servlet container in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init() method.||A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.|
|There is one ServletConfig parameter per servlet.||There is one ServletContext for the entire webapp and all the servlets in a webapp share it.|
|The param-value pairs for ServletConfig object are specified in the ||The param-value pairs for ServletContext object are specified in the |
22.What's the difference between forward() and sendRedirect() methods?
|A forward is performed internally by the servlet.||A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original.|
|The browser is completely unaware that it has taken place, so its original URL remains intact.||The browser, in this case, is doing the work and knows that it's making a new request.|
|Any browser reload of the resulting page will simple repeat the original request, with the original URL||A browser reloads of the second URL ,will not repeat the original request, but will rather fetch the second URL.|
|Both resources must be part of the same context (Some containers make provisions for cross-context communication but this tends not to be very portable)||This method can be used to redirect users to resources that are not part of the current context, or even in the same domain.|
|Since both resources are part of same context, the original request context is retained||Because this involves a new request, the previous request scope objects, with all of its parameters and attributes are no longer available after a redirect. |
(Variables will need to be passed by via the session object).
|Forward is marginally faster than redirect.||redirect is marginally slower than a forward, since it requires two browser requests, not one.|
23.What is the difference between the include() and forward() methods?
|The ||The |
|If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored.||The forwarded resource may be another servlet, JSP or static HTML document, but the response is issued under the same URL that was originally requested. In other words, it is not the same as a redirection.|
|The ||The |
24.What's the use of the servlet wrapper classes??
The HttpServletRequestWrapper and HttpServletResponseWrapper classes are designed to make it easy for developers to create custom implementations of the servlet request and response types. The classes are constructed with the standard HttpServletRequest and HttpServletResponse instances respectively and their default behaviour is to pass all method calls directly to the underlying objects.
25.What is the directory structure of a WAR file?
A deployment descriptor is an XML document with an .xml extension. It defines a component's deployment settings. It declares transaction attributes and security authorization for an enterprise bean. The information provided by a deployment descriptor is declarative and therefore it can be modified without changing the source code of a bean.
The JavaEE server reads the deployment descriptor at run time and acts upon the component accordingly.
27.What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?
|ServletRequest.getRequestDispatcher(String path)||ServletContext.getRequestDispatcher(String path)|
28.What is preinitialization of a servlet?
A container does not initialize the servlets as soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.
29.What is the
Note: The container loads the servlets in the order specified in the