Accessing post variables using Java Servlets
What is the Java equivalent of PHP's $_POST
? After searching the web for an hour, I'm still nowhere closer.
Solution 1:
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");
....
....
}
}
Solution 2:
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)