一、入门程序
① 前期准备
设置maven:window -> preferences -> Maven
-> Installations:add添加自己下载的maven插件,然后选中打上勾;
-> User Settings:配置自己的settings.xml文件;
② 创建Maven项目
点击【Finish】按钮,完成项目的创建
③ web.xml丢失,在src -> main -> webapp下创建WEB-INF目录,并在目录下创建web.xml
④ 修改Maven默认编译级别 - 修改JDK版本
全局编译级别 -> 修改settings.xml
项目级别修改 -> 项目对应的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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.alin</groupId> <artifactId>HelloMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>maven</name> <description>这是我的第一个maven</description> <build> <!-- 插件列表 --> <plugins> <!-- 通过坐标获取插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
修改完成后Alt+F5刷新当前项目
⑤ 在Java Resourcese -> src/main/java下写代码,创建包和Servlet
package cn.alin.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("<h1>Hello Tomcat7:run!!!</h1>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
⑥ pom.xml文件的project标签中添加servlet所需包的坐标
在servlet-api中添加scope标签,值为:proviede
如果不加此标签有很大几率报错,因为tomcat中也有servlet-api的包导致冲突
<!-- 依赖标签 配置项目中依赖的jar包坐标 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- 原生servlet与jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies>
⑦ 运行:发生错误,原因是maven默认使用tomcat6.xx,不支持@WebServle注解
解决方案1:需要去web.xml中配置Servlet
解决方案2:配置高版本tomcat插件
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>HelloMaven</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>cn.alin.web.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app>
tomcat:run -> 运行成功
⑧ 最后测试,启动服务,在地址栏输入请求,显示结果
二、使用 tomcat7 插件运行项目
① 项目结构
② web.xml 与 java测试文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>hellomaven</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
package cn.alin.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/test") public class TestServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("<h1>Hello Tomcat7:run!!!</h1>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
④ 配置高版本tomcat插件 - 另外servlet与jsp依赖也许要配置为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>cn.alin</groupId> <artifactId>HelloMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>maven</name> <description>这是我的第一个maven</description> <!-- 依赖标签 配置项目中依赖的jar包坐标 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.5.32</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>8.5.32</version> <scope>provided</scope> </dependency> </dependencies> <build> <!-- 插件列表 --> <plugins> <!-- JDK编译级别 - JDK1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- tomcat7插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
<!-- build标签下的plugins中配置tomcat7插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin>
⑤tomcat7:run -> 运行测试