In: Computer Science
Explain how it is that we build the “processRequest()” method in a Servlet class, when the Servlet interface requires us to write 3 methods(init(), service() and destroy()). Why don’t we have to build these 3 methods???
Ans:-ProcessRequest technique, is whatever other strategy that you can use into your code which isn't overridden to anything. It is called from the above strategies to not complicate the code in them accordingly the solicitations are taken care of in it. so you can utilize ProcessRequest to deal with your solicitation if and just if it's called from one of the strategies above.Example:
protected void doGet(HttpServletRequest, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request,response);
} and
protected void doPost(HttpServletRequest, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request,response);
}
public void init(ServletConfig config) :It initializes the servlet. It is the existence cycle strategy for servlet and conjured by the web holder just a single time.
public void service(ServletRequest request,ServletResponse response) :It provides reaction for the approaching solicitation. It is conjured at each solicitation by the web holder.
public void destroy() :It is conjured just a single time and shows that servlet is being pulverized.
Example for above:
import javs.io.*;
import javax.servlet.*;
public class First implements Servlet{
ServletConfig config=null;
public void init(ServletConfig config)
{
this.config=config;
system.out.println("servlet initialized");
}
public void service(ServletRequest reqs,ServletResponse resp)
throws IOException,ServletException
{
resp.setContentType("text");
Printwriter out=resp.getWriter();
out.print("<html><body>");
out..print("hi smpl servlet");
out.print("</body></html>");
}
public void destroy(){system.out.println("servlet destroyed");}
public ServletConfig getServletConfig(){return config;}
}
Invoke the Servlet init() method:
When all the servlet examples are made, the servlet compartment summons the init() strategy for each launched servlet. There is some arrangement of boundaries to init that is utilized to introduce the servlet. You can indicate them in the sending descriptor file(web.xml).
For instance, there is the component load-on-startup in web.xml for Servlet. This property you can use to characterize the circumstance when the servlet will be stacked by the servlet compartment. Without this component, the servlet will be stacked when the main solicitation shows up.
Invoke the Servlets service() Method :
In the wake of stacking and introduction of servlets, a web worker is prepared to get a solicitation. At the point when a web worker gets a solicitation for a specific Servlet, the administration() strategy for that Servlet is summoned. In the event that your Servlet is the subclass of the class GenericServlet, at that point the solicitation is served by the administration() technique itself. On the off chance that the Servlet is a subclass of the class HttpServlet, at that point administration() technique gets the solicitation and chooses the right controller strategy dependent on the sort of solicitation.
For instance, if your Servlet got a Get Request the administration() strategy would dispatch the solicitation to the doGet() technique with demand boundaries.
Each sort of solicitation (Post, Head, Put, and so on) is dispatched by administration() strategy for the servlet to the comparing overseers doPost(), doHead(), doPut(), and so on .This servlet life cycle step can be executed zero or on various occasions.
Call the Servlets destroy() Method :
What's more, inevitably, a servlet can be emptied by the servlet compartment. In this circumstance, its wreck() strategy is called. This progression of a servlet life cycle can be executed just one time. A servlet can be pulverized by the compartment when the holder closes down. One more motivation to obliterate a servlet can be that the compartment reloads the entire web application at runtime.