I start off on the page
http://localhost/Chapter4/login.html
I enter an invalid login which should see the RequestDispatcher redirect me back to the Login.html page I started at.
but it instead goes to
http://localhost/Chapter4/servlet/LoginServlet ?
Why is this?
Here are the files I have ... AccountServlet is mot included
public class LoginServlet extends HttpServlet
{
Hashtable users = new Hashtable();
//This method will be called when someone types the URL for this servlet in the browser
public void doGet( HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost( req, res );
}
//This method retrieves the ID/Pwd, verifies them. If valid it forwards the request to the AccountServlet,
//else to the Login page.
public void doPost( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
String id = req.getParameter("id");
String pwd = req.getParameter("pwd");
if( id != null && pwd != null && pwd.equals( users.get(id)) )
{
//Set the user id in the Request.
req.setAttribute("id", id);
ServletContext context = getServletContext();
RequestDispatcher rd = context.getRequestDispatcher("/servlet/AccountServlet");
rd.forward( req, res );
}
else
{
System.out.println("Invalid ligin, going back to the Login page");
RequestDispatcher rd = req.getRequestDispatcher("../login.html");
rd.forward(req, res );
}
}
public void init()
{
System.out.println( "\nInitializing Login, creating some user names\n\n" );
users.put("ann", "aaa" );
}
}
Web.xml
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AccountServlet</servlet-name>
<servlet-class>AccountServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AccountServlet</servlet-name>
<url-pattern>/servlet/AccountServlet</url-pattern>
</servlet-mapping>
</web-app>
Login.html
<html>
<head>
<title>SCWCD Example</title>
</head>
<body><h3>Enter your ID and Password: </h3><p>
<form action="servlet/LoginServlet" method="POST">
User ID: <input type="text" name="id"><br></br></input>
Password: <input type="password" name="pwd"><br></br></input>
<input type="submit" value="Show statement"></input>
</form></p>
</body>
</html>