1 先建 父项目 MVC ,父项目 的pom.xml meavn 是这样
<?xml version="1.0" encoding="UTF-8"?> <!--suppress MavenModelInspection --> <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.max</groupId> <artifactId>MVC</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>MVC-Spring-demo02</module> <module>MVC-Spring-annotation</module> <module>MVC-Spring-demo03</module> </modules> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.20</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
2 删除 父项目 src 目录
3 创建 一个module 名称 MVC-Servlet,为测试一下servlet, meavn pom.xml 文件这样
<?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"> <parent> <artifactId>MVC</artifactId> <groupId>com.max</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>MVC-Servlet</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> 3.1 点在 module 右键,选择 add framework, 选择 web 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"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.max.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
创建java com.max.servlet.HelloServlet 继承 HttpServlet 就是 Servlet
重新方法 doGet doPost
package com.max.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method= req.getParameter("method"); System.out.println(method); if (method.equals("add")){ req.getSession().setAttribute("msg","执行ADD方法"); System.out.println("执行doGet方法"); } else if (method.equals("delete")){ req.getSession().setAttribute("msg","执行delete方法"); } else{ req.getSession().setAttribute("msg","执行"+method+"方法"); } req.getRequestDispatcher("WEB-INF/jsp/hello.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行doPost方法"); doGet(req, resp); } }
在WEB-INF 创建 jsp/hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hello</title> </head> <body> </body> <h3>${msg}</h3> </html>
目录如图
部署tomcat 启动,WEB-INF的下目录不能执行访问的。
访问 http://localhost:8080/hello?method=addlocalhosthttp://localhost:8080/
如果日志乱码 在tomcat 配置 -Dfile.encoding=UTF-8