Wednesday, March 1, 2017

What is doGet() and doPost() methods in Servlet

The Servlet interface is the central abstraction of the Servlet API. All servlets implement this either directly or commonly by extending a class that implements the interface. The TWO classes in the Servlet API that implement Servlet interface are GenericServlet and HttpServlet. For most purposes developers will extend HttpServlet to implement their Servlets.

HttpServelt class extends the GenericServlet. It is commonly used when developing Servlets that receive and process requests. The HttpServlet class provides specilized methods that handle the various types of HTTP requests.These method are doGet(),doOptions(),doDelete(),doHead(),doPost(),doPut(),doTrace(). A Servlet developer typically overrides one of these methods. In this post we will discuss only doGet() and doPost() methods because these are commonly used when handling form input.



Handling HTTP Get Requests:

Here we will discuss a Servlet that handles an HTTP GET Request. The GET method is the most common form of HTTP Request.The Servlet is invoked when a form on a webpage is submitted. When a Servlet Container first routes to a Servlet, it invokes the service() method.By default, the service() method will dispatch to the doGet() or doPost() based on the method information given in the HTTP Request Header.You must override doGet() method. The doGet() method receives HttpServelt Request and HttpServeltResponse object and must throw java.io.ServeltException and java.io.IOException.

Example:
public void doGet(HttpServletRequest req,HttpServeltResponse res)throws 
ServeltException,IOException
{
res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.println("learnprogrammingbyluckysir");
}

Note: 

1)The doGet() method is overridden to process any HTTP GET request that are sent to the Servlet. It uses getParameter() method of HttpServeletRequest. 

2) Parameters for an HTTP GET request are included as part of the URL that is sent to the web browser.



Handling HTTP POST Requests:

Here we will learn a Servlet that handles an HTTP Post requests. HTTP employs the POST method to transfer larger amounts of HTML Form data.The doPost() method called much like doGet() method. If the Servlet's service() method receives an HTTP POST request,the Servlets's doPost() method is called.

The doPost() method is overridden to process any HTTP POST request that are sent to the Servlet. It uses getParameter() method of HttpServeletRequest. The doPost() method receives HttpServelt Request and HttpServeltResponse object and must throw java.io.ServeltException and java.io.IOException.

Example:

public void doPost(HttpServletRequest req,HttpServeltResponse res)throws 
ServeltException,IOException
{
res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.println("learnprogrammingbyluckysir");
}

Note: Parameters for HTTP POST Request are not included as part of the URL that is sent to the web browser. The parameters names and values are sent in the body of the HTTP request.

Difference Between doGet() and doPost():

Recommend To Read:

JSP Interview Questions
Servlet Interview Questions
Spring Interview Questions
Struts Interview Questions
Hibernate Interview Questions
Servlet Life cycle

Let me know your comments  write down in the below post and share this post and keep follow me for latest updates.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...