JSP、GET 和 POST 参数

发布于 2024-10-02 13:22:33 字数 354 浏览 2 评论 0原文

我需要用JSP做一些小任务;作为 JSP 的新手,我想知道是否有可能从 HTTP 请求中仅获取 GET 或仅 POST 参数。

我见过 ServletRequest.getParameter (以及类似的),但是似乎这些方法同时获取 GET 和 POST 参数。有没有一种方法可以只获取其中一个,而无需自己解析 URL 或请求正文?如果没有,是否存在任何优先规则,哪些值会覆盖哪些值(例如 POST 参数总是覆盖 GET 参数)?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2024-10-09 13:22:33

一般来说,请求最好在 servlet 中处理。它们有 doGet(request, response)doPost(request, response) 方法来区分两者。

如果您确实坚持在 JSP 中执行此操作,则可以使用 request.getMethod()。它将返回 GETPOST

由于这是家庭作业,我想重点是学习如何使用 servlet 及其 doX 方法,所以就这样做吧。

更新:可以获取查询字符串(request.getQueryString()),这只是获取参数,并解析它,但我不会说这是一种常见且良好的做法。

Generally, requests should better be handled in servlets. They have doGet(request, response) and doPost(request, response) methods, to differentiate the two.

If you really insist on doing it in a JSP, you can differentiate the methods using request.getMethod(). It would return GET or 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.

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.

暮光沉寂 2024-10-09 13:22:33

在 JSP 中,您可以查看请求对象来确定它是什么类型的请求(GET 或 POST),但只有一个参数映射。

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.

潜移默化 2024-10-09 13:22:33

尝试[Servlet + JSP]。在 Servlet 中,您可以选择 doPost() 或 doGet()

Try [Servlet + JSP]. At Servlet you can choose between doPost() or doGet()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文