目录
1. 需求分析
2. 概要设计
2.1 技术选型
2.2 项目结构设计(为下文的模块结构)
2.3 User类(JavaBean)设计
2.4 UserDao(单例模式)设计
2.5 用户信息列表设计
2.6 添加用户设计
2.7 修改用户设计
2.8 删除用户设计
3. 开发阶段
3.1 环境搭建
3.1.1创建项目
3.1.2 配置tomcat服务器
3.1.3 导入需要的包
3.2 前端代码
3.2.1将 Bootstrap 相关的 css,js 两个文件夹复制到 web 目录下
3.2.2首页 index.jsp
3.2.3添加 addUser.jsp
3.2.4修改 updateUser.jsp
3.3 后端代码
3.3.1在 src 中创建 com.work.user 包,在其中创建以下类
3.3.2 User.java(上文)
3.3.3 UserDao.java(上文)
3.2.4 addServlet.java
3.2.5 updateServlet.java
3.2.6 removeSrevlet.java
3.3配置servlet
完成一个用户信息管理系统(基于 BS 架构),功能如下:
效果图如下:
用户信息列表:
添加用户信息:
删除用户信息:
修改用户信息:
package com.work.user;
public class User {
private int id;
private String grade;
private String xh;
private String name;
private String department;
public User(){
}
public User(int id, String grade, String xh, String name, String department) {
this.id = id;
this.grade = grade;
this.xh = xh;
this.name = name;
this.department = department;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", grade='" + grade + '\'' +
", xh='" + xh + '\'' +
", name='" + name + '\'' +
", department='" + department + '\'' +
'}';
}
}
package com.work.user;
import java.util.HashMap;
import java.util.Map;
public class UserDao {
private static UserDao userDao;
private UserDao(){}
public static synchronized UserDao getInstance(){
if(userDao == null){
userDao = new UserDao();
}
return userDao;
}
public static Map map = new HashMap<>();
static {
map.put(1, new User(1,"计BG192","6454111113","康玉学","曙光大数据学院"));
map.put(2, new User(2,"管理BG202","6412129228","韩梅梅","人文艺术学院"));
map.put(3, new User(3,"自BG192","5111239232","李蕾","电信学院"));
map.put(4, new User(4,"会计BG182","9411118215","秦毅","人文艺术学院"));
map.put(5, new User(5,"大数据BG171","6511239232","杨树林","曙光大数据学院"));
map.put(6, new User(6,"自BG192","6411117356","张三","电信学院"));
}
public static Map getALlUser() {
return map;
}
public static void deleteUser(int id){
map.remove(id);
}
public static void addUser(String grade, String xh, String name, String department){
int indexMax = map.keySet().stream().max((o1,o2) ->o1-o2).get();
int i = indexMax+1;
map.put(i,new User(i, grade, xh, name, department));
}
public static void updateUser(User user){
Integer id = user.getId();
map.put(id, user);
}
}
打开 IDEA(本文章使用的idea版本为最新版),创建一个 Java 项目 ,一直next,创建完成后,右键此项目,新建一个模快,一直next,然后Fininsh,然后右键模块,点击Add Framework Support,勾选WebApplication选项.(上文提到的项目结构为模块的结构)
<%--
Created by IntelliJ IDEA.
User: 康玉学
Date: 2021/04/22 0022
Time: 下午 7:33:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.work.user.UserDao" %>
<%@ page import="com.work.user.User" %>
用户列表
用户信息列表
编号
院系
班级
学号
姓名
操作
<%
for(User user: UserDao.getInstance().map.values()){
%>
<%=user.getId()%>
<%=user.getDepartment()%>
<%=user.getGrade()%>
<%=user.getXh()%>
<%=user.getName()%>
修改
删除
<%}%>
添加联系人
<%--
Created by IntelliJ IDEA.
User: 康玉学
Date: 2021/04/24 0024
Time: 下午 5:48:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加用户
添加用户
<%--
Created by IntelliJ IDEA.
User: 康玉学
Date: 2021/04/24 0024
Time: 下午 5:48:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加用户
添加用户
package com.work.user;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import javax.servlet.jsp.*;
public class addServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
Map pMap = req.getParameterMap();
UserDao.addUser(pMap.get("grade")[0],pMap.get("xh")[0],pMap.get("name")[0],pMap.get("department")[0]);
resp.getWriter().print("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
package com.work.user;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
public class updateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
Map pMap = req.getParameterMap();
User user = new User(Integer.parseInt(pMap.get("id")[0]),pMap.get("grade")[0],
pMap.get("xh")[0],pMap.get("name")[0],pMap.get("department")[0]);
UserDao.updateUser(user);
resp.getWriter().print("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
package com.work.user;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
public class removeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
UserDao.deleteUser(Integer.parseInt(id));
resp.getWriter().print("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
打开web目录下的WEB-INFO下的web.xml
将下面的代码加入
addServlet
com.work.user.addServlet
addServlet
/addServlet
updateServlet
com.work.user.updateServlet
updateServlet
/updateServlet
removeServlet
com.work.user.removeServlet
removeServlet
/removeServlet