1. 创建新的项目,选择Maven,选择org.apache.maven.archetypes:maven-archetype-webapp
2. 配置Maven坐标
3. 点击next
4. 点击Finish
5. 点击右下角的Enable Auto-lmport
6. 在main文件下新建java、resources,并将目录标记成对应的类型,在src下新建test文件夹,在test文件夹下java、resources,并将目录标记成对应的类型
7. 添加由Tomcat提供的jsp和Servlet依赖:
在pom.xml的dependencies标签中添加以下内容即可
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope>
8. 将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"> <display-name>Archetype Created Web Application</display-name> </web-app>
9. 创建Servlet
package io.zhangjia.maven; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/test") public class servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("name",req.getParameter("name")); req.getRequestDispatcher("/WEB-INF/views/test.jsp").forward(req,resp); } }
10. test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <h1>你的名字:${requestScope.name}</h1> </body> </html>
11. 配置TOMCAT运行项目,选择第二个
12. 打包
点击Maven中的packge可以将该项目进行打包
将打包好的jar文件复制到Tomcat的webapps文件夹下
将IDEA的Tomcat停止,然后打开bin目录下的startup.bat,Tomcat会自动将打包好的jar包解压缩,
此时再访问http://localhost:8888/ssm2/test?id=1,即可正常获取数据
请登录之后再进行评论