在tomcat 6.0中运行servlet程序

Rad*_*dhe 2 java servlets web-applications tomcat6

我如何在tomcat 6.0中运行servlet程序?

Pas*_*ent 7

首先,您需要在Web部署描述符(web.xml文件)中声明您的servlet,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
  version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>HelloWorldExample</servlet-name>
    <servlet-class>cnx.mywebapp.HelloWorldExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorldExample</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

基本上,我们的想法是在servlet元素中声明servlet的完全限定名称,并将其映射到URL中的url模式servlet-mapping(映射是通过唯一的servlet名称完成的)

然后,您需要将整个(servlet .class文件和部署描述符)打包到.war具有已定义结构的Web应用程序存档(具有扩展名)中:

mywebapp
|-- WEB-INF
|   |-- classes (java classes, including your servlet, go here)
|   |-- lib     (jar dependencies go here)
|   `-- web.xml (this is the deployment descriptor) 
`-- index.jsp

最后,在.cat webapps目录中部署(复制).war .要访问servlet:

http://localhost:8080/mywebapp/hello
           A       B     C       D
Run Code Online (Sandbox Code Playgroud)

哪里:

  • A是Tomcat运行的主机名(这里是本地机器)
  • B是Tomcat正在侦听的端口(默认为8080)
  • C是访问webapp的上下文路径(默认情况下为war的名称)
  • D是在web.xml中声明的用于调用servlet的模式