Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying To Run Servlet

843841Jun 26 2004 — edited Sep 3 2004
I'm trying to run a basic servlet but having no luck. The code for web.xml and MyServlet.java are below. I compiled my MyServlet.java file and then started Tomcat (5.0.18). In my browser I then ran the following links and they all gave a similar 404 message (example below)...

http://localhost:8080/MyServlet/MyServlet
http://localhost:8080/MyServlet/servlet/MyServlet
http://localhost:8080/MyServlet/Servlet/MyServlet


404 example message
The requested resource (/MyServlet/MyServlet) is not available.

Some possibilities that I can think of and some questions...

Does this have anything to do w/ the java code not being in a package? I think I tried before to do the same thing but put the code in a package and I still had no luck.

Also, is there a special way to compile a servlet? The book I read said you have to have servlet.jar in your classpath when compiling (or use "-classpath" switch when running javac). I added an environment variable (in win XP Pro) but I guess I have no way to know if it's working. I started a new cmd window (hoping to refresh the environment vars) and then compiled it again. I think I've read in the past that you have to reboot your system though to have new enviornment variables take effect in XP Pro.


C:\jakarta-tomcat-5.0.18\webapps\MyServlet\WEB-INF\classes\MyServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MyServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,
	    			  HttpServletResponse response)
	    			  throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>My 1st servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Finally I wrote a servlet</h1>");
		out.println("</body>");
		out.println("</html>");
	}

	public static void main (String[] args) {

	}
}
C:\jakarta-tomcat-5.0.18\webapps\MyServlet\WEB-INF\web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
  </servlet>
</web-app>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2004
Added on Jun 26 2004
14 comments
433 views