Servlet Interview Questions
Servlet Interview Questions
The servlet is removed from service, destroyed with the destroy() method, then
garbaged collected and finalized.
(OR)
Each servlet has the same life cycle:
A server loads and initializes the servlet (init())
The servlet handles zero or more client requests (service())
The server removes the servlet (destroy()) (some servers do this step only when they shut down).
TOP
Q5:Explain ServletContext.
A: ServletContext interface is a window for a servlet to view it's environment. A
servlet can use this interface to get information such as initialization parameters
for the web application or servlet container's version. Every web application has
one and only one ServletContext and is accessible to all active resource of that
application.
TOP
(OR)
GenericServlet is for servlets that might not use HTTP, like for instance FTP service.As of only
Http is implemented completely in HttpServlet. The GenericServlet has a service() method that
gets called when a client request is made. This means that it gets called by both incoming
requests and the HTTP requests are given to the servlet as they are.
[ Received from Amit Bhoir ] TOP
ServletConfig: The object created after a servlet is instantiated and its default
constructor is read. It is created to pass initialization information to the servlet.
Q10: Difference between single thread and multi thread model serv??
A: Typically, a servlet class is instantiated the first time it is invoked. The same
instance will be used over several client requests, so all members that are declared
in that servlet are shared across clients.
That is what is meant by multi threaded model, multiple clients that access the
same instance. There are situations where you want to protect your servlet member
variables from being modified by different clients.
In this case, you can have your servlet implement the marker interface
SingleThreadModel. Every time a client makes a request to a servlet that
implements this interface, the engine will create a new instance of the servlet.
For performance reasons, the engine can also maintain a instance pool, handing out
instances as they are needed. Or it could also serialize client requests, executing
one after another.
Q11: If you want a servlet to take the same action for both GET and POST request,
what should you do?
A: Simply have doGet call doPost, or vice versa.
Q12: Which code line must be set before any of the lines that use the
PrintWriter?
A: setContentType() method must be set before transmitting the actual document.
Q13: What are the advantages using servlets than using CGI?
A: Servlets provide a way to generate dynamic documents that is both easier to
write and faster to run. It is efficient, convenient, powerful, portable, secure and
inexpensive.
Servlets also address the problem of doing server-side programming with platform-
specific APIs. They are developed with Java Servlet API, a standard Java
extension.
Q14: When a servlet accepts a call from a client, it receives two objects. What are
they?
A: ServeltRequest: which encapsulates the communication from the client to the
server.
ServletResponse: which encapsulates the communication from the servlet back to
the client.
ServletRequest and ServletResponse are interfaces defined by the javax.servlet
package.
Q15: What information that the ServletResponse interface gives the servlet
methods for replying to the client?
A: It Allows the servlet to set the content length and MIME type of the reply.
Provides an output stream, ServletOutputStream and a Writer through which the
servlet can send the reply data.
Q16: What information that the ServletRequest interface allows the servlet
access to?
A: 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. 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.
Q17: What is a better approach for enabling thread-safe servlets and JSPs?
SingleThreadModel Interface or Synchronization?
A: Although the SingleThreadModel technique is easy to use, and works well for low
volume sites, it does not scale well. If you anticipate your users to increase in the
future, you may be better off implementing explicit synchronization for your
shared data.
The key however, is to effectively minimize the amount of code that is
synchronized so that you take maximum advantage of multithreading. Also, note
that SingleThreadModel is pretty resource intensive from the server's
perspective.
The most serious issue however is when the number of concurrent requests exhaust
the servlet instance pool. In that case, all the unserviced requests are queued until
something becomes free - which results in poor performance. Since the usage is
non-deterministic, it may not help much even if you did add more memory and
increased the size of the instance pool.
At the end of the session, we can inactivate the session by using the following
command
session.invalidate();