JavaWeb用户信息管理系统(附全部代码)
1、JavaEE项目的三层框架2 、本项目的目录文件(java文件)3、创建用户资源的数据表
4、编写JdbcUtils工具类(实现代码的反复利用)5、编写Dao耐久层(用来和数据库交互)
5.1、导入DBUtils 的 jar包5.2、编写BaseDao5.3、编写UserDao5.4、UserDao接口实现类UserDaoImpl
7、编写Service(处置业务逻辑,调用Dao耐久层操作数据库)
7.1、UserService接口7.2、UserService接口实现类UserServiceImpl
8、处置登录业务
8.1、业务逻辑(下同)8.2、登录页面Login.jsp8.3、Login_css.css(登录页面的美化文件)8.4、编写LoginServlet8.5、注册servlet8.6、登入胜利页面(显示所有用户)8.7、登入胜利页面的login_success_css.css文件8.8、登入胜利后页面数据恳求(下同)8.9、编写Userservlet
9、添加用户
9.1、编写AddServlet9.2、添加用户页面
10、修改用户
10.1、修改信息的显示页面10.2、编写UpdateServlet
11、删除用户
12、整体展示
主要实现根本用户信息的增删改功能,用户的信息显示在主页面。
lib中的包必需全部导入
1、JavaEE项目的三层框架
分层的目的是为理解耦。降低代码之间的耦合度,方便项目后期的维护和晋级。
2 、本项目的目录文件(java文件)
JavaBean类(com.example.bean) | Web层(com.example.servlet) | Dao耐久层(com.example.dao)(com.example.dao.impl) | service层(com.example.sercice)(com.example.sercice.impl) | Utils类(com.example.utils) | User | AddServlet LoginServlet | BaseDao | UserService | JdbcUtils | | DeleteServlet UpdateServlet | UserDao | UserServiceImpl | | UserServlet OneUserServlet | UserDaoImpl | test测试类放在idea生成的test包下。
3、创建用户资源的数据表
数据表根据自己的需求自己创建,下面是该项目用到的数据库字段。
id 用户编号 intusername 用户名 varcharpassword 密码 varcharname 真实姓名 varcharemail 邮箱 varchartelephone 手机号 varchar
3.1、编写数据库表对应的JavaBean对象
- packagecom.example.bean;publicclassUser{privateInteger id;privateString username;privateString password;privateString name;privateString email;privateString telephone;publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetUserName(){return username;}publicvoidsetUserName(String username){this.username = username;}publicStringgetPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicStringgetEmail(){return email;}publicvoidsetEmail(String email){this.email = email;}publicStringgetTelephone(){return telephone;}publicvoidsetTelephone(String telephone){this.telephone = telephone;}publicUser(Integer id,String userName,String password,String name,String email,String telephone){this.id = id;this.username = userName;this.password = password;this.name = name;this.email = email;this.telephone = telephone;}publicUser(){}@OverridepublicStringtoString(){return"User{"+"id="+ id +", useName='"+ username +'\''+", password='"+ password +'\''+", name='"+ name +'\''+", email='"+ email +'\''+", telephone='"+ telephone +'\''+'}';}}
复制代码 在如下包下面建立 jdbc.properties设置配置文件
- //详细内容
- username=root
- password=123abc
- url=jdbc:mysql://localhost:3306/*** //***为你的数据表所在数据库名称
- driverClassName=com.mysql.jdbc.Driver
- initialSize=5
- maxActive=10 //最多同时获取连接的数量
复制代码 4、编写JdbcUtils工具类(实现代码的反复利用)
读取 jdbc.properties 属性配置文件从流中加载数据创建数据库连接池
- packagecom.example.utils;importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.pool.DruidDataSourceFactory;importjava.io.InputStream;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.Properties;/**
- * @program: ManageSystem
- * @description: 管理数据库连接池的工具类
- * @author: Mr.Zeng
- * @create: 2021-05-26 12:13
- **/publicclassJdbcUtils{privatestaticDruidDataSource dataSource;static{try{Properties properties =newProperties();// 读取jdbc.properties配置文件InputStream inputStream =JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");//从流中加载数据
- properties.load(inputStream);//创建 数据库连接 池
- dataSource =(DruidDataSource)DruidDataSourceFactory.createDataSource(properties);}catch(Exception e){
- e.printStackTrace();}}/**
- * @Description: 获取数据库的连接 返回null则连接失败
- * @Param: []
- * @return: java.sql.Connection
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */publicstaticConnectiongetConnection(){Connection connection =null;try{
- connection = dataSource.getConnection();}catch(SQLException throwables){
- throwables.printStackTrace();}return connection;}/**
- * @Description: 放回数据库连接池
- * @Param: [connection]
- * @return: void
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */publicstaticvoidclose(Connection connection){if(connection !=null){try{
- connection.close();}catch(SQLException throwables){
- throwables.printStackTrace();}}}}
复制代码 值得注意的是,在外面获取一个连接后,最后记得关闭连接放回连接池,在这里可以在test包下建立一个测试JdbcUtils的测试类。
Tips:
可能出错的原因:
数据库是否对应
没有导入相应的jar包 (druid) 可以到网页搜索下载,非常方便。
5、编写Dao耐久层(用来和数据库交互)
5.1、导入DBUtils 的 jar包
commons-dbutils-1.7.jar ,我这里用的是1.7的版本。
5.2、编写BaseDao
- packagecom.example.dao;importcom.example.utils.JdbcUtils;importorg.apache.commons.dbutils.QueryRunner;importorg.apache.commons.dbutils.handlers.BeanHandler;importorg.apache.commons.dbutils.handlers.BeanListHandler;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.List;/**
- * @program: ManageSystem
- * @description: 使用DBUtils操作数据库
- * @author: Mr.Zeng
- * @create: 2021-05-26 12:59
- **/publicabstractclassBaseDao{//使用DBUtils操作数据库privateQueryRunner queryRunner =newQueryRunner();/**
- * @Description: update()方法用来执行:Insert\Update\Delete语句
- * @Param: []
- * @return: int 返回-1表示执行失败,返回其他表示影响的行数
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */publicintupdate(String sql,Object...args){Connection connection =JdbcUtils.getConnection();try{return queryRunner.update(connection,sql,args);}catch(SQLException throwables){
- throwables.printStackTrace();}finally{JdbcUtils.close(connection);}return-1;}/**
- * @Description: 查询返回一个javaBean的sql语句
- * @Param: [type 返回的对象类型, sql 执行的sql语句, args sql对应的参数值]
- * @return: T 返回类型的泛型
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */public<T>TqueryForOne(Class<T> type,String sql,Object...args){Connection connection =JdbcUtils.getConnection();try{return queryRunner.query(connection,sql,newBeanHandler<T>(type),args);}catch(SQLException throwables){
- throwables.printStackTrace();}finally{JdbcUtils.close(connection);}returnnull;}/**
- * @Description: 查询返回多个javaBean的sql语句
- * @Param: [type 返回的对象类型, sql 执行的sql语句, args sql对应的参数值]
- * @return: T 返回类型的泛型
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */public<T>List<T>queryForList(Class<T> type,String sql,Object...args){Connection connection =JdbcUtils.getConnection();try{return queryRunner.query(connection,sql,newBeanListHandler<T>(type),args);}catch(SQLException throwables){
- throwables.printStackTrace();}finally{JdbcUtils.close(connection);}returnnull;}}
复制代码 5.3、编写UserDao
- packagecom.example.dao;importcom.example.bean.User;importjava.util.List;publicinterfaceUserDao{/**
- * @Description: 根据用户名和密码查询用户信息,增删查改,返回全部用户的功能
- * @Param: [username 用户名 , password 密码]
- * @return: com.example.bean.User
- * @Author: Mr.Zeng
- * @Date: 2021/5/26
- */UserqueryUserByUsernameAndPassword(String username,String password);intadd(User user);intdeleteById(Integer id);intupdate(User user);UserqueryById(Integer id);List<User>queryUsers();}
复制代码 5.4、UserDao接口实现类UserDaoImpl
- packagecom.example.dao.impl;importcom.example.bean.User;importcom.example.dao.BaseDao;importcom.example.dao.UserDao;importjava.util.List;/**
- * @program: ManageSystem
- * @description:
- * @author: Mr.Zeng
- * @create: 2021-05-26 13:19
- **/publicclassUserDaoImplextendsBaseDaoimplementsUserDao{@OverridepublicUserqueryUserByUsernameAndPassword(String username,String password){String sql ="select * from user where username = ? and password = ?";returnqueryForOne(User.class, sql, username, password);}@Overridepublicintadd(User user){String sql ="insert into user(username,password,name,email,telephone) values (?,?,?,?,?)";returnupdate(sql, user.getUserName(), user.getPassword(), user.getName(),
- user.getEmail(), user.getTelephone());}@OverridepublicintdeleteById(Integer id){String sql ="delete from user where id = ?";returnupdate(sql, id);}@Overridepublicintupdate(User user){String sql ="update user set username=?,password=?,name=?,email=?,telephone=? where id = ?";returnupdate(sql, user.getUserName(), user.getPassword(), user.getName(),
- user.getEmail(), user.getTelephone(), user.getId());}@OverridepublicUserqueryById(Integer id){String sql ="select * from user where id = ?";returnqueryForOne(User.class, sql, id);}@OverridepublicList<User>queryUsers(){String sql ="select * from user";returnqueryForList(User.class, sql);}}
复制代码 7、编写Service(处置业务逻辑,调用Dao耐久层操作数据库)
7.1、UserService接口
- packagecom.example.service.impl;importcom.example.bean.User;importjava.util.List;publicinterfaceUserService{Userlogin(User user);voidadd(User user);voiddeleteById(Integer id);voidupdate(User user);UserqueryById(Integer id);List<User>queryUsers();}
复制代码 7.2、UserService接口实现类UserServiceImpl
- packagecom.example.service;importcom.example.bean.User;importcom.example.dao.UserDao;importcom.example.dao.impl.UserDaoImpl;importcom.example.service.impl.UserService;importjava.util.List;/**
- * @program: ManageSystem
- * @description: 处置登入业务
- * @author: Mr.Zeng
- * @create: 2021-05-26 14:31
- **/publicclassUserServiceImplimplementsUserService{privatefinalUserDao userDao =newUserDaoImpl();publicUserlogin(User user){return userDao.queryUserByUsernameAndPassword(user.getUserName(), user.getPassword());}@Overridepublicvoidadd(User user){
- userDao.add(user);}@OverridepublicvoiddeleteById(Integer id){
- userDao.deleteById(id);}@Overridepublicvoidupdate(User user){
- userDao.update(user);}@OverridepublicUserqueryById(Integer id){return userDao.queryById(id);}@OverridepublicList<User>queryUsers(){return userDao.queryUsers();}}
复制代码 8、处置登录业务
8.1、业务逻辑(下同)
8.2、登录页面Login.jsp
- <%--
- Created by IntelliJ IDEA.
- User: zengxiong
- Date: 2021/5/26
- Time: 0:18
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>用户登入页面</title>
- <link type="text/css" rel="stylesheet" href="Login_css.css">
- </head>
- <body>
- <div style="width:400px; height:400px;">
- <img src="2.jpg" width="394" height="444" style="margin-left:166px; margin-top:104px;" alt="图片无法显示">
- </div>
- <div id="login">
- <h1>用户管理系统</h1>
- <form action="LoginServlet" method="post">
- <input type="hidden" name="action" value="login">
- <label>
- <input type="text" required="required" placeholder="用户名" name="username">
- </label>
- <label>
- <input type="password" required="required" placeholder="密码" name="password">
- </label>
- <button class="but" type="submit">登录</button>
- </form>
- </div>
- </body>
- </html>
复制代码 8.3、Login_css.css(登录页面的美化文件)
- html{width: 100%;height: 100%;overflow: hidden;}body{width: 100%;height: 100%;font-family:'Open Sans',sans-serif;margin: 0;background-image:url("img.png");}#login{position: absolute;top: 50%;left:50%;margin: -150px 0 0 147px;width: 300px;height: 300px;}#login h1{color: #fff;text-shadow:0 0 10px;letter-spacing: 1px;text-align: center;}h1{font-size: 2em;margin: 0.67em 0;}input{width: 300px;height: 40px;margin-bottom: 10px;outline: none;padding: 10px;font-size: 13px;color: #fff;text-shadow:1px 1px 1px;border-top: 1px solid #312E3D;border-left: 1px solid #312E3D;border-right: 1px solid #312E3D;border-bottom: 1px solid #56536A;border-radius: 4px;background-color: #2D2D3F;}.but{width: 300px;min-height: 20px;display: block;background-color: #4a77d4;border: 1px solid #3762bc;color: #fff;padding: 9px 14px;font-size: 15px;line-height: normal;border-radius: 5px;margin: 0;}
复制代码 8.4、编写LoginServlet
- packagecom.example.web;importcom.example.bean.User;importcom.example.service.UserServiceImpl;importcom.example.service.impl.UserService;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/**
- * @program: ManageSystem
- * @description: 用户登入功能实现
- * @author: Mr.Zeng
- * @create: 2021-05-26 14:47
- **/@WebServlet(urlPatterns ="/LoginServlet")publicclassLoginServletextendsHttpServlet{privatefinalUserService userService =newUserServiceImpl();@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doPost(req, resp);}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{//获取恳求参数String username = req.getParameter("username");String password = req.getParameter("password");//调用UserService.login()登录处置业务User loginUser = userService.login(newUser(0, username, password,null,null,null));if(loginUser ==null){//跳回到Login页面
- req.getRequestDispatcher("/Login.jsp").forward(req, resp);}else{//跳转到UserServlet
- req.getRequestDispatcher("/UserServlet").forward(req, resp);}}}
复制代码 8.5、注册servlet
- 在这里我是直接在类里面注册的:
@WebServlet(urlPatterns = “/LoginServlet”),你也可以在Web.xml里面停止注册一般在一个程序里面只要一个Servlet(利用反射优化),这里我将Servlet全部分开,便于理解但会形成代码冗余,建议自己学习并实现代码优化
8.6、登入胜利页面(显示所有用户)
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%--
- Created by IntelliJ IDEA.
- User: zengxiong
- Date: 2021/5/26
- Time: 15:07
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
- <title>用户管理系统</title>
- <link type="text/css" rel="stylesheet" href="login_success_css.css">
- <script>
- function test1(){
- // document.("表单的name值").action
- // document.("表单的name值").submit
- document.submitted.action="add_list.jsp";
- document.submitted.submit();
- }
- function test2() {
- document.submitted.action = "DeleteServlet";
- document.submitted.submit();
- }
- </script>
- <style>
- button.asd{
- margin-right: -818px;
- background-color: #4a77d4;
- }
- input.abc{
- margin-right: 20px;
- background-color: #89d289;
- }
- input.def{
- background-color: #d91e1e;
- width: 42px;
- height: 25px;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <form name="submitted" method="post">
- <table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">
- <caption></caption>
- <tr>
- <th colspan="7"><button class="asd" οnclick="test1()">添加用户</button></th>
- </tr>
- <tr>
- <th scope="col" abbr="Configurations">编号</th>
- <th scope="col" abbr="Dual 1.8">用户名</th>
- <th scope="col" abbr="Dual 2">密码</th>
- <th scope="col" abbr="Dual 2">姓名</th>
- <th scope="col" abbr="Dual 2">邮箱</th>
- <th scope="col" abbr="Dual 2">电话</th>
- <th scope="col" abbr="Dual 2.5">操作</th>
- </tr>
- <c:forEach items="${requestScope.user}" var="user">
- <tr>
- <th scope="row" abbr="Model" class="spec" name="id">${user.id}</th>
- <td>${user.userName}</td>
- <td>${user.password}</td>
- <td>${user.name}</td>
- <td>${user.email}</td>
- <td>${user.telephone}</td>
- <td><label>
- <%--formaction直接设置行为,并且设置提交的数据值--%>
- <input formaction="OneUserServlet?id=${user.id}" type="submit" class="abc" value="修改">
- </label>
- <label>
- <%--formaction直接设置行为,并且设置提交的数据值--%>
- <input formaction="DeleteServlet?id=${user.id}" type="submit" class="def" value="删除">
- </label></td>
- </tr>
- </c:forEach>
- </table>
- </form>
- </body>
- </html>
复制代码 8.7、登入胜利页面的login_success_css.css文件
- body{font-family:'Open Sans', sans-serif;margin: 0;background-image:url("img.png");width: 100%;height: 100%;border: 1px solid red;text-align: center;}a{color: #c75f3e;}#mytable{width: 1000px;padding: 0;border: 1px solid green;margin: 100px auto 0;height: 300px;}caption{padding: 0 0 5px 0;width: 700px;font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;text-align: right;}th{font: bold 26px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;color: #4f6b72;border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;border-top: 1px solid #C1DAD7;letter-spacing: 2px;text-transform: uppercase;padding: 6px 6px 6px 12px;background: #CAE8EA no-repeat;text-align: center;}td{border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;background: #fff;font-size: 11px;padding: 6px 6px 6px 12px;color: #4f6b72;text-align: center;}td.alt{background: #F5FAFA;color: #797268;}th.spec{border-left: 1px solid #C1DAD7;border-top: 0;background: #fff no-repeat;font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;}th.specalt{border-left: 1px solid #C1DAD7;border-top: 0;background: #f5fafa no-repeat;font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;color: #797268;}html > body td{font-size: 11px;}
复制代码 8.8、登入胜利后页面数据恳求(下同)
8.9、编写Userservlet
- packagecom.example.web;importcom.example.bean.User;importcom.example.service.UserService_1;importcom.example.service.impl.UserService_1Impl;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.util.List;/**
- * @program: ManageResourceSystem
- * @description: 处置业务
- * @author: Mr.Zeng
- * @create: 2021-05-26 23:05
- **/@WebServlet(urlPatterns ="/UserServlet")publicclassUserServletextendsHttpServlet{UserService_1 userService1 =newUserService_1Impl();@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doPost(req, resp);}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{//恳求数据的中文乱码问题
- req.setCharacterEncoding("UTF-8");//客户端网页我们控制为UTF-8//通过UserService查询全部信息List<User> users = userService1.queryUsers();//把全部信息保管到Request域中
- req.setAttribute("user",users);//恳求转发到LoginSuccess.jsp
- req.getRequestDispatcher("/LoginSuccess.jsp").forward(req, resp);}}
复制代码 9、添加用户
9.1、编写AddServlet
- packagecom.example.web;importcom.example.bean.User;importcom.example.service.UserService_1;importcom.example.service.impl.UserService_1Impl;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/**
- * @program: ManageResourceSystem
- * @description: 添加数据的处置业务
- * @author: Mr.Zeng
- * @create: 2021-05-27 08:01
- **/@WebServlet(urlPatterns ="/AddServlet")publicclassAddServletextendsHttpServlet{UserService_1 userService1 =newUserService_1Impl();@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{//获取恳求参数String username = req.getParameter("username");String password = req.getParameter("password");String name = req.getParameter("name");String email = req.getParameter("email");String telephone = req.getParameter("telephone");//调用add保管用户
- userService1.add(newUser(null, username, password, name, email, telephone));//跳转到用户列表页面,此方法有表单反复提交//req.getRequestDispatcher("/UserServlet").forward(req, resp);//无反复添加
- resp.sendRedirect("UserServlet");}}
复制代码 9.2、添加用户页面
- <%--
- Created by IntelliJ IDEA.
- User: zengxiong
- Date: 2021/5/27
- Time: 8:04
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>添加用户</title>
- <link type="text/css" rel="stylesheet" href="login_success_css.css">
- <style>
- form {
- margin-top: 200px;
- margin-left: 300px;
- }
- .but{
- width: 200px;
- min-height: 20px;
- display: block;
- background-color: #4a77d4;
- border: 1px solid #3762bc;
- color: #fff;
- padding: 9px 14px;
- font-size: 15px;
- line-height: normal;
- border-radius: 5px;
- margin-left: 364px;
- margin-top: 60px;
- }
- </style>
- </head>
- <body>
- <form action="AddServlet" method="post">
- <table>
- <tr>
- <th scope="col" abbr="Dual 1.8">用户名</th>
- <th scope="col" abbr="Dual 2">密码</th>
- <th scope="col" abbr="Dual 2">姓名</th>
- <th scope="col" abbr="Dual 2">邮箱</th>
- <th scope="col" abbr="Dual 2">电话</th>
- </tr>
- <tr>
- <td><label>
- <input type="text" name="username">
- </label></td>
- <td><label>
- <input type="text" name="password">
- </label></td>
- <td><label>
- <input type="text" name="name">
- </label></td>
- <td><label>
- <input type="text" name="email">
- </label></td>
- <td><label>
- <input type="text" name="telephone">
- </label></td>
- </tr>
- </table>
- <input type="submit" value="提交" class="but">
- </form>
- </body>
- </html>
复制代码 10、修改用户
10.1、修改信息的显示页面
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%--
- Created by IntelliJ IDEA.
- User: zengxiong
- Date: 2021/5/27
- Time: 8:05
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>修改用户</title>
- <link type="text/css" rel="stylesheet" href="login_success_css.css">
- <style>
- form {
- margin-top: 200px;
- margin-left: 300px;
- }
- .but{
- width: 200px;
- min-height: 20px;
- display: block;
- background-color: #4a77d4;
- border: 1px solid #3762bc;
- color: #fff;
- padding: 9px 14px;
- font-size: 15px;
- line-height: normal;
- border-radius: 5px;
- margin-left: 364px;
- margin-top: 60px;
- }
- </style>
- </head>
- <body>
- <form method="post">
- <table>
- <tr>
- <th scope="col" abbr="Dual 1.8">用户名</th>
- <th scope="col" abbr="Dual 2">密码</th>
- <th scope="col" abbr="Dual 2">姓名</th>
- <th scope="col" abbr="Dual 2">邮箱</th>
- <th scope="col" abbr="Dual 2">电话</th>
- </tr>
- <tr>
- <td><label>
- <input type="text" name="username" value="${requestScope.user.userName}">
- </label></td>
- <td><label>
- <input type="text" name="password" value="${requestScope.user.password}">
- </label></td>
- <td><label>
- <input type="text" name="name" value="${requestScope.user.name}">
- </label></td>
- <td><label>
- <input type="text" name="email" value="${requestScope.user.email}">
- </label></td>
- <td><label>
- <input type="text" name="telephone" value="${requestScope.user.telephone}">
- </label></td>
- </tr>
- </table>
- <input formaction="UpdateServlet?id=${requestScope.user.id}" type="submit" value="提交" class="but">
- </form>
- </body>
- </html>
复制代码 10.2、编写UpdateServlet
- packagecom.example.web;importcom.example.bean.User;importcom.example.service.UserServiceImpl;importcom.example.service.impl.UserService;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/**
- * @program: ManageResourceSystem
- * @description: 修改图书的业务
- * @author: Mr.Zeng
- * @create: 2021-05-27 08:02
- **/@WebServlet(urlPatterns ="/UpdateServlet")publicclassUpdateServletextendsHttpServlet{UserService userService1 =newUserServiceImpl();@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{//获取恳求参数String id = req.getParameter("id");String username = req.getParameter("username");String password = req.getParameter("password");String name = req.getParameter("name");String email = req.getParameter("email");String telephone = req.getParameter("telephone");int i =0;try{
- i =Integer.parseInt(id);}catch(NumberFormatException e){
- e.printStackTrace();}//修改用户
- userService1.update(newUser(i, username, password, name, email, telephone));//转到用户信息列表页面
- resp.sendRedirect("UserServlet");}}
复制代码 Tip :当我们要修改用户信息时,一般首先会展示原本用户的信息,在这里编写一个获取当前修改用户信息的OneUserServlet。
- 编写OneUserServlet
- packagecom.example.web;importcom.example.bean.User;importcom.example.service.UserServiceImpl;importcom.example.service.impl.UserService;importjavax.servlet.*;importjavax.servlet.http.*;importjavax.servlet.annotation.*;importjava.io.IOException;@WebServlet(urlPatterns ="/OneUserServlet")publicclassOneUserServletextendsHttpServlet{UserService userService1 =newUserServiceImpl();@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{super.doGet(req, resp);}@OverrideprotectedvoiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{//获取用户idString id = request.getParameter("id");int i =0;try{
- i =Integer.parseInt(id);}catch(NumberFormatException e){
- e.printStackTrace();}//通过UserService查询此id用户信息User user = userService1.queryById(i);//把全部信息保管到Request域中
- request.setAttribute("user",user);//恳求转发到update_list.jsp
- request.getRequestDispatcher("/update_list.jsp").forward(request, response);}}
复制代码 11、删除用户
11.1、编写DeleteServlet
- packagecom.example.web;importcom.example.service.UserServiceImpl;importcom.example.service.impl.UserService;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/**
- * @program: ManageResourceSystem
- * @description: 删除数据的业务
- * @author: Mr.Zeng
- * @create: 2021-05-27 08:02
- **/@WebServlet(urlPatterns ="/DeleteServlet")publicclassDeleteServletextendsHttpServlet{UserService userService1 =newUserServiceImpl();@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{//获取恳求的参数IDString id = req.getParameter("id");int i =0;try{
- i =Integer.parseInt(id);}catch(NumberFormatException e){
- e.printStackTrace();}//调用userServlet1的deleteById方法
- userService1.deleteById(i);//重返信息列表
- resp.sendRedirect("UserServlet");}}
复制代码 12、整体展示
|