如何使用JSP翻页保留参数
a.jsp:
<form method=post action="b.jsp">
<INPUT TYPE="text" name="test" value="hello world!">
</form>
b.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
response.sendRedirect("http://example.com/c.jsp");
%>
http: //example.com/c.jsp:(此页面在其他服务器中)
<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
System.out.println(request.getParameterValues("test")); //must use request.getParameter
%>
如何在c.jsp
中获取参数test
?
请不要使用 GET 方法,例如: response.sendRedirect("c.jsp?test=helloworld!");
我尝试使用forward,但抛出异常:
404 Not Found
/acc/http:/example.com/ccc.jsp was not found on this server.
感谢您的帮助:)
a.jsp:
<form method=post action="b.jsp">
<INPUT TYPE="text" name="test" value="hello world!">
</form>
b.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
response.sendRedirect("http://example.com/c.jsp");
%>
http://example.com/c.jsp: (this page in an other server)
<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
System.out.println(request.getParameterValues("test")); //must use request.getParameter
%>
how can I get parameter test
in c.jsp
?
please don't using GET method like: response.sendRedirect("c.jsp?test=helloworld!");
I tried using forward,but throw exception:
404 Not Found
/acc/http:/example.com/ccc.jsp was not found on this server.
thanks for help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能(除了你提出的
GET
解决方案,事实上这并没有那么糟糕)。好吧,您可以使用会话来存储输入值并在读取后将其删除(类似于闪存范围),但如果使用使用同一会话的多个选项卡,您可能会遇到麻烦。
如果您想使用客户端重定向。如果您可以使用转发(服务器端重定向):
request.getRequestDispatcher("/c.jsp").forward(request, response)
- 因此请求保持不变并且您拥有请求参数可用的。最后 - 不要在 JSP 中这样做。使用 servlet 来编写逻辑。
更新:由于您的 c.jsp 位于另一台服务器上,因此您实际上没有任何选项(除了 GET 之外),
看来您可以使用 307 重定向来使重定向使用 post - 检查这个问题(这是关于asp.net的,但是java中也有类似的方法响应类)
You can't (apart from the
GET
solution you proposed, and which in fact is not that bad).Well, you can use the session to store the input value and remove it after you read it (something like a flash scope), but if mulitple tabs using the same session are used you may get in trouble with that.
That's if you want to use client-side redirect. If you can use forwarding (server-side redirect):
request.getRequestDispatcher("/c.jsp").forward(request, response)
- thus the request remains the same and you have the request parameters available.And finally - don't do that in JSPs. Use servlets for writing your logic.
Update: since your c.jsp is on another server you don't really have any options (apart from GET)
It appears you can use a 307 redirect to make the redirect use post - check this question (it's about asp.net, but there are similar methods in the java response class)
您可以尝试
requestforward()
(参考)或
。(参考)You can try
request forward()
(reference) or<jsp:forward>
.(reference)只需让表单直接提交到该 URL 即可。
无需走一些笨拙的弯路。
Just let the form submit to that URL directly.
No need for kludgy detours.