<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Servlet Interview Questions</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/servlet-interview-questions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>OVERVIEW OF SERVLETS</title>
		<link>http://www.bestjavainterviewquestions.com/overview-of-servlets/</link>
		<comments>http://www.bestjavainterviewquestions.com/overview-of-servlets/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 16:23:43 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[server side programming]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[servlets]]></category>
		<category><![CDATA[servlets overview]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=590</guid>
		<description><![CDATA[OVERVIEW OF SERVLETS Servlets provide a Java based solution used to address the problems currently associated with doing server side programming including inextensible scripting solution, platform specific API’s, and incomplete interfaces. Servlets are objects that confirm to a specific interface that can be plugged into a Java based server. Servlets are to the server side [...]]]></description>
			<content:encoded><![CDATA[<p><strong>OVERVIEW OF SERVLETS</strong></p>
<p>Servlets provide a Java based solution used to address the problems currently associated with doing server side programming including inextensible scripting solution, platform specific API’s, and incomplete interfaces.</p>
<p>Servlets are objects that confirm to a specific interface that can be plugged into a Java based server. Servlets are to the server side what applets are to the client side object byte code that can be dynamically loaded off the net.<br />
They differ from applets. In that they are face less objects (with out graphics or GUI component).They serve as platform independent, dynamically loadable, pluggable  helper byte code objects on the server side that can be used to dynamically extend server side functionality.</p>
<p>For example, an HTTP servlet can be used to generate dynamic HTML content. When you use servlets to do dynamic content you get the following advantages:</p>
<p>They are faster and cleaner than CGI scripts<br />
They use a  standard API (the servlet API)<br />
They provide all the advantages of Java(run on variety of    servers with out needing to be rewritten)</p>
<p><strong>The Attractiveness of Servlets</strong></p>
<p>There are many features of servlets that make them easy and attractive to use. These include:<br />
Easily configured using the GUI based admin Tool.<br />
Can be loaded and invoked from a local disc or remotely across the network.<br />
Can be linked together, or chained so that on a servlet can call another servlet, or several servlets in sequence.<br />
Can be called dynamically from within HTML pages, using server side include tags.<br />
Are secure- even when downloading across the net work, the servlet security model and servlet sandbox protect your system from unfriendly behavior.</p>
<p><strong>The Advantages of the servlet API</strong></p>
<p>One of the great advantages of the servlet API is protocol independence. It assumes nothing about:<br />
The protocol being used to transmit on the net<br />
How it is loaded<br />
The server environment it will be running in tjese qualities are important, because it allows the servlet API to be embedded in many different kindsof servers.<br />
There are other advantages to the servlet API  as well.<br />
These include:<br />
It’s extensible-you can inherit all your functionality from the classes made available to you;<br />
It’s simple, small, and easy to use.</p>
<p><strong>JAVA SERVLET FEATURES:</strong></p>
<p>The JavaServer provides several important servlet features.<br />
These includes:<br />
Loading and Invoking Servlets</p>
<p>Servlets can be loaded both locally and remotely.</p>
<p>Filters and Servlet Chaining</p>
<p>The JavaServer uses mime types to call servlets sequentially.<br />
<em><br />
Server-Side Includes</em></p>
<p>Server can be invoked from dynamic HTML docements using    server-side include tags.</p>
<p><strong>Replacing CGI Scripts</strong></p>
<p>Servlets are an effective substitute for CGI scripts, and provide a faster and cleaner way to genarate dynamic documents.</p>
<p><strong>INTERNAL SERVLETS:</strong></p>
<p>The Java TM Web Server TM servlet architecture is very flexible and the server takes advantage of thie by dividing  its work among several internal servlets. These are:</p>
<p><strong> File Servlet:</strong></p>
<p>The File Servlet provides the standard document serving capabilities of Java Web Server. This servlet includes a caching mechanism to speed up response times for frequently accessed files.</p>
<p><strong> Invoker Servlet:</strong></p>
<p>The purpose of this servlet is to invoke other servlets which are explicitly requested by name, that is</p>
<p><em>http://&lt;server-host-name&gt;/servlets/servlet name.</em></p>
<p><strong> Server Side Include Servlet:</strong></p>
<p>Servlets can be embedded within html documents using the servlet tag. When the server detects the servlet, and sends output of the servlet to the client at the point where the servlet tag was embedded.</p>
<p><strong> Admin Servlet:</strong></p>
<p>The Admin servlet facilitates administration of the Java web server through a GUI front end Administration Tool.</p>
<p><strong> CGI Servlet:</strong></p>
<p>This servlet acts as a gateway for CGI 1.1 interface. This servlet allows any program that utilizes the CGI 1.1     standard to under operate Java Web Server.</p>
<p><strong> Image Map Servlet:</strong></p>
<p>This servlet implements server-side image maps utilizing an extension of standard NCSA map files.</p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question">Basic Java Interview Question</a> (1)</li><li>April 7, 2009 -- <a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions-3/" title="J2EE Interview Questions">J2EE Interview Questions</a> (0)</li><li>March 29, 2009 -- <a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions/" title="J2EE Interview Questions">J2EE Interview Questions</a> (0)</li><li>March 25, 2009 -- <a href="http://www.bestjavainterviewquestions.com/creating-a-stateful-session-bean/" title="Creating a Stateful session Bean">Creating a Stateful session Bean</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/overview-of-servlets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread safety of the servlet</title>
		<link>http://www.bestjavainterviewquestions.com/thread-safety-of-the-servlet/</link>
		<comments>http://www.bestjavainterviewquestions.com/thread-safety-of-the-servlet/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 06:05:45 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>
		<category><![CDATA[threadsafety of servlet]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=454</guid>
		<description><![CDATA[Q) How to ensure the thread safety of the servlet? Rep) 1) It is better to avoid instance variables &#38; static variables in a servlet class 2) If the instance variable reference to a threadsage object then there will be no problem 3) If all Instance variables refers to an object that is not thread [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q) How to ensure the thread safety of the servlet?</strong></p>
<p><strong>Rep) </strong></p>
<p>1) It is better to avoid instance variables &amp; static variables in a servlet class</p>
<p>2) If the instance variable reference to a threadsage object then there will be no problem</p>
<p>3) If all Instance variables refers to an object that is not thread safe then we most provide synchronize block as shown below.</p>
<p>public class Sone extends HttpServlet</p>
<p>ArrayList a1=new ArrayList();</p>
<p>public void service(&#8211;)throws &#8212; {</p>
<p>&#8212;-</p>
<p>synchronized(a1) {</p>
<p><strong> &#8211;&gt; In this case the out variable is an instance variable</strong></p>
<p>al.add(&#8212;);</p>
<p>}</p>
<p>}   //Synchronized block must be written by a share code.</p>
<p>}</p>
<p>4) We must provide synchronized block while accessing thread unsafe objects that are stored as Attributes of Sessionobject and application object.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/explain-stream-tokenizer/" title="Explain Stream Tokenizer">Explain Stream Tokenizer</a> (0)</li><li>October 3, 2008 -- <a href="http://www.bestjavainterviewquestions.com/explain-jsp-directives/" title="Explain Jsp Directives">Explain Jsp Directives</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-the-webxml-file-can-do/" title="What the web.xml file can do">What the web.xml file can do</a> (1)</li><li>September 28, 2009 -- <a href="http://www.bestjavainterviewquestions.com/how-to-delete-the-textbox-using-the-javascript/" title="How To Delete the textbox using the Javascript">How To Delete the textbox using the Javascript</a> (1)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-httpsession-class/" title="Define HttpSession Class">Define HttpSession Class</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/thread-safety-of-the-servlet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Can we provide a constructor in the servlet class</title>
		<link>http://www.bestjavainterviewquestions.com/can-we-provide-a-constructor-in-the-servlet-class/</link>
		<comments>http://www.bestjavainterviewquestions.com/can-we-provide-a-constructor-in-the-servlet-class/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 05:11:19 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=444</guid>
		<description><![CDATA[Rep) As the Web Container uses newInstance() method for creating the servlet object,If requiered we can provide a zero arguent constructor Random PostsDecember 16, 2008 -- Define Array in Java (1)November 6, 2008 -- Load Factor (0)October 4, 2008 -- What is JTAPI (0)June 23, 2008 -- JAVA UTIL PACKAGE (0)July 7, 2008 -- JSP [...]]]></description>
			<content:encoded><![CDATA[<p>Rep) As the Web Container uses newInstance() method for creating the servlet object,If requiered we can provide a zero arguent constructor</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session-hijacking-3/" title="Define Session Hijacking">Define Session Hijacking</a> (0)</li><li>December 12, 2008 -- <a href="http://www.bestjavainterviewquestions.com/url-connection-class/" title="URL Connection Class">URL Connection Class</a> (0)</li><li>September 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/session-tracking-example-using-servlets/" title="Session Tracking Example using Servlets">Session Tracking Example using Servlets</a> (0)</li><li>October 27, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-finalizer-methods/" title="What is Finalizer Methods">What is Finalizer Methods</a> (0)</li><li>June 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/java-jdbc-questions/" title="Java-Jdbc Questions">Java-Jdbc Questions</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/can-we-provide-a-constructor-in-the-servlet-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How the Web Container creates a servlet object</title>
		<link>http://www.bestjavainterviewquestions.com/how-the-web-container-creates-a-servlet-object/</link>
		<comments>http://www.bestjavainterviewquestions.com/how-the-web-container-creates-a-servlet-object/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 05:09:36 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>
		<category><![CDATA[java webcontainer]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=442</guid>
		<description><![CDATA[Rep) Web Container reads the name of the servlet class from web.xml and uses Class.forName() method to load the servlet class and it calls the newInstance() method to create the servlet Object. Random PostsJuly 15, 2008 -- Springs Interview Questions (0)September 2, 2008 -- Difference between session.update() and session.lock() (0)June 23, 2008 -- JAVA UTIL [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Rep) </strong>Web Container reads the name of the servlet class from web.xml and uses Class.forName() method to load the servlet class and it calls the newInstance() method to create the servlet Object.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>July 21, 2010 -- <a href="http://www.bestjavainterviewquestions.com/explain-briefly-about-serialization/" title="Explain Briefly about Serialization">Explain Briefly about Serialization</a> (0)</li><li>June 25, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jdbc-interview-questions-2/" title="Jdbc Interview Questions">Jdbc Interview Questions</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/why-does-ejb-needs-two-interfaces/" title="Why does EJB needs two interfaces">Why does EJB needs two interfaces</a> (0)</li><li>April 4, 2009 -- <a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions-2/" title="J2EE Interview Questions">J2EE Interview Questions</a> (0)</li><li>June 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/awt-interview-questions-3/" title="AWT Interview Questions">AWT Interview Questions</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/how-the-web-container-creates-a-servlet-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Servlet Important Questions</title>
		<link>http://www.bestjavainterviewquestions.com/servlet-important-questions/</link>
		<comments>http://www.bestjavainterviewquestions.com/servlet-important-questions/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 17:21:25 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>
		<category><![CDATA[servlet important and repeated questions]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=43</guid>
		<description><![CDATA[Q)  What is the servlet? A) Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company&#8217;s order database. Servlets are to servers what applets are to browsers. Unlike [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q)  What is the servlet?<br />
</strong><strong>A) </strong>Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company&#8217;s order database.<br />
Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.</p>
<p><strong>Q)  Whats the advantages using servlets than using CGI?<br />
</strong><strong>A)</strong> Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension.</p>
<p><strong>Q) What are the uses of Servlets?<br />
A)</strong> A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing.<br />
Servlets can forward requests to other servers and servlets.Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.</p>
<p><strong>Q) Which pakage provides interfaces and classes for writing servlets?<br />
A)</strong> javax<br />
<strong><br />
Q) Whats the Servlet Interfcae?<br />
A)</strong> The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.<br />
Servlets&#8211;&gt;Generic Servlet&#8211;&gt;HttpServlet&#8211;&gt;MyServlet.<br />
The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some</p>
<p><strong>Q) What information that the ServletRequest interface allows the servlet access to?<br />
A) </strong>Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it.<br />
The input stream, ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods.<br />
<strong><br />
Q) What information that the ServletResponse interface gives the servlet methods for replying to the client?<br />
A)</strong> It Allows the servlet to set the content length and MIME type of the reply.<br />
Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data.<br />
<strong><br />
Q) What is Servlet chaining?<br />
A) </strong>response object from one servlet is passed as request to another Servlet. Try this example and you will come to know what servlet chaining is all about.<br />
public class ServletA extends HttpServlet<br />
{<br />
public void init(ServletConfig config) throws ServletException {}<br />
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException<br />
{<br />
String aValue = request.getParameter(&#8220;valueA&#8221;);<br />
System.out.println(&#8220;ServletA read: &#8220;+aValue);<br />
request.setAttribute(&#8220;ReadTheValue&#8221;,&#8221;Yes&#8221;);<br />
//do other servlet stuff&#8230;just don&#8217;t open a write and output to the page&#8230;.<br />
request.getRequestDispatcher(&#8220;/servletB&#8221;).forward(req,resp);<br />
}<br />
}<br />
public class ServletB extends HttpServlet<br />
{<br />
public void init(ServletConfig config) throws ServletException {}<br />
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException<br />
{<br />
String aValue = request.getParameter(&#8220;valueA&#8221;);<br />
System.out.println(&#8220;ServletB also got the value: &#8220;+aValue);<br />
System.out.println(&#8220;ServletB also found attribute: &#8220;+request.getAttribute(&#8220;ReadTheValue&#8221;));<br />
//finish the task and write to the page.<br />
}<br />
}</p>
<p>Advantages:<br />
1) Chaining reduces the demand on a single servlet<br />
2) Modular design<br />
3) Enables different complex processes to be maintained by more than one person.<br />
4) Allows steps in a process to be modified (eg servlet 1 -&gt; servlet 2 or servlet 1 -&gt; servlet 3 -&gt; servlet 2, etc)<br />
5) Removes the complexity in a single servlet</p>
<p><strong>Q) Suppose that Myservlet implements SingleThreadModel and there are Local  variables, Instance variables, Class variables,Request attributes,Session attributes and Context attributes. Which among  these variables would be thread safe?<br />
A)</strong> Local,Instance and request variables are thread safe.<br />
I have the following deployed in my web.xml file.<br />
&lt;session-config&gt;<br />
&lt;session-timeout&gt;30&lt;/session-timeout&gt;<br />
&lt;/session-config&gt;<br />
<strong><br />
Q) then I perform setMaxInactiveInterval(2400) for my session object.The First one denotes 30 minutes and second one denoted 40 minutes. After how long would the session expire?<br />
A)</strong> 40 minutes. The timeout defined in your DD is used until you redefine it with<br />
setMaxInactiveInterval</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>October 4, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-jtapi/" title="What is JTAPI">What is JTAPI</a> (0)</li><li>September 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/example-of-hibernate/" title="Example of Hibernate">Example of Hibernate</a> (0)</li><li>September 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/java-package/" title="Java Package">Java Package</a> (0)</li><li>June 25, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jdbc-interview-questions-2/" title="Jdbc Interview Questions">Jdbc Interview Questions</a> (0)</li><li>April 4, 2009 -- <a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions-2/" title="J2EE Interview Questions">J2EE Interview Questions</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/servlet-important-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Servlet Interview Questions</title>
		<link>http://www.bestjavainterviewquestions.com/servlet-interview-questions/</link>
		<comments>http://www.bestjavainterviewquestions.com/servlet-interview-questions/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 15:31:49 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Interview Questions]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=35</guid>
		<description><![CDATA[SERVLETS INTERVIEW QUESTIONS AND ANSWERS Q) What is a Servlet? A) A Servlet is a server side java program which processes client requests and generates dynamic web content. Q) Explain the architechture of a Servlet? A) javax.servlet.Servlet interface is the core abstraction which has to be implemented by all servlets either directly or indirectly. Servlet [...]]]></description>
			<content:encoded><![CDATA[<p><strong>SERVLETS INTERVIEW QUESTIONS AND ANSWERS </strong></p>
<p><strong>Q) What is a Servlet?<br />
A)</strong> A Servlet is a server side java program which processes client requests and generates dynamic web content.</p>
<p><strong>Q) Explain the architechture of a Servlet?<br />
A)</strong> javax.servlet.Servlet interface is the core abstraction which has to be implemented by all servlets either directly or indirectly. Servlet run on a server side JVM ie the servlet container.Most servlets implement the interface by extending either javax.servlet.GenericServlet or javax.servlet.http.HTTPServlet.A single servlet object serves multiple requests using multithreading.</p>
<p><strong>Q) What is the difference between an Applet and a Servlet?<br />
</strong><strong>A) </strong>An Applet is a client side java program that runs within a Web browser on the client machine whereas a servlet is a server side component which runs on the web server. An applet can use the user interface classes like AWT or Swing while the servlet does not have a user interface. Servlet waits for client&#8217;s HTTP requests from a browser and generates a response that is displayed in the browser.</p>
<p><strong>Q) What is the difference between GenericServlet and HttpServlet?<br />
A)</strong> GenericServlet is a generalised and protocol independent servlet which defined in javax.servlet package. Servlets extending GenericServlet should override service() method. javax.servlet.http.HTTPServlet extends GenericServlet. HTTPServlet is http protocol specific ie it services only those requests thats coming through http.A subclass of HttpServlet must override at least one method of doGet(), doPost(),doPut(), doDelete(), init(), destroy() or getServletInfo().</p>
<p><strong>Q) Explain life cycle of a Servlet?<br />
A)</strong> On client&#8217;s initial request, Servlet Engine loads the servlet and invokes the init() methods to initialize the servlet. The servlet object then handles subsequent client requests by invoking the service() method. The server removes the servlet by calling destry() method.</p>
<p><strong>Q) What is the difference between doGet() and doPost()?<br />
A) </strong>doGET Method : Using get method we can able to pass 2K data from HTML All data we are passing to Server will be displayed in URL (request string).<br />
doPOST Method : In this method we does not have any size limitation. All data passed to server will be hidden, User cannot able to see this info on the browser.</p>
<p><strong>Q) What is the difference between ServletConfig and ServletContext?<br />
A)</strong> ServletConfig is a servlet configuration object used by a servlet container to pass information to a servlet during initialization. All of its initialization parameters can only be set in deployment descriptor. The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized. ServletContext is an interface which defines a set of methods which the servlet uses to interact with its servlet container. ServletContext is common to all servlets within the same web application. Hence, servlets use ServletContext to share context information.</p>
<p><strong>Q) What is the difference between using getSession(true) and getSession(false) methods?<br />
A)</strong> getSession(true) method will check whether already a session is existing for the user. If a session is existing, it will return the same session object, Otherwise it will create a new session object and return taht object.</p>
<p>getSession(false) method will check for the existence of a session. If a session exists, then it will return the reference of that session object, if not, it will return null.</p>
<p><strong>Q) What is meant by a Web Application?<br />
A) </strong>A Web Application is a collection of servlets and content installed under a specific subset of the server&#8217;s URL namespace such as /catalog and possibly installed via a .war file.</p>
<p><strong>Q) What is a Server Side Include ?<br />
A)</strong> Server Side Include is a Web page with an embedded servlet tag. When the Web page is accessed by a browser, the web server pre-processes the Web page by replacing the servlet tag in the web page with the hyper text generated by that servlet.</p>
<p><strong>Q) What is Servlet Chaining?<br />
A)</strong> Servlet Chaining is a method where the output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.</p>
<p><strong>Q) How do you find out what client machine is making a request to your servlet?<br />
A)</strong> The ServletRequest class has functions for finding out the IP address or host name of the client machine. getRemoteAddr() gets the IP address of the client machine and getRemoteHost()gets the host name of the client machine.</p>
<p><strong>Q) What is the structure of the HTTP response?<br />
A)</strong> The response can have 3 parts:<br />
1) Status Code &#8211; describes the status of the response. For example, it could indicate that the request was successful, or that the request failed because the resource was not available. If your servlet does not return a status code, the success status code, HttpServletResponse.SC_OK, is returned by default.<br />
2) HTTP Headers &#8211; contains more information about the response. For example, the header could specify the method used to compress the response body.<br />
3) Body &#8211; contents of the response. The body could contain HTML code, an image, etc.</p>
<p><strong>Q) What is a cookie?<br />
A)</strong> A cookie is a bit of information that the Web server sends to the browser which then saves the cookie to a file. The browser sends the cookie back to the same server in every request that it makes. Cookies are often used to keep track of sessions.<br />
<strong><br />
Q) Which code line must be set before any of the lines that use the PrintWriter?<br />
A)</strong> setContentType() method must be set.</p>
<p><strong>Q) Which protocol will be used by browser and servlet to communicate ?<br />
A) </strong>HTTP<br />
<strong><br />
Q) Can we use the constructor, instead of init(), to initialize servlet?<br />
A)</strong> Yes, of course you can use the constructor instead of init(). There&#8217;s nothing to stop you. But you shouldn&#8217;t. The original reason for init() was that ancient versions of Java couldn&#8217;t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won&#8217;t have access to a ServletConfig or ServletContext.</p>
<p><strong>Q) How can a servlet refresh automatically if some new data has entered the database?<br />
A)</strong> You can use a client-side Refresh or Server Push<br />
<strong><br />
Q) What is the Max amount of information that can be saved in a Session Object?<br />
A) </strong>As such there is no limit on the amount of information that can be saved in a Session Object. Only the RAM available on the server machine is the limitation. The only limit is the Session ID length(Identifier) , which should not exceed more than 4K. If the data to be store is very huge, then it&#8217;s preferred to save it to a temporary file onto hard disk, rather than saving it in session. Internally if the amount of data being saved in Session exceeds the predefined limit, most of the servers write it to a temporary cache on hard disk.<br />
<strong><br />
Q) What is HTTP Tunneling?<br />
A)</strong> HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intra-network of an organization 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 sent as HttpRequest. The masking of other protocol requests as http requests is HTTP Tunneling.<br />
<strong><br />
Q) What&#8217;s the difference between sendRedirect( ) and forward( ) methods?<br />
A)</strong> A sendRedirect method creates a new request (it&#8217;s also reflected in browser&#8217;s URL ) where as forward method forwards the same request to the new target(hence the change is NOT reflected in browser&#8217;s URL). The previous request scope objects are no longer available after a redirect because it results in a new request, but it&#8217;s available in forward. sendRedirect is slower compared to forward.</p>
<p>Q) Is there some sort of event that happens when a session object gets bound or unbound to the session?<br />
A) HttpSessionBindingListener will hear the events When an object is added and/or remove 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).<br />
<strong><br />
Q) Is it true that servlet containers service each request by creating a new thread?<br />
A)</strong> If that is true, how does a container handle a sudden dramatic surge in incoming requests without significant performance degradation?<br />
The implementation depends on the Servlet engine. For each request generally, a new Thread is created. But to give performance boost, most containers, create and maintain a thread pool at the server startup time. To service a request, they simply borrow a thread from the pool and when they are done, return it to the pool. For this thread pool, upper bound and lower bound is maintained. Upper bound prevents the resource exhaustion problem associated with unlimited thread allocation. The lower bound can instruct the pool not to keep too many idle threads, freeing them if needed.</p>
<p><strong>Q) What is URL Encoding and URL Decoding?<br />
A)</strong> URL encoding is the method of replacing all the spaces and other extra characters into their corresponding Hex Characters and Decoding is the reverse process converting all Hex Characters back their normal form.<br />
For Example consider this URL,<br />
/ServletsDirectory/Hello&#8217;servlet/<br />
When Encoded using URLEncoder.encode(&#8220;/ServletsDirectory/Hello&#8217;servlet/&#8221;) the output is<br />
http%3A%2F%2Fwww.javacommerce.com%2FServlets+Directory%2FHello%27servlet%2F<br />
This can be decoded back using<br />
URLDecoder.decode(&#8220;http%3A%2F%2Fwww.javacommerce.com%2FServlets+Directory%2FHello%27servlet%2F&#8221;)</p>
<p><strong>Q) Do objects stored in a HTTP Session need to be serializable? Or can it store any object?<br />
A)</strong> Yes, the objects need to be serializable, but only if your servlet container supports persistent sessions. Most lightweight servlet engines (like Tomcat) do not support this. However, many EJB-enabled servlet engines do. Even if your engine does support persistent sessions, it is usually possible to disable this feature.</p>
<p><strong>Q) What is the difference between session and cookie?<br />
A)</strong> The difference between session and a cookie is two-fold.<br />
1) session should work regardless of the settings on the client browser. even if users decide to forbid the cookie (through browser settings) session still works. There is no way to disable sessions from the client browser.<br />
2) session and cookies differ in type and amount of information they are capable of storing. Javax.servlet.http.Cookie class has a setValue() method that accepts Strings. Javax.servlet.http.HttpSession has a setAttribute() method which takes a String to denote the name and java.lang.Object which means that HttpSession is capable of storing any java object. Cookie can only store String objects.</p>
<p><strong>Q) How to determine the client browser version?<br />
A) </strong>Another name for &#8220;browser&#8221; is &#8220;user agent.&#8221; You can read the User-Agent header using request.getUserAgent() or request.getHeader(&#8220;User-Agent&#8221;)</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>July 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsf-important-interview-questions/" title="JSF Important Interview Questions">JSF Important Interview Questions</a> (2)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-are-command-line-arguments-in-java/" title="What are Command Line Arguments in Java">What are Command Line Arguments in Java</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-u-mean-by-web-application/" title="What u mean by Web Application">What u mean by Web Application</a> (1)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/can-we-provide-a-constructor-in-the-servlet-class/" title="Can we provide a constructor in the servlet class">Can we provide a constructor in the servlet class</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-log-file/" title="What is LOG FILE">What is LOG FILE</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/servlet-interview-questions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

