64

What is the Java equivalent of PHP's $_POST? After searching the web for an hour, I'm still nowhere closer.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Patrick
  • 90,362
  • 11
  • 51
  • 61

5 Answers5

59

Here's a simple example. I didn't get fancy with the html or the servlet, but you should get the idea.

I hope this helps you out.

<html>
<body>
<form method="post" action="/myServlet">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" />
</form>
</body>
</html>

Now for the Servlet

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

public class MyServlet extends HttpServlet {
  public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {

    String userName = request.getParameter("username");
    String password = request.getParameter("password");
    ....
    ....
  }
}
ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • 2
    can you tel me how to get the POST request value in servlet page? I'm using $.ajax call not form action and XMLHttpRequest? – selladurai May 20 '11 at 09:23
  • my Question http://stackoverflow.com/questions/6068510/i-couldnt-get-the-post-value-in-servlet-page – selladurai May 20 '11 at 09:30
53

Your HttpServletRequest object has a getParameter(String paramName) method that can be used to get parameter values. http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

Ryan Ahearn
  • 7,886
  • 7
  • 51
  • 56
26

POST variables should be accessible via the request object: HttpRequest.getParameterMap(). The exception is if the form is sending multipart MIME data (the FORM has enctype="multipart/form-data"). In that case, you need to parse the byte stream with a MIME parser. You can write your own or use an existing one like the Apache Commons File Upload API.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 7
    Note: this answer predates the release of Servlet 3.0 and standard support for `multipate/form-data`. See the [Java EE 6 API](http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/MultipartConfig.html) and/or [this blog post](http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html) – McDowell Mar 09 '12 at 09:05
3

The previous answers are correct but remember to use the name attribute in the input fields (html form) or you won't get anything. Example:

<input type="text" id="username" /> <!-- won't work --> <input type="text" name="username" /> <!-- will work --> <input type="text" name="username" id="username" /> <!-- will work too -->

All this code is HTML valid, but using getParameter(java.lang.String) you will need the name attribute been set in all parameters you want to receive.

hgc2002
  • 318
  • 2
  • 11
1

For getting all post parameters there is Map which contains request param name as key and param value as key.

Map params = servReq.getParameterMap();

And to get parameters with known name normal

String userId=servReq.getParameter("user_id");
Siddappa Walake
  • 303
  • 5
  • 14