2

I try to extract the form data from an javax.servlet.http.HttpServletRequest instance. As recommended online I tried request.getParameter(String paramKey), but it did not work. request.getParameterValues(), request.getParameterNames() and request.getParameterMap() return no form data as well. What I want is a Map with form data or another method to get them.

loresIpsos
  • 175
  • 1
  • 1
  • 11
  • Possible duplicate of [Getting request payload from POST request in Java servlet](http://stackoverflow.com/questions/14525982/getting-request-payload-from-post-request-in-java-servlet) – AB D CHAMP Feb 03 '17 at 10:12

2 Answers2

6

it will behave where you write the code request.getParameter(). this thing always needs to write in the doGetPost() Method of the servlet like mentioned below. refer the following example.

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException  {

     String id = req.getParameter("realname");
     String password = req.getParameter("mypassword");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

    String id = req.getParameter("realname");
    String password = req.getParameter("mypassword");
}
Ravi MCA
  • 2,315
  • 2
  • 17
  • 26
0

There are several methods you need to override. They are doPost(), doGet(), service()

doPost() will get executed when the received request type is POST.

doGet() will get executed when the received request type is GET.

If you want a single method which should be executed for the both post, get you better to use service() method.

Example:

 public class TestServlet{

    public void service( HttpServletRequest request, HttpServletResponse response ){

        request.getParameter( "paramterName" ).
    }
 }
Ravi MCA
  • 2,315
  • 2
  • 17
  • 26