我如何在tomcat 6.0中运行servlet程序?
First, you need to declare your servlet in a web deployment descriptor (a web.xml
file) which looks like this:
<?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>
Basically, the idea is to declare the fully qualified name of your servlet in the servlet
element and to map it to an url pattern in the servlet-mapping
(the mapping is done via a unique servlet name)
Then, you need to package the whole (the servlet .class
file and the deployment descriptor) in a web application archive (with a .war
extension) which has a defined structure:
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
Finally, deploy (copy) the .war in the webapps
directory of Tomcat. To access the servlet:
http://localhost:8080/mywebapp/hello
A B C D
Where:
通过构建一个webapp并将其放入webapp根目录,就像在另一个版本的tomcat或任何其他servlet容器中一样。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.