1. 选择菜单 File ->new -> Other
选择Maven下的Maven Project, next
2.选中下图红框部分的复选框(跳过骨架),next
3. 定义工程坐标
注意工程的打包方式war
4. 手动添加web.xml
(1)在src /main/webapp 目录下手动创建WEB-INF目录
(2)WEB-INF目录下创建web.xml文件,并把其他项目里面的web.xml内容复制到这个上面
5. 添加插件
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>3.1</version>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8080</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
点击工程右键 Maven -> Update Project
6. 在 pom.xml 中添加 servlet-api
因为我们创建的是 war 工程 ,需要 response 和 request 等对象,而这些对象是在servlet-api 中的,所以我们需要添加 servlet-api ,才能保证工程正常编译。注意:必须指定 scope 为 provided , 这是设置依赖范围,我们后边的章节会详细讲解。如果设置则启动会报错。
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
添加后更新工程
7. 创建 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setAttribute("xxx", "rqeust_XXX123");
session.setAttribute("xxx", "session_XXX123");
pageContext.setAttribute("xxx", "pagecontext_XXX123");
application.setAttribute("xxx", "application_XXX123");
%>
${requestScope.xxx}<br/>
${sessionScope.xxx}<br/>
${pageScope.xxx}<br/>
${applicationScope.xxx}
</body>
</html>
部署运行测试 执行命令
tomcat7:run
用浏览器测试,地址: http://localhost:8080/index.jsp