我正在使用 Eclipse,并且可以让我的每个 servlet 通过 Eclipse 独立工作(使用 HTTP Client 进行测试)。但真正的工作是让它们同时工作。
我正在使用 Tomcat,但不知道如何同时运行所有三个 servlet。它们都在 web.xml 文件中正确映射。我如何从 Eclipse 部署它们?
最佳答案
好吧,只需将所有 3 个都映射到 web.xml
中即可。并部署它们。下面是一个示例 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>
请注意以下 URL(假设 mycontext
是上下文):
-
http://hostname:port/mycontext/path1/foo
-
http://hostname:port/mycontext/path1/bar?aparam=avalue
-
http://hostname:port/mycontext/path1
匹配模式<url-pattern>/path1/*</url-pattern>
(因此您不需要将它们映射到/path1、/path1/*)。
如果您使用的是 Eclipse WTP,则可以将 Tomcat 注册为服务器,然后从 Eclipse 将您的动态 Web 项目部署到它上面(右键单击 strong> 并选择 Run As > Run on Server)。
在 Eclipse 之外,您必须将您的应用程序打包为 .war
(webapp 的标准格式)并在 Tomcat 上部署这场 war 。有几种方法可以做到这一点,但最直接的方法是将 war 复制到 $TOMCAT_HOME/webapps
中。 .
关于java - 将多个 servlet 部署到单个 Tomcat 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127516/