Question

In: Computer Science

Explain how it is that we build the “processRequest()” method in a Servlet class, when the...

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???

Solutions

Expert Solution

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.


Related Solutions

Explain how it is that we build the “processRequest()” method in a Servlet class, when the...
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?  
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you “ChattBank” Project. This MyServlet will display a message like “Go Braves” in a simple <h1> tag. Run this servlet from a Browser window by typing in the servlet name in the URL line. (ie. http://localhost:8080/ChattBank/MyServlet). Make sure that your Server is up and running before you test this Servlet. The best way to do this is just Run your “ChattBank” Project once before you...
Explain Design Build method
Explain Design Build method
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you...
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you go about running this Servlet? a. From the Browser’s URL(give path):
How do you schedule a Servlet to run, from an HTML file, when the User clicks...
How do you schedule a Servlet to run, from an HTML file, when the User clicks on the Submit button? Show html code here
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Questions related to C#. When you build a class in C#.NET, how many sub classes can...
Questions related to C#. When you build a class in C#.NET, how many sub classes can inherit from the class that you just built? In Object Oriented Development, there are 3 primary concepts that define Object Oriented Programming. What are these concepts? Function/Method Overloading is when you build 2 or 3 functions with the same name but different argument lists. T/F?
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Explain how to build a strip, what are the purposes of a strip strategy? Build a...
Explain how to build a strip, what are the purposes of a strip strategy? Build a real life strip for an American stock of your choice, pull the options contracts and paste them on the answer. Please explain each part of it, what the credit or debit will be for the transaction, include every detail of each option contract you will use to build the strip trade.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT