Web项目环境搭建

实现一个简单的注册功能

项目整体结构:

1、使用maven创建web项目

2、添加依赖和插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- MyBatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>

<!-- servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- 监听器依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

将spring-mybatis整合的代码复制到src下

3、编写页面

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<h1>员工注册</h1>
<form action="registerServlet" method="post">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="0">女</td>
</tr>
<tr>
<td>电子邮件</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>部门编号</td>
<td><input type="radio" name="deptId" value="1">1
<input type="radio" name="deptId" value="2">2</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="注册提交">
</td>
</tr>
</table>
</form>
</body>
</html>

registerSuccess.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册成功页面</title>
</head>
<body>
<p>注册成功</p>
</body>
</html>

4、编写servlet

1.在web.xml中注册servlet和listener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--注册servlet-->
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.zyz.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/registerServlet</url-pattern>
</servlet-mapping>

<!--注册监听器ContextLoaderListener 自动创建容器对象,并放入全局作用域ServletContext中
用户发起请求时可以直接使用对象,不需要再创建容器-->
<!--自定义配置文件路径 创建容器对象时需要读取配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2、编写servlet程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String deptId = request.getParameter("deptId");

WebApplicationContext webApplicationContext = null;
/*// 从ServletContext中获取容器对象
String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
Object attribute = getServletContext().getAttribute(key);
if(attribute!=null){
webApplicationContext = (WebApplicationContext) attribute;
}*/

// 使用框架提供的工具类直接从ServletContext中获取创建好容器对象
ServletContext servletContext = getServletContext();
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

// 获取service
EmployeeService employeeService = (EmployeeService) webApplicationContext.getBean("employeeService");
Employee employee = new Employee(null,name,gender,email,Integer.parseInt(deptId));
employeeService.addEmp(employee);

// 注册成功 页面跳转
request.getRequestDispatcher("/registerSuccess.jsp").forward(request,response);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}