Dennis Heimbigner
Dennis Heimbigner

Reputation: 173

Tomcat 6 not recognizing <url-mapping>

I have what I think is the simplest possible hello world example (see below). But when asking for "http://localhost:8080/hello" thru firefox, it gives me the "The requested resource (/hello/) is not available" error.

Environment: newly installed tomcat 6.0.32 on Windows 7.

Other information: 1. None of the "similar questions" provides any clues.

  1. From experimentation, it appears that tomcat is not doing the mapping from localhost:8080/hello to my servlet.

  2. I set "<load-on-startup>" which showed me that the servlet's init entry was being called, but doGet() is never called.

  3. The log files show no errors.

  4. I have tried both starting tomcat with the hello directory already in webapps, with hello.war in webapps, and deploying using the manager application. All act the same way.

Some possibilities I have considered:

  1. According to the documentation, I should not need to use a context.xml file, and my experiments with a context.xml produced the same resource not found error.

  2. localhost:8080/hello should instead be localhost:8080/.../hello, but if so, then what is the ... supposed to be?

  3. Trailing / (e.g. /hello versus /hello/). I changed the url-pattern to "/hello/*", but it fails the same way.

I assume the problem is something simple, but I cannot see it.

[Added 8/8/2011] The answers about using context.xml were correct; thanks. In looking around, it appears that an alternate way to achieve the same effect is to put this into my web.xml file.

<context-param>
<param-name>ContextPath</param-name>
<param-value>/dts</param-value>
</context-param>

web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

HelloServlet.java: package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {

  public void init()
  {
    System.out.println("\nHelloServlet.init");
  }

  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    System.out.println("\nHelloServlet.doGet");
    PrintWriter out = res.getWriter();
    out.println("Hello, world!");
    out.close();
  }
}

Upvotes: 2

Views: 4230

Answers (4)

MetroidFan2002
MetroidFan2002

Reputation: 29868

With a Java Servlet Application (part of, but not the total sum of Java EE - Java Enterprise Edition), applications have servlets under what is called a "context path". This "context path" has to be specified in order to map any request to the application.

Apache Tomcat makes this context path pretty easy to configure, either via server.xml (not recommended) or individual context files (recommended). Both ways specify where to find your web application directory (an unpacked web application archive, or WAR file) and where to place it on the server at a context path.

As Vlad has already said, if you deploy your war file into Tomcat's webapps directory and have automatic installation on (I believe it is on by default), Tomcat will unpack the .war into a directory under that location and use the war's name as its context path. His example war file is named "helloapp.war", so, with the default settings, it would receive any request to http://localhost:8080/helloapp because its context path becomes helloapp.

Of course, once the request is sent to the context path, something needs to match against it. That's where the web.xml comes in to play. While it is possible to use the root as a matcher (every request to the context path gets handled by the same process), typically a pattern is used (such as *.do, *.action, etc), so that individual requests to the helloapp are easily distinguishable (it's easier to read and debug http://localhost:8080/helloapp/login.action and http://localhost:8080/helloapp/doSomethingElse.action than both being recognized via some parameters and the same path of http://localhost:8080/helloapp in my opinion)

So, the context path gets to your application, then your application has to do a lookup on the web.xml to see where to send the actual request. In your example, if your webapp was deployed at the context path of helloapp, to access it with the proper mapping, you would simply append /hello, so the request becomes http://localhost:8080/helloapp/hello

Upvotes: 4

user187702
user187702

Reputation:

If you go to /hello/hello I bet you'll see your app. If you're using tomcat, use context.xml. You may not need to use it for a webapp to work, but if you deploy under tomcat, things just work more coherently when you have a context.xml file.

In /yourtomcatinstall/webapps/hello/META-INF/ create a context.xml file with this information"

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/hello">
</Context>

And change the url mapping of your servlet in web.xml to / and/or /* you can have more than one url mapping for a servlet.

Upvotes: 0

Vlad
Vlad

Reputation: 10780

You are deploying your hello servlet in a webapp. Assuming the webapp is in a folder helloapp or in an archive helloapp.war in Tomcat's webapps directory then your sevlet would be accessible at http://localhost:8080/helloapp/hello

Upvotes: 2

Femi
Femi

Reputation: 64690

You will either need to rename the package to ROOT.war (or the ROOT directory) or modify the ROOT.xml context.xml file to point to the hello folder.

Upvotes: 1

Related Questions