
On containers that implement Servlets 2.4, such as Tomcat 5.5 and above, it is possible to list servlets as welcome pages.
This makes it easy to use a servlet as the main web page for your site, instead of redirecting to a servlet from your index.jsp page or using similar work-arounds.
As with most Tomcat application configuration, the changes are made in <appName>/WEB-INF/web.xml.
For this example, we will assume your servlet is in a package called com.examplecompany, and that the fully qualified class name is com.examplecompany.MyCMS.
<!-- Define the com.examplecompany.MyCMS servlet class, giving it the name MyCMS --> <servlet> <servlet-name>MyCMS</servlet-name> <servlet-class>com.examplecompany.MyCMS</servlet-class> </servlet>
<!-- Map the MyCMS name to the URI /CMS (main page for site) --> <servlet-mapping> <servlet-name>MyCMS</servlet-name> <url-pattern>/CMS</url-pattern> </servlet-mapping>
<!-- The main page for the site will be the MyCMS servlet (http://website/CMS) -->
<welcome-file-list>
<welcome-file>CMS</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
When someone accesses your website, the following happens
No URI mapping is defined for any folder except the root folder:
http://yoursite.com/CMS - this will work http://yoursite.com/someFolder/CMS - this won't work
Therefore, if one of the other folders is accessed, one of the other files listed with <welcome-file> elements will be loaded, if it exists. Those files are index.html, index.htm, index.jsp. This is the standard behaviour of a fresh Tomcat installation, which is why we have included these above.
<welcome-file>servlet/MyServlet</welcome-file>
Here is a full example web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- ========================================================== -->
<!-- General Info -->
<!-- ========================================================== -->
<display-name>My App</display-name>
<description>
My Application
</description>
<!-- ========================================================== -->
<!-- CMS Servlet -->
<!-- ========================================================== -->
<!-- Define the com.examplecompany.MyCMS servlet class, giving it the name MyCMS -->
<servlet>
<servlet-name>MyCMS</servlet-name>
<servlet-class>com.examplecompany.MyCMS</servlet-class>
</servlet>
<!-- Map the MyCMS name to the URI /CMS (main page for site) -->
<servlet-mapping>
<servlet-name>MyCMS</servlet-name>
<url-pattern>/CMS</url-pattern>
</servlet-mapping>
<!-- ========================================================== -->
<!-- Welcome Files -->
<!-- ========================================================== -->
<!-- The main page for the site will be the MyCMS servlet (http://website/CMS) -->
<!-- No mapping is defined for other folders (http://website/someFolder/CMS), -->
<!-- so one of the other files will be displayed (index.html, index.htm, index.jsp) -->
<welcome-file-list>
<welcome-file>CMS</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
If you are using servlets 2.3 or lower, you can point to a JSP file instead, and make that file redirect to your servlet. eg:
File redirect.jsp (where /CMS is mapped to your servlet, as in the above examples)
<%
response.sendRedirect("http://yoursite.com/CMS");
%>
Welcome-file list in web.xml:
<!-- The main page for the site will be redirect.jsp servlet, which redirects -->
<!-- to the MyCMS servlet (http://yoursite.com/CMS) -->
<!-- No mapping is defined for other folders (http://website/someFolder/CMS), -->
<!-- so one of the other files will be displayed (index.html, index.htm, index.jsp) -->
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
| VotePlugin.properties | ![]() |
95 bytes |