简体   繁体   English

GET strips 查询参数的表单提交

[英]Form submission for GET strips query params

I have a jsp page which takes a parameter from my Java code and calls the URL onLoad as below.我有一个 jsp 页面,它从我的 Java 代码中获取一个参数,并如下调用 URL onLoad。
When I do this the form strips the resultant Url's parameters.当我这样做时,表单会去除生成的 Url 参数。 As in everything after?就像之后的一切? is stripped.被剥离。 So the below result url http://hostname/abc?data=123 appears as http://hostname/abc which is not expected.因此,下面的结果 url http://hostname/abc?data=123显示为http://hostname/abc ,这是不期望的。
What am I missing or is it completely wrong to use form for GET requests.我错过了什么,或者将表单用于GET请求是完全错误的。 Is there a better way to do this using javascript like window.location=result;有没有更好的方法来使用 javascript 像window.location=result; My jsp page is:我的 jsp 页面是:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head>

<body onload="javascript:document.Form.submit()">
     <%
     String result = (String)request.getAttribute("result");
     %>
     <form action= <%= result %> method="get" name="Form">

         
     </form>

 </body>
</html>



Normaly when a form is submitted the url is constructed by the form and all form controls are passed as query parameters (that happens only when the form has the submit method of GET).通常,当提交表单时,url 由表单构造,并且所有表单控件都作为查询参数传递(仅当表单具有 GET 提交方法时才会发生)。

You trying to construct the url with query parameters that the form is supposed to construct is wrong.您尝试使用表单应该构造的查询参数构造 url 是错误的。 Hence you get the error.因此你得到错误。

Share with us what you try to achieve so we can understand a bit better how to help you.与我们分享您尝试实现的目标,以便我们更好地了解如何为您提供帮助。

<body onload="javascript:document.Form.submit()">

What I understand from that is that you try when the page is first loaded to call another url.我从中了解到的是,您在首次加载页面时尝试调用另一个 url。 That could happen on servlet level so there you can do with java whatever you want.这可能发生在 servlet 级别,因此您可以随心所欲地使用 java。

Using pure javascript as described here get request from js you could achieve what you try to do with the form action.使用此处描述的纯 javascript从 js 获取请求,您可以实现您尝试使用表单操作执行的操作。

However keep in mind that this would not be considered best practice when you deal with JSP但是请记住,当您处理 JSP 时,这不会被视为最佳实践

Right.正确的。 I am trying to redirect to another URL with the right parameter.我正在尝试使用正确的参数重定向到另一个 URL。 I have now updated form to have a hidden input parameter named as data as below: My final intended URL is http://hostname/abc?data=a1%2Bz%3D%3D However the value in the form keeps appending the numeric 25 after each % so the value in data is a1%252Bz%253D%253D.我现在已经更新了表单以具有一个名为 data 的隐藏输入参数,如下所示: 我的最终预期 URL 是 http://hostname/abc?data=a1%2Bz%3D%3D 但是表单中的值一直附加数字 25在每个 % 之后,因此数据中的值为 a1%252Bz%253D%253D。 Is some wierd URL encoding taking place here.这里是否发生了一些奇怪的 URL 编码。

Right the browser will try to encode each url before trying to access it.在尝试访问之前,浏览器会尝试对每个 url 进行编码。 Check here browser encoding .As you can see % will be encoded to %25.在这里检查浏览器编码。如您所见,% 将被编码为 %25。 On the other end when some system parses that url it will first decode it so the %25 will again become 25. That's pretty normal另一方面,当某些系统解析 url 时,它将首先对其进行解码,因此 %25 将再次变为 25。这很正常

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

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