使用Java Servlet访问post变量

时间:2020-11-30 11:50:30

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

什么是PHP的$ _POST的Java等价物?在网上搜索了一个小时后,我仍然离我更近了。

5 个解决方案

#1


45  

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)

您的HttpServletRequest对象具有可用于获取参数值的getParameter(String paramName)方法。 http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

#2


49  

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

这是一个简单的例子。我对html或servlet并不满意,但你应该明白这个想法。

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

现在为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");
    ....
    ....
  }
}

#3


21  

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.

POST变量应该可以通过请求对象访问:HttpRequest.getParameterMap()。例外情况是表单是否发送多部分MIME数据(FORM具有enctype =“multipart / form-data”)。在这种情况下,您需要使用MIME解析器解析字节流。您可以编写自己的或使用现有的Apache Commons File Upload API。

#4


1  

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:

以前的答案是正确的,但记得在输入字段(html表单)中使用name属性,否则你将得不到任何东西。例:

<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 -->

<! - 将无效 - > <! - 将工作 - > <输入type =“text”name="“username”id" =“username” /> <! - 也可以 - >

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.

所有这些代码都是HTML有效的,但是使用getParameter(java.lang.String),您需要在要接收的所有参数中设置name属性。

#5


1  

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

为获取所有post参数,Map包含请求参数名称作为键,param值作为键。

Map params = servReq.getParameterMap();

And to get parameters with known name normal

并获取已知名称正常的参数

String userId=servReq.getParameter("user_id");

#1


45  

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)

您的HttpServletRequest对象具有可用于获取参数值的getParameter(String paramName)方法。 http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

#2


49  

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

这是一个简单的例子。我对html或servlet并不满意,但你应该明白这个想法。

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

现在为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");
    ....
    ....
  }
}

#3


21  

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.

POST变量应该可以通过请求对象访问:HttpRequest.getParameterMap()。例外情况是表单是否发送多部分MIME数据(FORM具有enctype =“multipart / form-data”)。在这种情况下,您需要使用MIME解析器解析字节流。您可以编写自己的或使用现有的Apache Commons File Upload API。

#4


1  

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:

以前的答案是正确的,但记得在输入字段(html表单)中使用name属性,否则你将得不到任何东西。例:

<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 -->

<! - 将无效 - > <! - 将工作 - > <输入type =“text”name="“username”id" =“username” /> <! - 也可以 - >

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.

所有这些代码都是HTML有效的,但是使用getParameter(java.lang.String),您需要在要接收的所有参数中设置name属性。

#5


1  

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

为获取所有post参数,Map包含请求参数名称作为键,param值作为键。

Map params = servReq.getParameterMap();

And to get parameters with known name normal

并获取已知名称正常的参数

String userId=servReq.getParameter("user_id");

相关文章