从上级目录中的servlet转发到子文件夹中的jsp

发布于 2024-08-11 21:18:40 字数 478 浏览 4 评论 0原文

我有

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 技术交流群。

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

发布评论

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

评论(3

烟若柳尘 2024-08-18 21:18:40

尝试始终使用绝对路径(以 / 开头),它被解释为相对于上下文根。

Try always using absolute paths (starting with a / ) which are interpreted as relative to the context root.

彩虹直至黑白 2024-08-18 21:18:40

/logged/form.jsp 应该是正确的。我建议阅读应用程序服务器日志。很有可能里面有一个IllegalStateException:响应已提交

等等,等等,你的实际问题是你想要更改访问者在地址栏中看到的URL?

如果是这样,那么不,前锋不可能做到这一点。然后我建议从另一边解决问题。只需将 form.jsp 隐藏在 /WEB-INF 文件夹中,并始终使用 servlet 来获取/发布表单。

伪:

protected void doGet(request, response) {
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

protected void doPost(request, response) {
    doYourSubmitThingHere();
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

将此 servlet 映射到 /logged/formurl-pattern 上,替换

; 通过 然后您可以通过 http://example.com 使用/调用它/记录/表单

还可以更进一步,采用页面控制器模式,利用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 an IllegalStateException: 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:

protected void doGet(request, response) {
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

protected void doPost(request, response) {
    doYourSubmitThingHere();
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

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 by http://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.

澜川若宁 2024-08-18 21:18:40
req.getRequestDispatcher("../ïndex.jsp").forward(req,resp);
req.getRequestDispatcher("../ïndex.jsp").forward(req,resp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文