Java 在 JSP 中不使用 HTML 表单传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/862440/
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
Passing parameter without use of HTML forms in JSP
提问by Lycana
I have done some research, and majority of the examples I have found use forms (obviously for user input) to collect data which is then passed to another JSP page through the request object.
我做了一些研究,我发现的大多数示例都使用表单(显然用于用户输入)来收集数据,然后通过请求对象将数据传递到另一个 JSP 页面。
My question is: Is it possible to pass a parameter between JSP pages if the parameter hasn't been set in HTML <form>
tags?
我的问题是:如果没有在 HTML<form>
标签中设置参数,是否可以在 JSP 页面之间传递参数?
采纳答案by Matthew Flaschen
There's no way a JSP page can tell the difference between a manually constructed GET url, e.g.:
JSP 页面无法区分手动构建的 GET url,例如:
<a href="/foo.jsp?bar=baz">Go to the next JSP page</a>
, versus something like:
<a href="/foo.jsp?bar=baz">Go to the next JSP page</a>
, 与类似的东西:
<form method="get" action="/foo.jsp">
<input name="bar" value="baz">
</form>
Either way it can be accessed through getParameter
or getParameterValues
无论哪种方式,它都可以通过getParameter
或getParameterValues
Is that what you're asking?
这就是你要问的吗?
回答by Peter ?tibrany
You usually pass data between servlet/JSP or JSP pages in scoped attributes (in request, session or application). E.g. request.setAttribute("key", data)
can set "key" attribute in one JSP, and request.getAttribute("key")
gets you this data in other JSP (if multiple JSPs are processing same request).
您通常在 servlet/JSP 或 JSP 页面之间以作用域属性(在请求、会话或应用程序中)传递数据。例如,request.setAttribute("key", data)
可以在一个 JSP 中设置“key”属性,并request.getAttribute("key")
在其他 JSP 中获取此数据(如果多个 JSP 正在处理相同的请求)。
It is possible to create fake parameters by wrapping your request, and overriding getParameter and similar method, if that is what you really need.
如果您确实需要,可以通过包装您的请求并覆盖 getParameter 和类似方法来创建假参数。
Update: my answer is talking about passing data between several parties which all process same request.This may or may not be what you want, as your question isn't clear.
更新:我的回答是在处理相同请求的多方之间传递数据。这可能是也可能不是您想要的,因为您的问题不清楚。
回答by Kris
There are a few ways to pass information from one JSP page to another.
有几种方法可以将信息从一个 JSP 页面传递到另一个页面。
1. Hidden values.
1. 隐藏的价值。
Simply write the data to an input field within a form with the type 'hidden', e.g.
只需将数据写入“隐藏”类型的表单中的输入字段,例如
<input type="hidden" name="mydata" value="<%=thedata%>">
Data written thus will get posted back when the relevant form is submitted. This can be a useful way to 'carry along' information as the user fills out a series of dialogs as all state is user side and the back and forward buttons will work as expected.
提交相关表格时,这样写入的数据将被发回。当用户填写一系列对话框时,这可能是一种“携带”信息的有用方法,因为所有状态都是用户侧,并且后退和前进按钮将按预期工作。
2. URL writing.
2.网址编写。
Attach parameters to URLs in links on the page, e.g.
将参数附加到页面链接中的 URL,例如
<a href="another.jsp?mydata=<%=thedata>>Go!</a>
This also maintains the state with the client while removing the need for a form element to be submitted.
这也保持了与客户端的状态,同时消除了提交表单元素的需要。
3. Cookies.
3. 饼干。
Should speak for itself.The state is still user side but is now handled by a cookie. More fragile in some ways since some people disable cookies. Also the back and forward buttons may do unexpected things if you are not careful
应该不言自明。状态仍然是用户端,但现在由 cookie 处理。由于某些人禁用 cookie,因此在某些方面更加脆弱。如果您不小心,后退和前进按钮也可能会做出意想不到的事情
4. Server side (session)
4. 服务器端(会话)
Finally you could store the data in a session variable on one JSP and retrieve it on the next, e.g.
最后,您可以将数据存储在一个 JSP 上的会话变量中并在下一个 JSP 上检索它,例如
session.setAttribute(String key, Object value)
and
和
session.getAttribute(String key)
Here the state is kept server side which has some benefits (the user can browse away and return without losing his place, but tends to make the back and forward buttons in the browser a bit unreliable unless you are careful. Also the value is available to all pages.
在这里,状态保持在服务器端,这有一些好处(用户可以浏览并返回而不会丢失自己的位置,但往往会使浏览器中的后退和前进按钮有点不可靠,除非您小心。此外,该值可用于所有页面。
It does however have the advantage that the information is never sent to the client and is thus more secure and more tamper proof.
然而,它的优点是信息永远不会发送给客户端,因此更安全,更防篡改。