Basic Java Servlet Interview Questions: Advantages of Servlets Over CGI Are
Basic Java Servlet Interview Questions: Advantages of Servlets Over CGI Are
com
http://www.allapplabs.com/struts/struts.htm
http://www.geocities.com/srcsinc/EJBQUESTIONS.html
http://interview-questions-and-answers.blogspot.com/2007/10/java-servlet-
interview-questions-part1.html
• Servlets are multithreaded which can support multiple users’ requests. i.e., servlet is a
resource and where as requests are treated as threads. So multiple requests (threads) can
access a single instance of servlet resource asynchronously. Whereas CGI provide a new
instance for every request, it is a single thread model .By creating new instance every
request server won’t support multiple requests.
• Servlets are used to generate dynamic content.
• Servlets are platform independent.
• Servlets are more efficient compare to other server side programming.
2. What is meant by a servlet?
Servlet is a server side component written in java, which can host request of client,
process the request and response sent back to the client.
Servlet is a java program that is run inside a servlet engine called web server. It take
requests from client and responds to those requests.
3. What are the types of servlets? What is the difference between 2 types of Servlets?
There are two types of servlets provided by servlet API those are
1) GenericServlet- It can use for any type of protocols (http, ftp, telnet)
It is protocol independent ,it support service() method only
2) HttpServlet- It can use for only http protocol, it support doGet() and doPost() methods
with Service() methods
4. What is the type of method for sending request from HTTP server?
Http server request can sent in two types one is GET and second one is POST
When we make a request to any servlet, request doesn’t goes directly to the servlet
instead it will go to the service method of the super class and then it is passed as a
parameters to the service method of your class. In doGet() method the data will append to
the url as a query string and we can’t send huge amount of data using doGET() method.
Where as in doPost() method the data can be directly sent to browser without displaying
it in the url. With this we can send as much of data as we can. But doGet() method is little
faster when compared to doPost().but it is not recommended as for some sensitive
messages like password will be displayed as a query string in it.
doGet()
doPost()
doHead()
doPut()
doOptions()
doDelete()
doTrace().
20. What are the types of SessionTracking? Why do you use Session Tracking in
HttpServlet?
1. Using Cookies
2. Using URL rewriting
3. Using HTTP session
4. Using Hidden form fields.
Servlet
|
Generic Servlet
|
HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests
|
MyServlet
28. What is the difference between Difference between doGet() and doPost()?
GET 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).
30. Which code line must be set before any of the lines that use the PrintWriter?
setContentType() method must be set.
At the end of the session, we can inactivate the session by using the following command
session.invalidate();
33. How Can You invoke other web resources (or other servlet / jsp ) ?
Servlet can invoke other Web resources in two ways: indirect and direct.
Indirect Way: Servlet will return the resultant HTML to the browser which will point to another
Servlet (Web resource)
Direct Way: We can call another Web resource (Servlet / Jsp) from Servlet program itself, by
using RequestDispatcher object.
You can get this object using getRequestDispatcher("URL") method. You can get this object
from either a request or a Context.
Example :
RequestDispatcher dispatcher = request.getRequestDispatcher("/jspsample.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
}
Included WebComponent (Servlet / Jsp) cannot set headers or call any method (for example,
setCookie) that affects the headers of the response.
38. What is the difference between using getSession(true) and getSession(false) methods?
getSession(true) - This method will check whether already a session is existing for the user. If a
session is existing, it will return that session object, Otherwise it will create new session object
and return taht object.
getSession(false) - This method will check existence of session. If session exists, then it returns
the reference of that session object, if not, this methods will return null.
A: The javax.servlet.Servlet interface defines the three methods known as life-cycle method.
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException,
IOException
public void destroy()
First the servlet is constructed, then initialized wih the init() method.
Any request from client are handled initially by the service() method before delegating to the
doXxx() methods in the case of HttpServlet.
The servlet is removed from service, destroyed with the destroy() methid, then garbaged
collected and finalized.
A: Cookies
SSL sessions
URL- rewriting
Q: 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 applicationor
servlet container's version. Every web application has one and only one ServletContext and is
accessible to all active resource of that application.
A: A container doesnot initialize the servlets ass 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 <load-on-startup> 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.
A: A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this
limitation. A request string for doGet() looks like the following:
http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN
doPost() method call doesn't need a long text tail after a servlet name in a request. All parameters
are stored in a request itself, not in a request string, and it's impossible to guess the data
transmitted to a servlet only looking at a request string.
Q: What is the difference between HttpServlet and GenericServlet?
A: ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet
container, for example, to get the MIME type of a file, dispatch requests, or write to a log
file.The ServletContext object is contained within the ServletConfig object, which the Web
server provides the servlet when the servlet is initialized
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.
Answer: a) Translation phase ? conversion of JSP to a Servlet source, and then Compilation of
servlet source into a class file. The translation phase is typically carried out by the JSP engine
itself, when it receives an incoming request for the JSP page for the first time
b) init(), service() and destroy() method as usual as Servlets.
Question: How many cookies can one set in the response object of the servlet? Also, are there
any restrictions on the size of cookies?
Answer: If the client is using Netscape, the browser can receive and store 300 total cookies
4 kilobytes per cookie (including name)
20 cookies per server or domain
Question: Is there some sort of event that happens when a session object gets bound or unbound
to the session?
Answer: 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).
Question: What do the differing levels of bean storage (page, session, app) mean?
Answer: page life time - NO storage. This is the same as declaring the variable in a scriptlet and
using it from there.
session life time - request.getSession(true).putValue "myKey", myObj);
application level ? getServletConfig().getServletContext().setAttribute("myKey ",myObj )
request level - The storage exists for the lifetime of the request, which may be forwarded
between jsp's and servlets
Question: Is it true that servlet containers service each request by creating a new thread? If that is
true, how does a container handle a sudden dramatic surge in incoming requests without
significant performance degradation?
Answer: 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.