从上级目录中的servlet转发到子文件夹中的jsp
我有
root/logged/form.jsp
root/servlet
root/logged/form.jsp
jsp 页面 logged/form.jsp
,该页面将表单提交给 servlet action="../update"
。现在我想添加一些参数来请求并将其转发到 logged/form.jsp
但它不起作用,并且仅在根上下文中向我显示 form.jsp root/servlet
。请帮助我应该将我的请求转发到哪个网址。我无法使用 sendRedirect 因为必须保留请求对象。
我尝试过使用forward(logged/form.jsp
)和forward(/logged/form.jsp
)和forward(/form.jsp
) 在我的 servlet 中
i have
root/logged/form.jsp
root/servlet
root/logged/form.jsp
I have jsp page logged/form.jsp
this submits form to servlet action="../update"
. Now i want to add some parameters to request and forward it to logged/form.jsp
but its not working and showing me form.jsp in root context only root/servlet
. Please help what url should i forward my request to. I cannot use sendRedirect as has to retain request object.
I have tried with forward(logged/form.jsp
) and forward(/logged/form.jsp
) and forward(/form.jsp
) in my servlet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试始终使用绝对路径(以 / 开头),它被解释为相对于上下文根。
Try always using absolute paths (starting with a / ) which are interpreted as relative to the context root.
/logged/form.jsp
应该是正确的。我建议阅读应用程序服务器日志。很有可能里面有一个IllegalStateException:响应已提交
。等等,等等,你的实际问题是你想要更改访问者在地址栏中看到的URL?
如果是这样,那么不,前锋不可能做到这一点。然后我建议从另一边解决问题。只需将
form.jsp
隐藏在/WEB-INF
文件夹中,并始终使用 servlet 来获取/发布表单。伪:
将此 servlet 映射到
/logged/form
的url-pattern
上,替换还可以更进一步,采用页面控制器模式,利用
HttpServletRequest#getPathInfo()
来获取请求路径(以及JSP文件的路径),这样就不用再沸沸扬扬了每个 JSP 都有一个新的 servlet。The/logged/form.jsp
ought to be the right one. I suggest to read the appserver logs. Big chance that there's anIllegalStateException: response already committed
inside.Wait, wait, your actual problem is thus that you want to change the URL which the visitor sees in the address bar?
If so, then no, that isn't possible with a forward. I'd then suggest to solve the problem from the other side on. Just "hide"
form.jsp
in the/WEB-INF
folder and use a servlet all the time to get/post the form.Pseudo:
map this servlet on an
url-pattern
of/logged/form
, replace the<form method="post" action="/servlet">
by<form method="post" action="/logged/form">
and then you can use/invoke it byhttp://example.com/logged/form
.You can also go a step further by adopting the page controller pattern and make use of
HttpServletRequest#getPathInfo()
to obtain the request path (and the JSP file's path) so that you don't need to boil a new servlet for every JSP.