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

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

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

Where are the package statements in those servlets? And where, under WEB_INF/classes are they located, exactly.

the package statements are as follows -

package com.java;

and under WEB-INF/classes, they are located in com/java/ folder.

Well, in your web.xml I see you servlet tags, but not you servlet mappings

<servlet>
        <servlet-name>SomeServletMapping</servlet-name>
        <servlet-class>some.package.SomeClass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SomeServletMapping</servlet-name>
        <url-pattern>/some/page/url</url-pattern><!--does not include application name (i.e. servletexam in your case)-->
    </servlet-mapping>

I tried with the servlet mapping, but still it does not work.
I guess, if we don't specify any mappings, by default it is assumed as .html by container. Please confirm.

hi masijade,
thanks for your suggession,now it is working after adding the servlet mapping. However I tried with the same before also, but it didn't work then. But now when I entered the url http://localhost:8080/servletexam/login.html, instead of rendering the login page, it directly forwared to the loginServlet.
But the flow supposed to be after user put the user name and password and submit the button, only then it will invoke the LoginServlet servlet.
Thanks in advance.

Sorry for intruding on this post, but I have this problem too but I put it in the Java instead of the JSP thread (this is using Jakarta-tomcat 5.5.9 and the teacher won't allow me to update it):


Thanks for helping me out with my coding last time. Now I have an error with a class not found for my servlet.

In the log, the following shows up:

SEVERE: Error loading WebappClassLoader

  delegate: false

  repositories:

    /WEB-INF/classes/

----------> Parent Classloader:

org.apache.catalina.loader.StandardClassLoader@1c0e45a

 cart.CartServlet
java.lang.ClassNotFoundException: cart.CartServlet

I've compiled all of the files properly but the fact that it can't find the class disturbs me. It's in the WEB-INF/classes folder along with all of the rest of the classes what could it not find.

When I refresh the page I get this code:

type Status report

message Servlet CartServlet is not available

description The requested resource (Servlet CartServlet is not available) is not available.

Here's a screen cap of the class hierarchy:

[IMG]http://img607.imageshack.us/img607/395/sessioncart.png[/IMG]

Uploaded with ImageShack.us

Thanks once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.