1.Explain Servlet life cycle in java.

  • Servlet engine controls the life cycle of servlet
  • Servlet life cycle is described by three life cycle methods under 5 life cycle phases
  • life cycle methods are
  1. init(ServletConfig obj)
  2. service(servletRequest, servletResponse)
  3. destroy() 

  • When ever somethings happens in the life of servlet. servlet engine calls these methods on the servlet instance and hence the name.

Read more here: Servlet Life Cycle


2.What is ServletConfig ?

  • Request parameters are used by the servlet to receive data from client to process the request.
  • Some specific data must be supplied to the servlet at the time of initialization of the servlet and this data is specific to the client.But data is not supplied by the client via request parameters
  • Use initialization parameters to supply the data to the servlet at the time of initialization of the servlet. initialization parameters are set in deployment descriptor   (Web.xml).
  • Servlet can access these parameters during the run time
  • Request parameters change form Request to request 
  • Initialization parameters change from servlet to servlet.

Read more at : ServletConfig

3. What is ServletContext?

  • We can deploy multiple web applications in a servlet container.
  • Each web application contains its own resources in a separate  environment. This environment called ad Web Application context or ServletContext.
  • The resources belongs to the one web application context are not available to the other web application context.
  • A Servlet context contains zero or more number of servlets. For every servlet object the container creates separate ServletConfig object.
Read More at : ServlectContext


4. Can we define a Constructor in servlet?

  • Yes we can define a constructor in servlet but we can not call the constructor explicitly because servlet container will create the object of servlet so it will be called by the servlet container.

5. Can we call destroy() method inside init() method of a servlet. If yes what will happen?

  • Yes we can call destroy() method inside a init() method.
  • Actually container will call destroy() method if we call that method explicitly nothing will happen.
  • If we override destroy() method and called from init(). method will be called and code will be executed.

6.What are the difference between GenericServlet and HttpServlet?

GenericServlet HttpServlet
Abstract Class Abstract Class
Protocol Independent Http Protocol Dependent
Subclass of Servlet Subclass of GenericServlet
public void service(ServletRequest req,ServletResponse res ). Supports public void service() and protected void service(), doGet(),doPost(),doPut(),doDelete(),doHead(),doTrace(),doOptions()etc.


7.What are the differences between doGet() and doPost()?



doGet() doPost()
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException Protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
handles client's GET request handles client's POST request
Request parameters appended to the URL and sent along with header information Request parameters are submitted via form.
Request Parameters are not encrypted Request Parameters are encrypted
Maximum size of data that can be sent using doget() method is 240 bytes There is no maximum size for data.




8. Can we call a servlet from another servlet?

  • Yes. We can call a servlet from another servlet this is known as inter servlet communication.
  • By using RequestDispatcher object we can do this.
  • RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");
  • rd.forward(req, res);
Read more at : RequestDipatcher in java

9. What are the differences between forward() and sendRedirect() methods?

 


forward() sendRedirect()
request.getRequestDispathcer("example.jsp").
forward(request, response);
response.sendRedirect
("http://www.instanceofjava.com");
Used to forward the Request to resources available in within the server Used to redirect the Request to other resources or domains

10.How can you get the information about one servlet context in another servlet?

  • By using setAttribut() method we can set the data in one servlet and get in another
  • Context.setAttribute (“name”,” value”)
  • Context.getAttribute (“name”)

11.Explain Servlet architecture?

  • Frequently asking java interview question in servlets you may get questions like
  • explain the architecture of java servlet?
  • explain the architecture of java servlet with diagram?
  • define the servlet architecture?
  • servlet architecture overview?
  • Servlets read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
  • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
Read More : servlet architecture with diagram

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Java Servlet Interview Questions

  1. Well done sir!!This is excellent platform for all java learners,keep it up.

    ReplyDelete

Select Menu