Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying multiple servlets to a single Tomcat Server

I'm using Eclipse and can get each of my servlets to work independently (using HTTP Client to test) through Eclipse. But the real work is getting them to work at the same time.

I'm using Tomcat, but have no idea how to run all three servlets at the same time. They are all mapped properly in the web.xml file. How do I deploy these from Eclipse?

like image 775
arunjitsingh Avatar asked Jun 27 '10 13:06

arunjitsingh


1 Answers

Well, simply map all 3 of them in the web.xml and deploy them. Below, a sample web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>com.acme.Servlet1</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Servlet2</servlet-name>
    <servlet-class>com.acme.Servlet2</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Servlet3</servlet-name>
    <servlet-class>com.acme.Servlet3</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/path1/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Servlet2</servlet-name>
    <url-pattern>/path2/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Servlet3</servlet-name>
    <url-pattern>/path3/*</url-pattern>
  </servlet-mapping>
</web-app>

Note that the following URLs (assuming mycontext is the context):

  • http://hostname:port/mycontext/path1/foo
  • http://hostname:port/mycontext/path1/bar?aparam=avalue
  • http://hostname:port/mycontext/path1

Match the pattern <url-pattern>/path1/*</url-pattern> (so you don't need to map them on /path1, /path1/*).

If you are using Eclipse WTP, you can register Tomcat as a Server and deploy your Dynamic Web Project on it from Eclipse (right-click on the project and select Run As > Run on Server).

Outside Eclipse, you'll have to package your application as a .war (the standard format for a webapp) and deploy this war on Tomcat. There are several ways to do this but the most straightforward way is to copy the war into $TOMCAT_HOME/webapps.

like image 126
Pascal Thivent Avatar answered Oct 05 '22 22:10

Pascal Thivent