PhantomM
PhantomM

Reputation: 845

Can't get servlets to work in Tomcat 6

I am a Java beginner and trying to make my first example work.

I have installed Tomcat6.0 and using Eclipse on Windows.

I have placed HelloWorld folder in webapps. In WEB-INF have placed classes folder and web.xml.

When I place this as the URL: http://localhost:8080/HelloWorld/HelloWorld

I get the following error:

HTTP Status 404: The requested resource () is not available.

When I try http://localhost:8080 it works fine and gives access to Tomcat's home page and I can execute the example from there.

My web.xml is:

<?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>Hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

My HelloWorld.java is :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

        response.setContentType("text/html");

        PrintWriter pw = response.getWriter();
        pw.println("<html>");
        pw.println("<head><title>Hello World</title></title>");
        pw.println("<body>");
        pw.println("<h1>Hello World</h1>");
        pw.println("</body></html>");
    }
}

Please help. I am stuck on this from two days. EDIT: Solved the problem. Thanks. I added HelloWorld in web.xml and it worked. Thanks for the help.

Upvotes: 2

Views: 2081

Answers (2)

MaVRoSCy
MaVRoSCy

Reputation: 17849

What you need to have for this to work is the following:

1) Create a folder HellowWorld inside $CATALINA_HOME/webapps directory

2) Create a folder named WEB-INF inside the HellowWorld directory and place in the web.xml exactly as you have given it to us.

3) Place HellowWorld.class (not the .java) inside WEB-INF/classes (exactly as you have given it to us)

4) Then Start your tomcat server that listens on port 8080 (preferable a clean installation).

5) Call http://localhost:8080/HelloWorld/HelloWorld (directly from browser's url or from a form with action=get)

6) Now it should work fine (works on my tomcat7)

If you have anything different in your configuration then that is what causes the problem.

Upvotes: 1

Tarun Gupta
Tarun Gupta

Reputation: 1242

Please put HelloWorld folder outside the webapps folder.

Upvotes: 0

Related Questions