Monday 11 August 2014

getMethod() : Get the name of the Http Requested method

public String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(urlPatterns = {"/GetRequestedMethod"})
public class GetRequestedMethod extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
        try(PrintWriter out = res.getWriter()){
            String method;
            method = req.getMethod();
            
            out.println("<html><head><title>Server Details</title>");
            out.println("<body><h1>");
            out.println("Requested Method type: " + method);
            out.println("</h1></body></html>");
        }
    }   
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
        doPost(req, res);
    }
}

 Run the servlet like below.
'http://localhost:8080/servlet/GetRequestedMethod'

Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment