Servlet Interview Questions & Answers Part 2

Servlet Interview Tips Questions and Answers

Question: What is the Max amount of information that can be saved in a Session Object ?
There is no such limit on the amount of information that can be saved in a Session Object. The only limit is the Session ID length , which should not exceed more than 4K.

Question: Why should we go for inter servlet communication?
The three major reasons to use inter servlet communication are: a) Direct servlet manipulation - allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object) b) Servlet reuse - allows the servlet to reuse the public methods of another servlet. c) Servlet collaboration - requires to communicate with each other by sharing specific information (through method invocation)

Question: 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 un interpreted 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.

Question: What is a Hidden Comment
Hidden Comments are JSP comments. A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.

Question: What are the differences between a session and a cookie?
Session is stored in server but cookie stored in client. Session should work regardless of the settings on the client browser. There is no limit on the amount of data that can be stored on session. But it is limited in cookie. Session can store objects and cookies can store only strings. Cookies are faster than session.

Question: What is HttpTunneling?
HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intranet is blocked by a firewall and the network is exposed to the outer world only through a specific web server port, that listens for only HTTP requests. To use any other protocol, that by passes the firewall, the protocol is embedded in HTTP and send as HttpRequest.

Question: How to pass information from JSP to included JSP?
By using jsp:param tag.

Question: What is the better way to enable thread-safe servlets and JSPs? SingleThreadModel Synchronization?
The better approach is to use synchronization. Because SingleThreadModel is not scalable. SingleThreadModel is pretty resource intensive from web server's perspective. The most serious issue is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the un serviced requests are queued until something becomes free - which results in poor performance.

Question: What is the difference between ServletContext and PageContext?
ServletContext gives the information about the container and PageContext gives the information about the Request

Question: Why in Servlet 2.4 specification SingleThreadModel has been deprecated?
SingleThreadModel is pretty resource intensive from web server's perspective. When the number of concurrent requests exhaust the servlet instance pool, all the un serviced requests are queued until something becomes free - which results in poor performance.

Question: How can I set a cookie?

Cookie c = new Cookie("name","value");

Question: How will you delete a cookie?

Cookie c = new Cookie ("name", null);

Question: What is the difference between Context init parameter and Servlet init parameter?
Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the tag inside Deployment Descriptor, where as context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag directly inside the web-app tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);

Question: What are the different ways for getting a servlet context?
We will get ServletContext by calling getServletConfig ().getServletContext (). This is because a ServletConfig always hold a reference to ServletContext. By calling this.getServletContext () also we will get a ServletContext object.

Question: What is the difference between an attribute and a parameter?
The return type of attribute is object, where the return type of parameter is String. The method to retrieve attribute is getAttribute () where as for parameter is getParamter (). We have a method setAttribute to set an attribute. But there is no setters available for setting a parameter.

Question: How to make a context thread safe?
Synchronizing the ServletContext is the only solution to make a ServletContext thread safe.

Eg: synchronized (getServletContext ()) {
// do whatever you want with thread safe context.

Question: What is the difference between setting the session time out in deployment descriptor and setting the time out programmatically?
In DD time out is specified in terms of minutes only. But in programmatically it is specified in seconds. A session time out value of zero or less in DD means that the session will never expire. To specify session will never expire programmatically it must be negative value.

Question: How can we set the inactivity period on a per-session basis?
We can set the session time out programmatically by using the method setMaxInactiveInterval() of HttpSession.

Question: How can my application get to know when a HttpSession is removed?
You can define a class which implements HttpSessionBindingListener and override the valueUnbound() method.

Question: How many cookies can one set in the response object of the servlet? Also, are there any restrictions on the size of cookies?
If the client is using Netscape, the browser can receive and store 300 total cookies and 4 kilobytes per cookie. And the no of cookie is restricted to 20 cookies per server or domain

Question: When a session object gets added or removed to the session, which event will get notified ?
HttpSessionBindingListener will get notified When an object is added and/or removed from the session object, or when the session is invalidated, in which case the objects are first removed from the session, whether the session is invalidated manually or automatically (timeout).

Question: What is URL Encoding and URL Decoding ?
URL encoding is the method of replacing all the spaces and other extra characters into their corresponding Hex Characters and Decoding is the process of converting all Hex Characters back to their normal form.

Question: What is the difference between an applet and a servlet?
Servlets run inside a web server and applets run inside web browsers. Applets must have graphical user interfaces whereas servlets have no graphical user interfaces.

Question: What are the different web servers available name few of them?
Tomcat, Java Web Server, JRun

Question: What are the different types of ServletEngines?
The different types of ServletEngines available are:- Standalone ServletEngine: This is a server that includes built-in support for servlets. Add-on ServletEngine: It is a plug-in to an existing server. It adds servlet support to a server that was not originally designed with servlets in mind.

Question: What is a Session Id?
It is a unique id assigned by the server to the user when a user starts a session.

Question: What is use of parseQuestion:ueryString ?
It parses a query string and builds a Hashtable of key-value pairs, where the values are arrays of strings. The query string should have the form of a string packaged by the GET or POST method.

Question: When init() and Distroy() will be called.
init() is called whenever the servlet is loaded for the first time into the web server. Destroy will be called whenever the servlet is removed from the web server.

Question: What is the use of setComment and getComment methods in Cookies ?
setComment: If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment. This is not supported by version zero cookies.
getComment: Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.

Question: Why we are used setMaxAge() and getMaxAge() in Cookies ?
setMaxAge : Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behavior, and will be deleted when the browser closes.
getMaxAge : Returns the maximum specified age of the cookie. If none was specified, a negative value is returned, indicating the default behavior described with setMaxAge.

Question: What is the use of setSecure() and getSecure() in Cookies ?
setSecure method indicates to the web browser that the cookie should only be sent using a secure protocol (https). getSecure method returns the value of the 'secure' flag.

Servlet Interview Questions & Answers

Servlet Interview Questions & Answers Part 3
JSP Interview Questions & Answers

Related Topic Struts FAQs
Servlet Interview Questions & Answers
JSP Interview Questions & Answers
Servlet Interview Questions & Answers Part 3
Latest Hibernate Interview Questions & Answers © 2011   Privacy Policy  Terms of Service  Feedback