You are on page 1of 4

Q--What is the servlet?

A--Servlets are modules that extend request/response-oriented servers, such as J


ava-enabled web servers. For example, a servlet may be responsible for taking da
ta in an HTML order-entry form and applying the business logic used to update a
company's order database.
Q--What is the servlet life cycle?
A--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).
Q--What is the difference between ServletContext and ServletConfig?
A--Both are interfaces. The servlet engine implements the ServletConfig interfac
e in order to pass configuration information to a servlet. The server passes an
object that implements the ServletConfig interface to the servlet's init() metho
d.
The ServletContext interface provides information to servlets regarding the envi
ronment in which they are running. It also provides standard way for servlets to
write events to a log file.

Q--What are the uses of Servlets?


A--A servlet can handle multiple requests concurrently, and can synchronize requ
ests. This allows servlets to support systems such as on-line conferencing. Serv
lets can forward requests to other servers and servlets. Thus servlets can be us
ed to balance load among several servers that mirror the same content, and to pa
rtition a single logical service over several servers, according to task.

Q--Can I just abort processing a JSP?


A--Yes. Because your JSP is just a servlet method, you can just put (whereever n
ecessary) a < % return; % >
Q--Can I invoke a JSP error page from a servlet?
A_-Yes, you can invoke the JSP error page and pass the exception object to it fr
om within a servlet. The trick is to create a request dispatcher for the JSP err
or page, and pass the exception object as a javax.servlet.jsp.jspException reque
st attribute. However, note that you can do this from only within controller ser
vlets.
If your servlet opens an OutputStream or PrintWriter, the JSP engine will throw
the following translation error:
java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has al
ready been obtained
The following code snippet demonstrates the invocation of a JSP error page from
within a controller servlet:
protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL, Throwable e) throws
ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Q--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 accross 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 membe
r variables from being modified by different clients.
In this case, you can have your servlet implement the marker interface SingleThr
eadModel. 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 o
ut instances as they are needed. Or it could also serialize client requests, exe
cuting one after another.

Q--What is the difference between the getRequestDispatcher(String path) method o


f javax.servlet.ServletRequest interface and javax.servlet.ServletContext interf
ace?
A--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 be
gins with a "/" it is interpreted as relative to the current context root.
The getRequestDispatcher(String path) method of javax.servlet.ServletContext int
erface cannot accepts relative paths. All path must sart with a "/" and are inte
rpreted as relative to curent context root.

Q--What is the Servlet Interface?


A--The central abstraction in the Servlet API is the Servlet interface. All serv
lets implement this interface, either directly or, more commonly, by extending a
class that implements it such as HttpServlet.
Servlets-->Generic Servlet-->HttpServlet-->MyServlet.
The Servlet interface declares, but does not implement, methods that manage the
servlet and its communications with clients. Servlet writers provide some or all
of these methods when developing a servlet.
Q-- Question: If you want a servlet to take the same action for both GET and POS
T request, what should you do?
A--Simply have doGet call doPost, or vice versa.

Q--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.

Q--What are the advantages using servlets than using CGI?


A--Servlets provide a way to generate dynamic documents that is both easier to w
rite 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 extens
ion.

Q--What is the difference between servlets and applets?


A--Servlets are to servers. Applets are to browsers. Unlike applets, however, se
rvlets have no graphical user interface.

Q--When a servlet accepts a call from a client, it receives two objects. What ar
e they?
A--ServeltRequest: which encapsulates the communication from the client to the s
erver.
ServletResponse: which encapsulates the communication from the servlet back to t
he client.
ServletRequest and ServletResponse are interfaces defined by the javax.servlet p
ackage.

Q--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. P
rovides an output stream, ServletOutputStream and a Writer through which the ser
vlet can send the reply data.
Q--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 tha
t made the request and the server that received it. The input stream, ServletInp
utStream.Servlets use the input stream to get data from clients that use applica
tion protocols such as the HTTP POST and PUT methods

Q--What is the difference between GenericServlet and HttpServlet?


A--GenericServlet is for servlets that might not use HTTP, like for instance FTP
service.As of only Http is implemented completely in HttpServlet. The GenericSe
rvlet has a service() method that gets called when a client request is made. Thi
s means that it gets called by both incoming requests and the HTTP requests are
given to the servlet as they are.

Q--How HTTP Servlet handles client requests?


A--An HTTP Servlet handles client requests through its service method. The servi
ce method supports standard HTTP client requests by dispatching each request to
a method designed to handle that request.
Q--What is a better approach for enabling thread-safe servlets and JSPs? SingleT
hreadModel Interface or Synchronization?
A--Although the SingleThreadModel technique is easy to use, and works well for l
ow volume sites, it does not scale well. If you anticipate your users to increas
e 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 synchronz
ied so
that you take maximum advantage of multithreading. Also, note that SingleThreadM
odel 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 usa
ge is non-deterministic, it may not help much even if you did add more memory an
d increased the size of the instance pool.

Q--What are the differences between GET and POST service methods?
A--A GET request is a request to get a resource from the server. Choosing GET as
the "method" will append all of the data to the URL and it will show up in the
URL bar of your browser. The amount of information you can send back using a GET
is restricted as URLs can only be 1024 characters. A POST request is a request
to post (to send) form data to a resource on the server.
A POST 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. You can send much mor
e information to the server this way - and it's not restricted to textual data e
ither. It is possible to send files and even binary data such as serialized Java
objects!

You might also like