views:

59

answers:

1

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.

  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..

+1  A: 

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.

BalusC
Yes, the form action URL does the URL change not a forward. (thanks for the correction) I will look into the approach 1 and see if this URL issue resolves. Will update the result later ;)
masato-san
Another thing I discovered is that my hosting server has mount directive set to "/servlet", that is the reason I had to follow even though the links you gave me in other post says it's bad practice. Fortunately they can remove it so will see!
masato-san