简体   繁体   English

Servlet 在 doPost 方法中获取 GET 和 POST 的参数

[英]Servlet get GET and POST's parameters at the doPost method

My problem is when I'm trying to access a POST Variable with request.getParameter("name") , it works perfectly.我的问题是当我尝试使用request.getParameter("name")访问POST变量时,它运行良好。 But in some conditions, when a POST request arrives at my application, I also need to get GET Parameter from the Query String.但是在某些情况下,当POST请求到达我的应用程序时,我还需要从查询字符串中获取GET参数。

As far as I can see, with getParameter , you can only access current request's parameters, but, as in my condition, as I said, I also need to fetch GET Parameters inside doPost method.据我所知,使用getParameter ,您只能访问当前请求的参数,但是,在我的情况下,正如我所说,我还需要在doPost方法中获取GET参数。

Is there a way to fetch GET Parameters without parsing the Query String?有没有办法在不解析查询字符串的情况下获取GET参数?

If you have parameters with the same name in the query string and in the posted form data, use getParameterValues() .如果在查询字符串和发布的表单数据中具有相同名称的参数,请使用getParameterValues()

Example:-例子:-

String fromQuery = request.getParameterValues("name")[0];
String fromForm = request.getParameterValues("name")[1];

The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST . getParameter()方法可以返回(如果可能)GET 和 POST 参数,因为它在GETPOST之间透明地工作。 You don't need to do any explicit work to get the GET parameters.您无需执行任何显式工作即可获取 GET 参数。 you can use getParameter for both query parameters and POST parameters.您可以将getParameter用于查询参数和 POST 参数。

But should you do it?但是你应该这样做吗? - It's considered a poor design practice especially if there is sensitive information to be sent. - 这被认为是一种糟糕的设计实践,尤其是在有敏感信息要发送的情况下。

Take a look at this answer:看看这个答案:

I think you have a confusion here.我想你在这里有一个困惑。 You can retrieve all the request parameters (in both GET or POST or others) using the same getParameter(..) depending upon the type of request.您可以根据请求的类型使用相同的getParameter(..)检索所有请求参数(在 GET 或 POST 或其他中)。 If it's a GET request, you can retrieve all the GET parameters.如果是 GET 请求,则可以检索所有 GET 参数。

If it's a POST request, you can retrieve all the POST parameters.如果是 POST 请求,则可以检索所有 POST 参数。 You get parameters using getParameter(...) .您使用getParameter(...)获取参数。 And you make one request at a time.你一次提出一个请求。 If you make a POST request in html or JSP file, you use doPost method receive all the parameters.如果您在 html 或 JSP 文件中发出 POST 请求,则使用 doPost 方法接收所有参数。 At this point, there is nothing in GET request.此时,GET 请求中没有任何内容。 Then after that, you make a GET request, you retrieve all the parameters in doGet method.然后,您发出 GET 请求,检索 doGet 方法中的所有参数。 At this moment, there is nothing in POST.此时,POST 中没有任何内容。 Remember, HTTP requests are stateless.请记住,HTTP 请求是无状态的。

To complete @Rei answer check out this code :要完成@Rei 答案,请查看此代码:

your form你的表格

<form action="?nom=nom1">
<input type="hidden" name="nom" value="nm2"/>

your doPost你的帖子

System.out.println(request.getParameter("nom"));
        
String s = "";
for(String ss : request.getParameterValues("nom")) {
    s += "|" + ss;
}
System.out.println(s);
System.out.println(request.getParameterMap().get("nom"));

what will be printed将打印什么

nom1
|nom1|nm2
[Ljava.lang.String;@c7068db

ps : thanks to Julien for the code and testing ps:感谢Julien的代码和测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM