Java Servlet sample questions and answers have been provided here for the reference of the students. These questions are different from the JSP sample questions and J2EE sample questions.
- Explain the life cycle methods of a Servlet.
The javax.servlet.Servlet interface defines the three methods known as life-cycle method.
Method 1: public void init(ServletConfig config) throws ServletExceptionpublic
Method 2: void service( ServletRequest req, ServletResponse res) throws ServletException, IOExceptionpublic
Method 3: void destroy()
First the servlet is constructed, then initialized with 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() method, then garbage collected and finalized.
- What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?
Answer: 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.The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths.All path must sart with a "/" and are interpreted as relative to curent context root.
- Explain the directory structure of a web application.
Answer: The directory structure of a web application consists of two parts. A private directory called WEB-INFA public resource directory which contains public resource folder. WEB-INF folder consists of 1.web.xml2.classes directory3.lib directory.
- What are the common mechanisms used for session tracking?
Answer: CookiesSSL sessionsURL- rewriting
- Explain ServletContext.
Answer: ServletContext interface is a window for a servlet to view its environment. A servlet can use this interface to get information such as initialization parameters for the web applicationor servlet containers version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.
- What is the difference between Difference between doGet() and doPost()?
Answer: 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=vNdoPost() 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.
- What is the difference between HttpServlet and GenericServlet?
Answer: A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).Both these classes are abstract.
The questions and answers provided above are meant for the help of the students. Reader can also refer to Java web development sample questions for further reference.