getMethod() Method Example

getMethod() Method Example

getMethod() method is defined in HttpServletRequest interface from javax.servlet.http package.

With getMethod() method, the Servlet Programmer can know what method (either get or post) is used by the client from HTML file to call the Servlet.

Let us see what Java API says about this method.

  • java.lang.String getMethod(): Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.

The method getMethod() returns a string, the attribute of METHOD in FORM tag used by the client to call the servlet, generally either GET or POST.

Example on getMethod(): Let us see the output by writing a program.

HTML Program: ClientData.html

Observe, the client uses GET value for attribute METHOD.

web.xml entry for ClientInformation servlet

Servelet Program:

Output screen of ClientData.html with text field filled up. Anyhow, the text field does not have any purpose here.


The output screen when submit button is clicked.


Leave a Reply

Your email address will not be published. Required fields are marked *