一、创建普通Maven项目
使用IDEA创建普通的Web项目如下图:
创建普通Maven项目
二、创建Web项目
这里创建Web项目为Maven方式。
1. 创建Web项目
创建Web项目
2. 目录结构
添加java目录
添加测试目录
3. Maven插件
<build>
<finalName>webshop</finalName>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<ignorePackaging>true</ignorePackaging>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
4. 测试运行
测试运行
测试运行
5. 添加目录WEB-INF
添加文件:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、Maven配置
<?xml version="1.0" encoding="UTF-8"?>
<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>com.neuedu</groupId>
<artifactId>webapp</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
四、配置Tomcat插件
https://github.com/apache/tomcat-maven-plugin
1. 配置maven的setting.xml
配置 Tomcat 访问权限:
在 conf/setting.xml 文件中的标签 <servers> 添加子标签。通过标签名字,我们知道这主要是为了让 maven 去关联我们的 Tomcat 服务器。
<server>
<id>tomcat8</id>
<username>admin</username>
<password>admin</password>
</server>
2. 配置Tomcat插件
Tomcat8
<pluginRepositories>
<pluginRepository>
<id>tomcat8-plugin</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>3.0-r1756463</version>
<configuration>
<!-- 这里配置端口号和访问路径 -->
<path>/</path>
<url>http://localhost:8080/</url>
<username>tomcat</username>
<password>tomcat</password>
<uriEncoding>UTF-8</uriEncoding>
<!--添加忽略war包检查标签,则可以让tomcat7:run指令正常启动tomcat-->
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
</build>
Tomcat7
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/test</path>
<uriEncoding>UTF-8</uriEncoding>
<ignorePackaging>true</ignorePackaging>
<server>tomcat7</server>
</configuration>
</plugin>
执行命令:
1)Run as → clean install
2)Run as → tomcat7:deploy 注:第1次部署执行
3)Run as → tomcat7:redeploy 注:第2次或以后需要重新发布执行
4)Run as → tomcat7:run 注:部署到 tomcat 中启动
五、Servlet测试代码
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter writer=response.getWriter();
writer.append("<!DOCTYPE html>")
.append("<html><head></head><body>")
.append("测试")
.append("</body></html>");
}
}