本文记录学习Tomcat搭建Http服务的简单步骤。
1 编写一个简单的Http服务程序SimpleHello.java
- 在eclipse中建立一个Maven工程,产生的pom.xml如下,主要引入了org.apache.tomcat依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.tomcat</groupId>
<artifactId>Tomcat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<name>Tomcat</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
- 以下Java代码实现了接收Http的post请求。在Tomcat中实现了支持Http协议的javax.servlet.http.HttpServlet类,只需要从HttpServlet类派生出一个子类,在子类中重写一些方法就可以完成相应的功能了。本例中是完成get请求,所以重写doGet方法即可。在doGet方法中,参数HttpServletRequest req中保存了请求的消息行,消息头,消息体。对SimpleHello中的doGet方法中的req的参数进行解析,进一步完成业务逻辑,本例中只是向客服端的浏览器写回一句Hello Word。
package org.tomcat;
import javax.servlet.ServletException;
import java.io.*;
import javax.servlet.http.*;
public class SimpleHello extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
PrintWriter out = resp.getWriter();
out.println("Hello Word");
out.close();
}
}
- 编译:由于建立了maven工程,所以可以使用mvn进行编译,在过程的根文件夹下可以看到有个pom.xml文件,执行mvn clean package即可编译完成,在target/classes下能找到编译好的SimpleHello.class文件,待用。
2 部署
- 部署信息简介
通常写好的应用程序会部署在Tomcat的安装目录的webapps下面。在本例中现在webapps下建立一个文件夹test作为该Web应用程序的根目录,在Servlet中定义了Web应用程序的层次结构如下
目录 | 描述 |
---|---|
/test | Web应用程序根目录,此Web应用程序的所有文件都存储在这个根目录下 |
/test/WEB-INF | 存放Web应用程序的部署描述文件web.xml |
/test/WEB-INF/classes | 存放Servlet和其它游泳的类文件 |
/test/WEB-INF/lib | 存放应用程序所依赖的其它jar文件,这些jar文件中可以包含Servlet和其它有用的类文件 |
/test/WEB-INF/web.xml | web.xml中包含了Web应用程序的配置和部署信息 |
- 本例中用到的web.xml文件如下。 标签用于声明Servlet,子标签用于定义Servlet的名字,这个名字必须唯一,且不能为空,定义Servlet类完整的限定名,如果有包名,同时需要给出包名;定义Servlet和URL之间的映射关系,他的子元素指定的名字必须和标签中指定的名字相同,定义对应与Servlet的URL路径,该路径是相对于Web应用程序的路径。经过这样配置就可以用http://localhost:8080/test/hello来访问SimpleHello这个Servlet了
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>SimpleHello</servlet-class>
</servlet>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>WelcomeYou</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
- 将SimpleHello.class拷贝到WEB-INF/classes下
- 在Tomcat的根目录下的bin文件中执行sh ./startup.sh。该服务即可启动,从浏览器用http://localhost:8080/test/hello就可以访问了。