Hi,
I wrote a page login.jsp (tomcat6) which has login form (username, password etc).
I've mapped the form action and servlet so the form submission results in invocation of login servlet.
1. login.jsp has form that invoke servlet.
<form name="login_form" action="servlet/login" method="POST">
2. servlet mapping in deployment descriptor (web.xml)
<servlet>
<servlet-name>ServletLogin</servlet-name>
<servlet-class>com.masatosan.loginservlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLogin</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
So, from the jsp page, when a user clicks on login button, the servlet (LoginServlet.class) takes care of login business.
When login fails such as mismatched password etc, the servlet redirects the user back to login page.
code snippet from LoginServlet.class
String address = "home.jsp"
if(hasError) {
address = "register.jsp"
}
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
Problem:
It works great for 1st request.
provide invalid credential (bad username or password) to login form.
hasError flag is set to true and redirect me back to the login page (with URL changed from original http://localhost:8080/login.jsp to http://localhost:8080/servlet/login)
I'm happy so far but not after the below.
- provide invalid credential to login form again
Result:
Now I get HTTP 404 and URL is: http://localhost:8080/servlet/servlet/login (with extra "servlet")
The solution I'm looking for when I redirect a user due to login failure, I want to redirect to original login page, is it possible to redirect to login.jsp OR instead not having extra "servlet" text in URL that cause 404?
NOTE: The online tutorial I read mentions not including "servlet" in url mapping like I did <url-pattern>/servlet/login</url-pattern>
but the jsp host I have force me to do (mount directive) due to they have to distinguish php and other things :(
UPDATE:
I found this on the net, have not tested yet but will update..
response.sendRedirect(LOGIN_PAGE);
Solution
sendRedirect approach was not what I wanted. I needed to forward request/response via dispatcher but sendRedirect only takes response as an argument.
The trick to original question was to use absolute path in form as suggested. My fix which worked is:
(login.jsp)
<%
String path = request.getContextPath();
%>
<form name="login_form" action="<%= path %>/servlet/login" method="POST">