Hey Guys.....I am new in web development and this was supposed to be my first Servlet but I am getting

java.lang.ClassNotFoundException: servlets.HelloWorldServlet

Now in my Apache tomcat folder C:\apache-tomcat-6.0.16\webapps\ServletExamples I have the WEB-INF folder.....which has the web.xml....

Web.xml is something like this...

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<servlet>
		<servlet-name>HelloWorldServlet</servlet-name>
		<servlet-class>servlets.HelloWorldServlet</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>LoginServlet</servlet-name>
		<servlet-class>servlets.LoginServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>HelloWorldServlet</servlet-name>
		<url-pattern>/HelloWorldServlet</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>LoginServlet</servlet-name>
		<url-pattern>/LoginServlet</url-pattern>
	</servlet-mapping>
	
</web-app>

The HelloWorldServlet.java is

package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class HelloWorldServlet extends HttpServlet{
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		PrintWriter pw = response.getWriter();
		pw.println("<html>");
		pw.println("<head>");
		pw.println("</head>");
		pw.println("<body>");
		pw.println("<h3>Hello World as a Servlet</h3>");
		pw.println("</body>");
		pw.println("</html>");
	}
}

and this is what I am writing on the url......

http://localhost:8080/ServletExamples/HelloWorldServlet

and I am assuming that the problem is coming in the <servlet-class> in the fully qualified name....

So I decided to make a classes folder in the WEB-INF folder and copy all my .class files and .java files in it.....

after this I am still getting the same error....

java.lang.ClassNotFoundException: servlets.HelloWorldServlet

and when I refresh it again it gives me a different error....which is

type Status report

message Servlet HelloWorldServlet is not available

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

So how can anybody help me out in starting my carrier in the servlets...

Thanks in advance

Regards

ISI

I too am new, but ClassNotFoundException is generally caused by the class file not being in the correct location. HelloWorldServlet class file needs to be in /web-inf/classes/servlets/

hi,there. I have downloaded your code and compiled it. It works. But you have to change package name to "servlets". just click Source Packages and then right click to default package and create a new java package. This packages name needs to be "servlets".

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.