Tomcat
Tomcat 是由 Apache 开发的一个 Servlet 容器,实现了对 Servlet 和 JSP 的支持,并提供了作为Web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阀等。
工程结构
以idea为例,创建一个JavaWeb工程(网上有很多教程,不重复了),工程结构为这样:
结构pom.xml
因为使用到了Spring/SpringMVC,所以在pom.xml中都有其依赖,内容如下
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava.demo</groupId>
<artifactId>helloweb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>hellowebMaven</name>
<url>http://maven.apache.org</url>
<properties>
<tomcat.embed.version>8.0.28</tomcat.embed.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- Tag libs support for view layer -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<!-- 嵌入式tomcat依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.embed.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.embed.version}</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.embed.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
</project>
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Hello World Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<context:annotation-config/>
<mvc:annotation-driven />
<context:component-scan base-package="com.bxw"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloController
@Controller
@RequestMapping(value = "hello")
public class HelloController {
@RequestMapping(value = "world", method = RequestMethod.GET)
@ResponseBody
public String helloWorld(){
return "hello";
}
}
嵌入式启动方式
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.net.URL;
public class TomcatStarter {
private int port = 8080;
private String contextPath = "helloweb";
public void start() throws Exception {
Tomcat tomcat = new Tomcat();
URL url = getClass().getClassLoader().getResource("spring-servlet.xml");
String pwd = StringUtils.substringBefore(url.getPath(), "/target/classes");
tomcat.setBaseDir(pwd);
tomcat.setPort(port);
StringBuilder webAppBuilder = new StringBuilder();
webAppBuilder.append(pwd).append(File.separator).append("web");
String webapp = webAppBuilder.toString();
tomcat.addWebapp(contextPath, webapp);
tomcat.enableNaming();
tomcat.start();
tomcat.getServer().await();
}
public static void main(String[] args) throws Exception {
TomcatStarter starter = new TomcatStarter();
starter.start();
}
}
执行main方法,启动成功,访问
访问成功idea插件启动方式
- 点击 Run/Debug Configurations > New Tomcat Server > local ,打开 Tomcat 配置页面。
- 点击 Confiure... 按钮,设置 Tomcat 安装路径。
- 点击 Deployment 标签页,设置要启动的应用。* 设置启动应用的端口、JVM 参数、启动浏览器等。
- 成功后,可以访问 [http://localhost:8080/](当然,你也可以在 url 中设置上下文名称)。
网友评论