Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Servlets Interview Questions and Answers For Freshers Part-4

Be First!

servlet31.Can multiple filters be configured?
Yes.

32.Can filtering be done in an ordered way? If so then how to achieve it?
Yes. The order of filter-mapping elements in web.xml determines the order in which the web container applies the filter to the servlet. To reverse the order of the filter, you just need to reverse the filter-mapping elements in the web.xml file.

33.How to configure a central error handling page in servlets?
Use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

34.How to configure a central error handler in servlets?
If you want to have a generic Error Handler for all the exceptions then you should define following error-page instead of defining separate error-page elements for every exception:

<error-page>
<exception-type>java.lang.Throwable</exception-type >
<location>/ErrorHandler</location>
</error-page>

35.What are cookies?
Cookies are text files stored on the client computer and they are kept for various information tracking purpose. Java Servlets transparently supports HTTP cookies.

36.How to create a cookie using servlet?
Setting cookies with servlet involves three steps:

(1) Creating a Cookie object: You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.

Cookie cookie = new Cookie(“key”,”value”);
Keep in mind, neither the name nor the value should contain white space or any of the following characters: [ ] ( ) = , ” / ? @ : ;

(2) Setting the maximum age: You use setMaxAge to specify how long (in seconds) the cookie should be valid. Following would set up a cookie for 24 hours.

cookie.setMaxAge(60*60*24);
(3) Sending the Cookie into the HTTP response headers: You use response.addCookie to add cookies in the HTTP response header as follows:

response.addCookie(cookie);

37.How to read a cookie using servlet?
To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest. Then cycle through the array, and use getName() and getValue() methods to access each cookie and associated value.

38.How to delete a cookie using servlet?
To delete cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps:

Read an already exsiting cookie and store it in Cookie object.

Set cookie age as zero using setMaxAge() method to delete an existing cookie.

Add this cookie back into response header.

39.What is session?
Session provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. The session persists for a specified time period, across more than one connection or page request from the user.

40.What is URL rewriting?
You can append some extra data on the end of each URL that identifies the session, and the server can associate that session identifier with data it has stored about that session.

(149)

Previous
Next

Leave a Reply