tag:blogger.com,1999:blog-40687365825372588072024-02-03T03:56:42.538+05:30Mohan's ServletsBookServlet's Interview QuestionsUnknownnoreply@blogger.comBlogger47125tag:blogger.com,1999:blog-4068736582537258807.post-41618484786799058522013-03-16T13:42:00.001+05:302013-03-16T13:42:36.967+05:30Difference between session and context object ?<div class=Section1> <p class=MsoNormal>Session object is created for a specific request and the values in it are accessible within that request whereas Context object is created for web application and the values in it will be accessible for all requests and accessible across the application.<o:p></o:p></p> </div> Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-33536491106726551432013-03-16T12:42:00.001+05:302013-03-16T12:42:49.513+05:30Why request object is created per request not reused ?<div class=Section1> <p class=MsoNormal>Request object is created per request not reused because HTTP is a stateless protocol and it does not expect the data to be remembed. But the servlet object is created once not per reuest. <o:p></o:p></p> <p class=MsoNormal>If different request comes from different browsers it creates different request threads rather than different servlet objects.<o:p></o:p></p> </div> Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-70599362836720883082013-03-16T12:23:00.001+05:302013-03-16T12:23:49.936+05:30Can we write static methods in a servlet?<div class=Section1> <p class=MsoNormal>We do not see any static methods in the servlet as the container has to create the servlet class object.<o:p></o:p></p> </div> Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-38266205956116140692012-12-13T23:59:00.003+05:302012-12-13T23:59:45.240+05:30What is the Servlet?<div dir="ltr" style="text-align: left;" trbidi="on">
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-<span class="Apple-tab-span" style="white-space: pre;"> </span>response programming model.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-85732783667432737032012-12-13T23:59:00.001+05:302012-12-13T23:59:11.790+05:30What are the new features added to Servlet 2.5?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Following are the changes introduced in Servlet 2.5:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>A new dependency on J2SE 5.0<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Support for annotations<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Loading the class<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Several web.xml conveniences<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>A handful of removed restrictions<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Some edge case clarifications<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-82221184124325070312012-12-13T23:57:00.003+05:302012-12-13T23:57:57.259+05:30What are the uses of Servlet?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Typical uses for HTTP Servlets include:<br />
Processing and/or storing data submitted by an HTML form.<br />
Providing dynamic content, e.g. returning the results of a database query to the client.<br />
A Servlet can handle multiple request concurrently and be used to develop high performance system<br />
Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-43413121794733397022012-12-13T23:57:00.001+05:302012-12-13T23:57:19.940+05:30What are the advantages of Servlet over CGI?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Servlets have several advantages over CGI:<br />
A Servlet does not run in a separate process. This removes the overhead of creating a new process for each request.<br />
A Servlet stays in memory between requests. A CGI program (and probably also an extensive runtime system or interpreter) needs to be loaded and started for each CGI request.<br />
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.<br />
Several web.xml conveniences<br />
A handful of removed restrictions<br />
Some edge case clarifications<br />
<div>
<br /></div>
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-44035721176327000252012-12-13T23:55:00.001+05:302012-12-13T23:55:44.201+05:30What are the phases of the servlet life cycle?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
The life cycle of a servlet consists of the following phases:<br />
Servlet class loading : For each servlet defined in the deployment descriptor of the Web application, the servlet container locates and loads a class of the type of the servlet. This can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet.<br />
<br />
Servlet instantiation : After loading, it instantiates one or more object instances of the servlet class to service the client requests.<br />
<br />
Initialization (call the init method) : After instantiation, the container initializes a servlet before it is ready to handle client requests. The container initializes the servlet by invoking its init() method, passing an object implementing the ServletConfig interface. In the init() method, the servlet can read configuration parameters from the deployment descriptor or perform any other one-time activities, so the init() method is invoked once and only once by the servlet container.<br />
<br />
Request handling (call the service method) : After the servlet is initialized, the container may keep it ready for handling client requests. When client requests arrive, they are delegated to the servlet through the service() method, passing the request and response objects as parameters. In the case of HTTP requests, the request and response objects are implementations of HttpServletRequest and HttpServletResponse respectively. In the HttpServlet class, the service() method invokes a different handler method for each type of HTTP request, doGet() method for GET requests, doPost() method for POST requests, and so on.<br />
<br />
Removal from service (call the destroy method) : A servlet container may decide to remove a servlet from service for various reasons, such as to conserve memory resources. To do this, the servlet container calls the destroy() method on the servlet. Once the destroy() method has been called, the servlet may not service any more client requests. Now the servlet instance is eligible for garbage collection<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH8hsDZMb8mH5aiWwEysJ_VmdxKYy_5Z5IzbGRTvNicw_W9xd2eju4ULIKhWi8aRGnPy8Plf9vuokEfGqq7I6ZujsA1cqzaqVXpb-8DVDYNYM-SJD7spuHp9vFcUFz0brZWSarkF8tShI/s1600/servlet-lifecycle.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="267" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH8hsDZMb8mH5aiWwEysJ_VmdxKYy_5Z5IzbGRTvNicw_W9xd2eju4ULIKhWi8aRGnPy8Plf9vuokEfGqq7I6ZujsA1cqzaqVXpb-8DVDYNYM-SJD7spuHp9vFcUFz0brZWSarkF8tShI/s400/servlet-lifecycle.gif" width="400" /></a></div>
The life cycle of a servlet is controlled by the container in which the servlet has been deployed.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-15010274550122840782012-12-13T23:51:00.001+05:302012-12-13T23:51:44.268+05:30Why do we need a constructor in a servlet if we use the init method?<div dir="ltr" style="text-align: left;" trbidi="on">
Even though there is an init method in a servlet which gets called to initialize it, a constructor is still required to instantiate the servlet. Even though you as the developer would never need to explicitly call the servlet's constructor, it is still being used by the container (the container still uses the constructor to create an instance of the servlet). Just like a normal POJO (plain old java object) that might have an init method, it is no use calling the init method if you haven't constructed an object to call it on yet.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-55512961583305160572012-12-13T23:50:00.002+05:302012-12-13T23:50:53.182+05:30How the servlet is loaded?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
A servlet can be loaded when:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>First request is made.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Server starts up (auto-load).<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Administrator manually loads.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-537319564623488922012-12-13T23:49:00.001+05:302012-12-13T23:49:21.961+05:30How a Servlet is unloaded?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
A servlet is unloaded when:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Server shuts down.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Administrator manually unloads.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-4004289012868303482012-12-13T23:47:00.003+05:302012-12-13T23:47:56.705+05:30What is Servlet interface?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
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.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7fER-O7bKLcQANWFFWFV1bM86GpENzi2wXGDbh2xw2j2GgdY1oMyQKL9qn5W861a7ZT3o-dRZku1eGSlVZTRzrCm0_9FjXnr0gYPjw0AcUUgfU46TvpeUTPWWCnn4NhvYLOICBNIbNFQ/s1600/hire.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7fER-O7bKLcQANWFFWFV1bM86GpENzi2wXGDbh2xw2j2GgdY1oMyQKL9qn5W861a7ZT3o-dRZku1eGSlVZTRzrCm0_9FjXnr0gYPjw0AcUUgfU46TvpeUTPWWCnn4NhvYLOICBNIbNFQ/s1600/hire.gif" /></a></div>
<br />
Note: Most Servlets, however, extend one of the standard implementations of that interface, namely javax.servlet.GenericServlet and javax.servlet.http.HttpServlet.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-16516050581043784402012-12-13T23:43:00.001+05:302012-12-13T23:43:12.275+05:30What is the GenericServlet class?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface. In addition to the methods declared in these two interfaces, this class also provides simple versions of the lifecycle methods init and destroy, and implements the log method declared in the ServletContext interface.<br />
Note: This class is known as generic servlet, since it is not specific to any protocol.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-71780863844104449692012-12-13T23:42:00.001+05:302012-12-13T23:42:21.151+05:30What's the difference between GenericServlet and HttpServlet?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
An abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides an framework for handling the HTTP protocol.<br />
<br />
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
The HttpServlet subclass passes generic service method requests to the relevant doGet() or doPost() method.<br />
<br />
GenericServlet is not specific to any protocol.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
HttpServlet only supports HTTP and HTTPS protocol.<br />
<div>
<br /></div>
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-45676133942271711162012-12-13T23:40:00.001+05:302012-12-13T23:40:46.273+05:30Why is HttpServlet declared abstract?<div dir="ltr" style="text-align: left;" trbidi="on">
The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. This is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet() requests for example, there is no need to write a doPost() method too.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-86755992963682184282012-12-13T23:39:00.001+05:302012-12-13T23:39:16.702+05:30Can servlet have a constructor ?<div dir="ltr" style="text-align: left;" trbidi="on">
One can definitely have constructor in servlet.Even you can use the constrctor in servlet for initialization purpose,but this type of approch is not so common. You can perform common operations with the constructor as you normally do.The only thing is that you cannot call that constructor explicitly by the new keyword as we normally do.In the case of servlet, servlet container is responsible for instantiating the servlet, so the constructor is also called by servlet container only.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-6423200512577567292012-12-13T23:37:00.002+05:302012-12-13T23:37:41.940+05:30What are the types of protocols supported by HttpServlet ?<div dir="ltr" style="text-align: left;" trbidi="on">
It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-36460379656926553612012-12-13T23:36:00.002+05:302012-12-13T23:36:31.575+05:30What is the difference between doGet() and doPost()?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
In doGet() the parameters are appended to the URL and sent along with header information.<span class="Apple-tab-span" style="white-space: pre;"> </span>In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar.<br />
<br />
The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.<span class="Apple-tab-span" style="white-space: pre;"> </span>You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects!<br />
<br />
doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent)<span class="Apple-tab-span" style="white-space: pre;"> </span>doPost() provides information (such as placing an order for merchandise) that the server is expected to remember<br />
<br />
Parameters are not encrypted<span class="Apple-tab-span" style="white-space: pre;"> </span>Parameters are encrypted<br />
<br />
doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance<span class="Apple-tab-span" style="white-space: pre;"> </span>doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length<br />
<br />
doGet() should be idempotent. i.e. doget should be able to be repeated safely many times<span class="Apple-tab-span" style="white-space: pre;"> </span>This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable.<br />
<br />
doGet() should be safe without any side effects for which user is held responsible<span class="Apple-tab-span" style="white-space: pre;"> </span>This method does not need to be either safe<br />
<br />
It allows bookmarks.<span class="Apple-tab-span" style="white-space: pre;"> </span>It disallows bookmarks.<br />
<div>
<br /></div>
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-69289906353903443492012-12-13T23:15:00.001+05:302012-12-13T23:17:48.650+05:30What is the "load-on-startup" element?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
The <load-on-startup> element of a deployment descriptor is used to load a servlet file when the server starts instead of waiting for the first request. It is also used to specify the order in which the files are to be loaded. The <load-on-startup> element is written in the deployment descriptor as follows:<br />
<servlet><br />
<servlet-name>ServletName</servlet-name><br />
<servlet-class>ClassName</servlet-class><br />
<load-on-startup>1</load-on-startup><br />
</servlet><br />
Note: The container loads the servlets in the order specified in the <load-on-startup> element.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-73592444174181156552012-12-13T23:14:00.003+05:302012-12-13T23:14:48.764+05:30What is preinitialization of a servlet?<div dir="ltr" style="text-align: left;" trbidi="on">
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.</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-45686505157738064432012-12-13T23:14:00.001+05:302012-12-13T23:14:02.386+05:30What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a “/” it is interpreted as relative to the current context root.<br />
<br />
The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accept relative paths. All path must start with a “/” and are interpreted as relative to current context root.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-17188131430658707992012-12-13T23:12:00.001+05:302012-12-13T23:12:04.831+05:30What is a deployment descriptor?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
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.<br />
The JavaEE server reads the deployment descriptor at run time and acts upon the component accordingly.<br />
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-34063704158008456932012-12-13T23:11:00.000+05:302012-12-13T23:11:07.758+05:30What is the directory structure of a WAR file?<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqqN9pSBGpa2g3v6VlDg7B-9jAy_GkVX2nEU4XBOCBXHtVk3sUQmLIiAZ_cwjYyYwMxgJM-aAfgvVvAH7LlUK5EhAE9M3W7kWzoWzcHQE7fZP3FImL1CuRfW665OxaqrlN_J42PkW4b38/s1600/war.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqqN9pSBGpa2g3v6VlDg7B-9jAy_GkVX2nEU4XBOCBXHtVk3sUQmLIiAZ_cwjYyYwMxgJM-aAfgvVvAH7LlUK5EhAE9M3W7kWzoWzcHQE7fZP3FImL1CuRfW665OxaqrlN_J42PkW4b38/s320/war.gif" width="320" /></a></div>
<br /></div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-79357058893759364822012-12-13T23:09:00.001+05:302012-12-13T23:09:18.938+05:30What is the difference between the include() and forward() methods?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
The RequestDispatcher include() method inserts the the contents of the specified resource directly in the flow of the servlet response, as if it were part of the calling servlet.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
The RequestDispatcher forward() method is used to show a different resource in place of the servlet that was originally called.<br />
<br />
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.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
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.<br />
<br />
The include() method is often used to include common "boilerplate" text or template markup that may be included by many servlets.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
The forward() method is often used where a servlet is taking a controller role; processing some input and deciding the outcome by returning a particular response page.<br />
<div>
<br /></div>
</div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-4068736582537258807.post-24396592242222053212012-12-13T23:08:00.001+05:302012-12-13T23:08:00.468+05:30What's the difference between forward() and sendRedirect() methods?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
A forward is performed internally by the servlet.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original.<br />
<br />
The browser is completely unaware that it has taken place, so its original URL remains intact.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
The browser, in this case, is doing the work and knows that it's making a new request.<br />
<br />
Any browser reload of the resulting page will simple repeat the original request, with the original URL<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
A browser reloads of the second URL ,will not repeat the original request, but will rather fetch the second URL.<br />
<br />
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)<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
This method can be used to redirect users to resources that are not part of the current context, or even in the same domain.<br />
<br />
Since both resources are part of same context, the original request context is retained<span class="Apple-tab-span" style="white-space: pre;"> </span>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.<br />
(Variables will need to be passed by via the session object).<br />
<br />
Forward is marginally faster than redirect.<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
redirect is marginally slower than a forward, since it requires two browser requests, not one.<br />
<div>
<br /></div>
</div>
Unknownnoreply@blogger.com