views:

41

answers:

2

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?

+3  A: 

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"&gt;
  <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):

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.

Pascal Thivent
That's awesome, and very prompt! I haven't even left the website yet!Anyway.. thanks.. I was able to run them all from Eclipse, but will deploy them as a web-archive for Tomcat.Thanks again!
arunjitsingh
@user377392 You're welcome.
Pascal Thivent
A: 

if you want the servlets to be started without any request being sent to the server add
<load-on-startup>N<load-on-startup/>

where N=1,2,3.... in the <servlet> mapping

frictionlesspulley