使用 Java Servlet 访问 post 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:05:53  来源:igfitidea点击:

Accessing post variables using Java Servlets

javahttpservlets

提问by Patrick

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

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

采纳答案by Ryan Ahearn

Your HttpServletRequestobject 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)

回答by McDowell

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

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

回答by ScArcher2

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

回答by hgc2002

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

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

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

回答by Siddappa Walake

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

为了获取所有发布参数,有 Map ,其中包含请求参数名称作为键和参数值作为键。

Map params = servReq.getParameterMap();

And to get parameters with known name normal

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

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