Hi,
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.
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.
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:
Just use absolute path to form's action path
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..