answersLogoWhite

0

The doGet() method is the method inside a servlet that gets called every time a request from a jsp page is submitted. The control first reaches the doGet() method of the servlet and then the servlet decides what functionality to invoke based on the submit request. The get method called when the type of page submission is "GET"

There is another way of submitting requests from a jsp page is "POST" and when that happens it calls the doPost() method inside the servlet.

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Basic Math

What is doPost in servlet?

doPost() is the method in the servlet that is used to service the Http requests submitted using the Post option. HTML forms can be submitted in two ways using the Get method and the Post method. You can specify the method while declaring the form itself. So you will have two methods in the servlet doGet() and doPost() each for one of them


Which method must to override in Servlet?

There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet


What is difference between GET and POST in servlets in java?

The difference between a GET and a POST is the way data is transferred to a servlet. With a GET, the URL will show each name/value pair on the query string in the URL. For example, if you had a form with a field named 'foo,' and when submitted had a value of 'bar,' the URL might be something like this: http://www.example.com/servlet?foo=bar With a POST, this information is not visible in the URL. Instead, it is transferred in the HTTP headers. As far as the actual servlet is concerned, there is not a great deal of difference when it comes to getting the parameters. Whether you use a GET or a POST, you still use request.getParameter("foo"); to get the value. The method used in the Servlet for processing either a GET or a POST is different too. If you use a GET, the method that is called is doGet(HttpServletRequest, HttpServletResponse). The doGet method is also called if there is no GET or POST data. If you use a POST, the method called is doPost(HttpServletRequest, HttpServletResponse).


How do JSP methods get translated to Servlet methods?

A JSP gets converted into a Servlet for execution and hence the methods in a JSP are similar to the ones in a Servlet.Scriptlets and Expressions will end up in the body of doGet or doPostDeclarations will end up as instance variablesjspInit() and jspDestroy() will get translated to Servlet's init() and destroy() methods


How to retrieve data from jsp to servlet?

To retrieve data from a JSP to a servlet, you can use form elements in your JSP to collect user input and submit it to the servlet. When creating the form, ensure to set the action attribute to the servlet's URL and the method attribute to either GET or POST. In the servlet, you can access the submitted data using the request.getParameter("parameterName") method, where "parameterName" corresponds to the name attribute of the form input elements. This allows you to process the data as needed in your servlet.

Related Questions

Why donot you have doget in jsp also?

Because the doGet is the work of the Servlet and not the JSP


What is doPost in servlet?

doPost() is the method in the servlet that is used to service the Http requests submitted using the Post option. HTML forms can be submitted in two ways using the Get method and the Post method. You can specify the method while declaring the form itself. So you will have two methods in the servlet doGet() and doPost() each for one of them


Which method must to override in Servlet?

There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet


What use import javaxservlethttpHttpServletRequest in java?

The Http RequestWhen a user hits a URL with a servlet at the other end, the Servlet Container creates an HttpServletRequest object. It passes this object as an argument to the servlet's service methods (doPut(), doGet(), and doPost()). There is a lot of information in this object, including the login details of the user making this request and the name of the HTTP method with which this request was made.


What is difference between GET and POST in servlets in java?

The difference between a GET and a POST is the way data is transferred to a servlet. With a GET, the URL will show each name/value pair on the query string in the URL. For example, if you had a form with a field named 'foo,' and when submitted had a value of 'bar,' the URL might be something like this: http://www.example.com/servlet?foo=bar With a POST, this information is not visible in the URL. Instead, it is transferred in the HTTP headers. As far as the actual servlet is concerned, there is not a great deal of difference when it comes to getting the parameters. Whether you use a GET or a POST, you still use request.getParameter("foo"); to get the value. The method used in the Servlet for processing either a GET or a POST is different too. If you use a GET, the method that is called is doGet(HttpServletRequest, HttpServletResponse). The doGet method is also called if there is no GET or POST data. If you use a POST, the method called is doPost(HttpServletRequest, HttpServletResponse).


What is HttpServletRequest?

ServletRequest provides the functionalities to the classes to access the HTTP requests. The servlet container creates an HttpServletRequest object and an HttpServletResponse object, and passes them as arguments to the servlet's service methods such as doGet, doPost, etc.


What is signature of service method in servlet?

The doXXX MethodsThere are 3 main types of requests that get processed by a Servlet. They are:&bull; Get&bull; Post&bull; PutEach of them have a corresponding doXXX() method in the Servlet class which would be:&bull; doGet&bull; doPost&bull; doPutThese methods are called by the service method in your Servlet.Let us now take a look at a sample Servlet that has these 3 doXXX methods.import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/*** A servlet that has the 3 doXXX Methods** @author Anand*/public class OurSecondServlet extends HttpServlet{// doGet()public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("GET", response);}// doPost()public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("POST", response);}// doPut()public void doPut(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("PUT", response);}public void requestType(String requestType,HttpServletResponse response)throws IOException, ServletException{response.setContentType("text/HTML");PrintWriter out = response.getWriter();out.println("< HTML >");out.println("< head >");out.println("< title >Our Second Servlet" +"< / title >");out.println("< / head >");out.println("< body >");out.println("< h1 >Servlet Request< / h1 >");out.println("The Submitted Request type is : " + requestType);out.println("< / body >");out.println("< / HTML >");}}


How do JSP methods get translated to Servlet methods?

A JSP gets converted into a Servlet for execution and hence the methods in a JSP are similar to the ones in a Servlet.Scriptlets and Expressions will end up in the body of doGet or doPostDeclarations will end up as instance variablesjspInit() and jspDestroy() will get translated to Servlet's init() and destroy() methods


How does Apache httpd work?

Tomcat is a servlet container, and the following is how a servlet container works The init, service, and destroy methods are the servlet's lifecycle methods. The init method is called once by the servlet container after the servlet class has been instantiated to indicate to the servlet that it being placed into service. The init method must complete successfully before the servlet can receive any requests. A servlet programmer can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, this method is normally left blank. The service method is then called by the servlet container to allow the servlet to respond to a request. The servlet container passes a javax.servlet.ServletRequest object and a javax.servlet.ServletResponse object. The ServletRequest object contains the client's HTTP request information and the ServletResponse encapsulates the servlet's response. These two objects enable you to write custom code that determines how the servlet services the client request. The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or when the servlet container needs some free memory. This method is called only after all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls destroy, it will not call the service method again on this servlet. The destroy method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, and threads) and make sure that any persistent state is synchronized with the servlet's current state in memory. For Better Picture, Visit below article full of images to clear the concept : http://shivasoft.in/blog/java/servlet/how-container-handles-the-servlet-request/


How do you write a servlet?

Servlet SkeletonIf I ask you, what are the components of a Java class, you'll happily tell me that, there are first package statements and then imports and then the class declaration. Within the class brackets, we have constructors, instance variables, methods etc. That was easy, wasn't it?The same way, every Servlet has a certain set of components that are mandatory for its well-being. (I just got carried away a bit) Or I must say, for its proper functioning.A Servlets skeleton would look like below:/** servlet name** servlet description* All other stuff that is part of a Standard Class comment section*///package declarations//import statementspublic class ServletName extends HttpServlet {// Instance Variables/*** Code to Initialize the Servlet*/public void init() throws ServletException{// Servlet Initialization Code goes here}/*** The Service Method* Gets invoked for every request submitted to the Servlet* This method is optional. We mostly use doGet, doPost Methods*/protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{// Code for the Service Method goes here}/*** Process a GET request**/protected void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doGet() method goes here}/*** Process a POST request**/protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doPost() method goes here}/*** Process a PUT request**/protected void doPut(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{//Code for the doPut() method goes here}/*** You can have any number of methods for your processing* here. There is no limit as to the number of methods or* any restrictions on what you can name them.* Since this is all java code, you need to keep them* syntactically correct as per Java Coding Standards.*//*** Clean-Up*/public void destroy(){// clean up activities before the Servlet is put to death}}The above is what a Servlets skeleton would look like


How do you get servlet context instance to servlet?

You can get the ServletContext instance in a servlet by using the getServletContext() method provided by the HttpServlet class, which is the base class for servlets. This method returns the ServletContext object associated with the servlet. For example: ServletContext context = getServletContext();


Describe the life cycle of servelet?

Each servlet has the same life cycle: * A server loads and initializes the servlet * The servlet handles zero or more client requests * The server removes the servlet (some servers do this step only when they shut down) == When a server loads a servlet, the server runs the servlet's init method. Initialization completes before client requests are handled and before the servlet is destroyed. Even though most servlets are run in multi-threaded servers, servlets have no concurrency issues during servlet initialization. The server calls the init method once, when the server loads the servlet, and will not call the init method again unless the server is reloading the servlet. The server can not reload a servlet until after the server has destroyed the servlet by calling the destroy method. == After initialization, the servlet is able to handle client requests. This part of the servlet life cycle was handled in the previous lesson. == Servlets run until the server are destroys them, for example, at the request of a system administrator. When a server destroys a servlet, the server runs the servlet's destroy method. The method is run once; the server will not run that servlet again until after the server reloads and reinitializes the servlet. When the destroy method runs, another thread might be running a service request. The Handling Service Threads at Servlet Termination lesson shows you how to provide a clean shutdown when there could be long-running threads still running service requests.