Mapping a specific servlet to be the default servlet in Tomcat
This should be useful to you.
From the Java™ Servlet Specification Version 3.1 (JSR 340)
Chapter 12. Mapping Requests to Servlets
12.2 Specification of Mappings
In the Web application deployment descriptor, the following syntax is used to define mappings:
A string beginning with a
/
character and ending with a/*
suffix is used for
path mapping.A string beginning with a
*.
prefix is used as an extension mapping.The empty string
("")
is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the formhttp://host:port/<contextroot>/
.
In this case the path info is/
and the servlet path and context path is
empty string("")
.A string containing only the
/
character indicates the "default" servlet of the
application. In this case the servlet path is the request URI minus the context path
and the path info is null.- All other strings are used for exact matches only.
As an addition, read this nice explanation with short examples from the book Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam (2nd edition) (quote):
The THREE types of
<url-pattern>
elements1) EXACT match
Example:
<url-pattern>/Beer/SelectBeer.do</url-pattern>
- MUST begin with a slash (
/
).- Can have an extension (like .do), but it’s not required.
2) DIRECTORY match
Example:
<url-pattern>/Beer/*</url-pattern>
- MUST begin with a slash (
/
).- Always ends with a slash/asterisk (
/*
).
3) EXTENSION match
Example:
<url-pattern>*.do</url-pattern>
- MUST begin with an asterisk (
*
) (NEVER with a slash).- After the asterisk, it MUST have a dot extension (.do, .jsp, etc.).
IMPORTANT NOTE:
The URL patterns represent logical / virtual structure, i.e. the patterns (paths) specified does not need to exist physically.
UPDATE
If you want, as you say in your comment,
I want host:port to hit my servlet, not the default tomcat servlet
then see the solution here:
How do I make my web application be the Tomcat default application
In other words, what you want is a path without application context, which implies the application context of the Tomcat default application.
Quote from the above link:
In a standard Tomcat installation, you will notice that under the same
directory (CATALINA_BASE)/webapps/, there is a directory called ROOT
(the capitals are important, even under Windows). That is the
residence of the current Tomcat default application, the one that is
called right now when a user calls up
http://myhost.company.com[:port]
. The trick is to put your
application in its place.
Servlet mapping to default / in tomcat
If I understood your question correctly, you would want to override the default servlet?
You could add a context element within the default <Host>
element in tomcat's conf/server.xml
For example:
<Context path="" docBase="app" />
where app is a folder in the webapps directory.
See the documentation here for more information.
It is NOT recommended to place elements directly in the server.xml file. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.
Hope it helps.
What is the default servlet for mapping with undefined servlet
Oracle docs:
Each Web application has a default servlet. This default servlet can be a servlet that you specify, or, if you do not specify a default servlet, WebLogic Server uses an internal servlet called the FileServlet as the default servlet.
You can register any servlet as the default servlet. Writing your own default servlet allows you to use your own logic to decide how to handle a request that falls back to the default servlet.
Setting Up a Default Servlet
Tomcat Specific
Need to make a servlet class default in tomcat
When browser requests http://localhost:8080/InterConnect/
, assuming InterConnect
is the context path, and given that you have declared <welcome-file>HomePageServlet</welcome-file>
, the servlet container will look to see if a servlet mapping exists for /HomePageServlet
.
You don't have a mapping for /HomePageServlet
, only one for /servlet/HomePageServlet
, so the web server returns 404 (Not found).
Try <welcome-file>servlet/HomePageServlet</welcome-file>
to see if that works, or change servlet mapping to /HomePageServlet
.
Default servlet mapping for different deployment environments
There is no way.
You should preferably not explicitly map to the container's default servlet in any way. You're not only tight coupling your webapp to the specific container, but until about one year ago, there was also a huge security hole when doing that in Tomcat and clones (JBoss, WebSphere, etc). It enables the attackers to request files (containing possibly sensitive information) in /WEB-INF
and /META-INF
whenever the default servlet was mapped on a different URL pattern than /
. See also issue 50026, reported by yours truly.
Rather map your front controller servlet on a more specific URL pattern instead of /*
and create and map a global filter on /*
which forwards to either the front controller or continues to the default servlet depending on the current request URI. See for a concrete example also How to access static resources when mapping a global front controller servlet on /*.
Override the default servlet behaviour for URL pattern / in web.xml
Use an empty pattern string to match the context root:
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
Query on hitting specific servlet as default tomcat servlet
If you want to redirect /
to /ent-app
, then you need to configure that. There is no magic provided by the ROOT
web application: you need to configure it yourself to perform the redirect.
You can either write your own servlet/filter that maps to /*
and just redirects everything to /ent-app
, or you can use something like url-rewrite and simply configure it to your liking.
Change servlet mapping depending on Tomcat context
If your application runs in servlet 3.0+ container you could use a ServletContextListener
to dynamically set the default servlet, depending on the context path.
public class MyServletContextListener implements ServletContextListener {
@Override public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
if ("".equals(sc.getContextPath())) {
ServletRegistration.Dynamic dreg = sc.addServlet("pageservlet", new PageServlet());
dreg.addMapping("");
}
}
}
How to set up the root servlet in Tomcat 6?
deploy an web app with context root /
and set servlet-mapping in web.xml as
<servlet-mapping>
..
<url-pattern>/</url-pattern>
</servlet-mapping>
Related Topics
Why People Are So Afraid of Using Clone() (On Collection and Jdk Classes)
Is There a Method for String Conversion to Title Case
Java Fileoutputstream Create File If Not Exists
Adding an Http Header to the Request in a Servlet Filter
How to Capture Global Key Presses in Java
How to Use Yamlpropertiesfactorybean to Load Yaml Files Using Spring Framework 4.1
How to Use "Cd" Command Using Java Runtime
Increase Http Post Maxpostsize in Spring Boot
How to Output Two Columns to the Console in Java
Servlet Seems to Handle Multiple Concurrent Browser Requests Synchronously
Missing Return Statement in a Non-Void Method Compiles
Escaping Special Characters in Java Regular Expressions
How to Make a Deep Copy of Java Arraylist