Reputation: 51
I'm building an application and currently working on my login page. When I launch Tomcat it starts up fine and opens a browser which displays my index.jsp page to log in. However on successful log in I am not forwarded to the next jsp page as instructed in the login servlet, instead I get a 404 "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
I have checked the web.xml I which looks fine, I'm using annotations to map servlets. I dont know what else to try. Even if I deliberately enter incorrect log in details I receive the same error rather than the output contained in the "else" statement.
@WebServlet("/Login")
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String uname = request.getParameter("username");
String pwd = request.getParameter("password");
if (uname.equals("MyUsername") && pwd.equals("MyPassword")){
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/welcome.jsp");
dispatcher.forward(request, response);
}else {
PrintWriter writer = response.getWriter();
writer.write("User details not recognised");
}
}
}
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Upvotes: 5
Views: 1380
Reputation: 16477
If you forward to /WEB-INF/welcome.jsp
, the server looks for a file welcome.jsp
inside a directory WEB-INF
in your context root, that is the WEB-INF
directory.
So, you should not forward to /WEB-INF/welcome.jsp
, but to /welcome.jsp
.
Anyway, WEB-INF
is reserved and does not work, no matter where you redirect.
Upvotes: 2