I am trying to write a simple servlet program. But getting the error The requested resource (/servletexam/LoginServlet) is not available.
I have created two servlet classes as follows -
LoginServlet
public class LoginServlet extends HttpServlet {
Hashtable<String,String> users = new Hashtable<String,String>();
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String userid = req.getParameter("userid");
String password = req.getParameter("password");
if(userid != null && password != null &&
password.equals(users.get(userid)) ) {
req.setAttribute("userid",userid);
ServletContext ct = getServletContext();
RequestDispatcher rd = ct.getRequestDispatcher("AccountServlet");
rd.forward(req, res);
return;
} else {
RequestDispatcher rd = req.getRequestDispatcher("../login.html");
rd.forward(req, res);
return;
}
}
public void init() {
System.out.println("Initializing login servlet");
users.put("ann", "aaa");
users.put("john", "jjj");
users.put("mark", "mmm");
}
}
AccountServlet
public class AccountServlet extends HttpServlet {
Hashtable<String,String[]> data = new Hashtable<String,String[]>();
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException,ServletException {
String userid = (String)req.getAttribute("userid");
if(userid != null) {
String[] records = data.get(userid);
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Account Status for "+userid+" at the start of previous three months...</h3><p>");
for(int i =0; i<records.length;i++) {
pw.println(records[i]+"<br>");
}
pw.println("</body>");
pw.println("</html>");
} else {
RequestDispatcher rd = req.getRequestDispatcher("../login.html");
rd.forward(req, res);
}
}
public void init() {
data.put("ann", new String[]{ "01/01/2002 : 1000.00","01/02/2002 : 1300.00", "01/03/2002 : 900.00"} );
data.put("john", new String[]{ "01/01/2002 : 4500.00","01/02/2002 : 2100.00", "01/03/2002 : 2600.00"} );
data.put("mark", new String[]{ "01/01/2002 : 7800.00","01/02/2002 : 5200.00", "01/03/2002 : 1900.00"} );
}
}
html file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SCWCD_Example_1_3</title>
</head>
<body>
<h3>Please enter your userid and password to see your account statement:</h3><p>
<form action="LoginServlet" method="POST">
Userid : <input type="text" name="userid"><br><br>
Password : <input type="password" name="password"><br><br>
<input type="submit" value="Show Statement">
</form>
</body>
</html>
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.java.LoginServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AccountServlet</servlet-name>
<servlet-class>com.java.AccountServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
I have created a classes folder and placed the class files there with proper package structure.
The purpose of the proj is a log in screen will appear and when submit button is pressed after providing user id and password, account details of that user will be displayed.
The login page is displayed when I entered following URL in the browser - http://localhost:8080/servletexam/login.html, but when providing user id and password and click submit button getting the error The requested resource (/servletexam/LoginServlet) is not available.
Any help is appreciated, thanks in advance.