如何处理之前servlet转发的页面请求?

发布于 2024-09-18 17:48:41 字数 2490 浏览 6 评论 0原文

我有一个 JSP 页面,有 2 个表单在 Tomcat 6 上运行。一个是注册表单,第二个是登录表单。 (请注意上下文路径,它稍后会杀了我)

(regis_and_login.jsp)

    <% String path = request.getContextPath(); %>

    //regis form
    <form id="regis_form" name="register_form" 
action="<%= path %>/servlet/process_register" method="post">

     //login form
    <form name="login_form" action="<%= path %>/servlet/login" method="POST">

目标

如果添加新用户的插入语句失败,我想将用户重定向回regis_and_login.jsp页面显示我的自定义错误。

servlet 行为

(登录 servlet)

如果登录失败,我的自定义错误消息将添加到请求中(即 request.setAttribute("error", "there is error!") 和调度程序将转发回 regis_and_login.jsp 页面并正确显示错误消息,但由于这是转发,因此 URL 会从 http://localhost:8080/regis_and_login.jsphttp://localhost:8080/servlet/login

(RegisterServlet) 注册 servlet 的工作方式相同。如果检测到任何错误,我会根据请求调用 setAttribute() 并调用 RequestDispatcher 的转发函数将用户重定向回 regis_and_login.jsp 页面,并显示我的自定义错误。

问题

每个表单单独工作都很好,但是当一起使用时,就会出现一些问题。

  1. 在登录表单中填写无效的输入,以便您被重定向回 http://localhost:8080/servlet/登录,我的自定义消息正确显示。

  2. 在同一页面(http://localhost:8080/servlet/login),填写在注册表单中并提交。

结果:

现在 URL 看起来很疯狂。

The requested resource (/servlet/servlet/process_register) is not available.

我的猜测是,这是因为在 regis_and_login.jsp 中,我设置了 contextPath 并连接到表单的操作值: action="<%= path %>/servlet/process_register

所以我相信通过转发修改URL后(http://localhost:8080/servlet/login ), contextPath 现在设置为“/servlet/”,这就是为什么我请求 /servlet/servlet/process_register 而不仅仅是 /servlet/process_register

有几个选项 ?在我看来:

  1. 只需使用绝对路径来表示表单的操作路径

  2. 不要转发,而是使用response.sendRedirect(address)来避免URL更改(但是这个不允许我根据请求设置属性,这样我就无法在 JSP 页面上显示错误消息..这违背了我的目标)

我应该使用绝对路径吗?

如果您需要更多说明,请告诉我。

更新

我要求服务台删除安装指令,现在它接受所有传入请求。 web.xml 和表单操作 URL 不再提及“/servlet”,代码开始正常工作。现在我认为有额外的“/servlet”的原因,我想知道是否是由于 mount 指令造成的。

I have a JSP page that has 2 forms running on Tomcat 6. One is register form, Second is login form.
(please pay attention to context path, it's going to kill me later)

(regis_and_login.jsp)

    <% String path = request.getContextPath(); %>

    //regis form
    <form id="regis_form" name="register_form" 
action="<%= path %>/servlet/process_register" method="post">

     //login form
    <form name="login_form" action="<%= path %>/servlet/login" method="POST">

GOAL

If insert statement to add new user fails, I want to redirect the user back to regis_and_login.jsp page with my custom error displayed.

servlet behavior

(login servlet)

If login fails, my custom error message is added to request (i.e. request.setAttribute("error", "there is error!") and dispatcher will forward back to the regis_and_login.jsp page and displays the error message properly but since this is forward, URL changes FROM http://localhost:8080/regis_and_login.jsp TO http://localhost:8080/servlet/login

(RegisterServlet)
Register servlet works same way. If any error is detected, I call setAttribute() on request and call RequestDispatcher's forward function to redirect the user back to regis_and_login.jsp page with my custom error displayed.

PROBLEM

Each form works fine individually but when used together, it has some problem.

  1. Fill in invalid input to login form so you get redirected back to http://localhost:8080/servlet/login with my custom message properly showing up.

  2. On the same page (http://localhost:8080/servlet/login), fill in the registration form and submit.

Result:

Now the URL looks crazy.

The requested resource (/servlet/servlet/process_register) is not available.

My guess is this is because in regis_and_login.jsp, I set contextPath and concatenate to form's action value: action="<%= path %>/servlet/process_register

So I believe after the URL is modified by forwarding (http://localhost:8080/servlet/login), contextPath is now set to "/servlet/" and that's why I am requesting /servlet/servlet/process_register instead of just /servlet/process_register ?

There are few options in my mind:

  1. Just use absolute path to form's action path

  2. Instead of forwarding, use response.sendRedirect(address) to stay away from URL change (but this disallow me to set attribute on request so I can't display error message on my JSP page..this is against my goal)

Should I just use absolute path?

Please let me know if you need more clarification..

UPDATE

I asked help desk to remove mount directives and now it accepts all incoming request. The web.xml and form action URL no longer mentions "/servlet" and code started working just fine. Now I think the reason for having extra "/servlet", I wonder if it is due to the mount directive..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

靖瑶 2024-09-25 17:48:41

但由于这是转发,URL 更改为 http://localhost:8080/regis_and_login.jsp TO http://localhost:8080/servlet/login

这句话有点奇怪。转发根本不会更改 URL。转发仅指向另一个资源(Servlet 或 JSP)以继续处理当前 HTTP 请求。 URL 更改是由于表单操作 URL 与原始请求 URL 不同而导致的。您正在此 URL 上触发同步 POST 请求。浏览器地址栏中的 URL 成为表单操作 URL。

现在这个网址看起来很疯狂。
请求的资源(/servlet/servlet/process_register)不可用。

那么,一开始你有一个上下文路径 "/" ,提交表单后它就变成了 "/servlet" ?抱歉,该问题是由其他原因引起的。您的 Web 应用程序是否部署为 ROOT?您正在运行您认为正在运行的代码吗?


但是,我有两个建议如何以不同的方式解决此问题:

  1. 不要将 servlet 映射到 /servlet/process_register/servlet/login 上,而只映射到 <代码>/process_register 和/login

    ... <表单操作=“登录”方法=“发布”>
  2. 或者,将 JSP 文件隐藏在 /WEB-INF 文件夹中,在 /register_and_login 上使用单个 servlet 进行 GET 和 POST,并让 servlet 执行所需的操作动作取决于按下的按钮。这样,URL 始终保持 http://localhost:8080/register_and_login

but since this is forward, URL changes FROM http://localhost:8080/regis_and_login.jsp TO http://localhost:8080/servlet/login

This phrase is a bit odd. A forward doesn't change the URL at all. A forward just points to another resource (Servlet or JSP) to continue processing the current HTTP request. The URL change is caused because the form action URL is different from the original request URL. You're firing a synchronous POST request on this URL. The URL in the browser address bar becomes the form action URL.

Now the URL looks crazy.
The requested resource (/servlet/servlet/process_register) is not available.

So, in the beginning you have a context path of "/" and after submitting the form it becomes "/servlet"? Sorry, the problem is caused by something else. Is your webapplication deployed as ROOT? Are you running the code you think you're running?


However, I have two recommendations how to approach this issue differently:

  1. Don't map the servlets on /servlet/process_register and /servlet/login but just on /process_register and /login.

    <form action="process_register" method="post">
    ...
    <form action="login" method="post">
    
  2. Or, hide the JSP file away in /WEB-INF folder, use a single servlet on /register_and_login for both GET and POST and let the servlet execute the desired action depending on the button pressed. This way the URL stays http://localhost:8080/register_and_login all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文