Servlets Interview Questions and Answers For Freshers Part-3
// Set error code and reason.
response.sendError(407, “Need authentication!!!” );
22.How to redirect a request from a servlet to another servlet?
Page redirection is generally used when a document moves to a new location and we need to send the client to this new location or may be because of load balancing, or for simple randomization. The simplest way of redirecting a request to another page is using method sendRedirect() of response object.
23.How sendRedirect method works?
This method generates a 302 response along with a Location header giving the URL of the new document.
24.How sendError method works?
This method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client.
25.What are servlets filters?
Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
To intercept requests from a client before they access a resource at back end.
To manipulate responses from server before they are sent back to the client.
26.Name some of the servlets filters?
There are various types of filters suggested by the specifications:
Data compression Filters.
Filters that trigger resource access events.
Image Conversion Filters.
Logging and Auditing Filters.
MIME-TYPE Chain Filters.
Tokenizing Filters .
XSL/T Filters That Transform XML Content.
27.How to do servlet filter mapping?
Filters are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in your application’s deployment descriptor.
28.For what purpose init() method of a filter is used?
This method is called by the web container to indicate to a filter that it is being placed into service.
29.For what purpose doFilter() method of a filter is used?
This method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
30.For what purpose destroy() method of a filter is used?
This method is called by the web container to indicate to a filter that it is being taken out of service.