Java JSP、GET 和 POST 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4178041/
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
JSP, GET and POST parameters
提问by poke
I am required to do some small tasks with JSP; being very new to JSP I was wondering if there was any possibility to get only GETor only POSTparameters from the HTTP request.
我需要用JSP做一些小任务;作为 JSP 的新手,我想知道是否有可能仅从HTTP 请求中获取GET或POST参数。
I have seen ServletRequest.getParameter(and alikes) but it seems that those methods get both GET andPOST parameters. Is there a way to get only one of them, without parsing the URL or the request body myself? And if not, is there any precedence rule which values overwrite which (like POST parameters always overwriting GET parameters)?
我见过ServletRequest.getParameter(和类似的),但这些方法似乎同时获得 GET和POST 参数。有没有办法只获取其中一个,而无需自己解析 URL 或请求正文?如果没有,是否有任何优先规则可以覆盖哪些值(例如 POST 参数总是覆盖 GET 参数)?
采纳答案by Bozho
Generally, requests should better be handled in servlets. They have doGet(request, response)
and doPost(request, response)
methods, to differentiate the two.
通常,请求最好在 servlet 中处理。他们有doGet(request, response)
和doPost(request, response)
方法,来区分这两者。
If you really insist on doing it in a JSP, you can differentiate the methods using request.getMethod()
. It would return GET
or POST
.
如果您确实坚持在 JSP 中执行此操作,则可以使用request.getMethod()
. 它会返回GET
或POST
。
Since this is homework, I guess the point is to learn how to use servlets and their doX
methods, so do it that way.
既然这是作业,我想重点是学习如何使用 servlet 及其doX
方法,所以就这样做吧。
Update:You canget the query string (request.getQueryString()
), which is only the get parameters, and parse it, but I wouldn't say that's a common and good practice.
更新:您可以获取查询字符串 ( request.getQueryString()
),它只是获取参数,并对其进行解析,但我不会说这是一种常见且好的做法。
回答by Pointy
In JSP, you can look at the request object to determine what kind of request it was (GET or POST), but there's only one parameter map.
在 JSP 中,您可以查看请求对象以确定它是哪种请求(GET 或 POST),但只有一个参数映射。
回答by apichaic
Try [Servlet + JSP]. At Servlet you can choose between doPost() or doGet()
尝试[Servlet + JSP]。在 Servlet 中,您可以在 doPost() 或 doGet() 之间进行选择