JSP,GET和POST参数 [英] JSP, GET and POST parameters
问题描述
我需要用JSP做一些小任务;对JSP非常陌生我想知道是否有可能只从HTTP请求中获取 GET 或仅 POST 参数。
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 GET or only POST parameters from the HTTP request.
我见过 ServletRequest.getParameter (和相似)但似乎这些方法同时获得GET 和 POST参数。有没有办法只获取其中一个,而无需自己解析URL或请求体?如果没有,是否有任何优先规则值覆盖哪些(如POST参数总是覆盖GET参数)?
I have seen ServletRequest.getParameter (and alikes) but it seems that those methods get both GET and POST 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)?
推荐答案
一般来说,请求最好在servlet中处理。他们有 doGet(请求,响应)
和 doPost(请求,响应)
方法,以区分这两者。
Generally, requests should better be handled in servlets. They have doGet(request, response)
and doPost(request, response)
methods, to differentiate the two.
如果你真的坚持在JSP中这样做,你可以使用 request.getMethod()
。它会返回 GET
或 POST
。
If you really insist on doing it in a JSP, you can differentiate the methods using request.getMethod()
. It would return GET
or POST
.
因为这个是家庭作业,我想重点是学习如何使用servlet及其 doX
方法,所以这样做。
Since this is homework, I guess the point is to learn how to use servlets and their doX
methods, so do it that way.
更新:您可以获取查询字符串( request.getQueryString()
),这只是get参数,并解析它,但我不会说这是一种常见且良好的做法。
Update: You can get 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.
这篇关于JSP,GET和POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!