6

Sorry for beginner's questions but I wonder how can I set the root servlet in Tomcat 6? For example I want to access my servlet on

localhost:8080, not on

localhost:8080/myservlet

Thanks!

jmj
  • 237,923
  • 42
  • 401
  • 438
gennad
  • 5,335
  • 12
  • 44
  • 47

4 Answers4

7

deploy an web app with context root /
and set servlet-mapping in web.xml as

<servlet-mapping>
  ..
  <url-pattern>/</url-pattern>
</servlet-mapping>  
jmj
  • 237,923
  • 42
  • 401
  • 438
3

This is an old thread, but Jigar Joshi's answer wasn't working for me on Tomcat 8.0 and Servlet 3.1. So I used the following mapping in web.xml.

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

In section 12.2, Servlet 3.0 specification states that:

The empty string ("") is a special UR L pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’ / ’ and the servlet path and context path is empty string (““).

Steve
  • 889
  • 1
  • 12
  • 26
0

I did the following in my web.xml. I mapped servlet as index.html.

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>myservlet</welcome-file>
</welcome-file-list>

and then the servlet itself:

<servlet>
<description></description>
<display-name>myservlet</display-name>
<servlet-name>myservlet</servlet-name>
<servlet-class>myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
0

From the Tomcat 6 context configuration documentation:

Context elements may be explicitly defined ... (snip) ... in individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.

You must also map the servlet in that application to the root path (/).

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267