list.jsp页面调整:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <!-- 网页使用的语言 --> <html lang="zh-CN"> <head> <!-- 指定字符集 --> <meta charset="utf-8"> <!-- 使用Edge最新的浏览器的渲染方式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。 width: 默认宽度与设备的宽度相同 initial-scale: 初始的缩放比,为1:1 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>用户信息管理系统</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <style type="text/css"> td, th { text-align: center; } </style> </head> <body> <div class="container"> <h3 style="text-align: center">用户信息列表</h3> <div style="float: left"> <form class="form-inline"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" class="form-control" id="exampleInputName2" placeholder="李四"> </div> <div class="form-group"> <label for="exampleInputEmail2">籍贯</label> <input type="email" class="form-control" id="exampleInputEmail2" placeholder="北京"> </div> <div class="form-group"> <label for="exampleInputEmail3">邮箱</label> <input type="email" class="form-control" id="exampleInputEmail3" placeholder=""> </div> <button type="submit" class="btn btn-default">查询</button> </form> </div> <div style="float: right; margin: 5px"> <td colspan="8" align="center"><a class="btn btn-primary" href="add.html">添加联系人</a></td> <td colspan="8" align="center"><a class="btn btn-primary" href="add.html">删除选中</a></td> </div> <table border="1" class="table table-bordered table-hover"> <tr class="success"> <th><input type="checkbox"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>籍贯</th> <th>QQ</th> <th>邮箱</th> <th>操作</th> </tr> <c:forEach items="${users}" var="user" varStatus="s"> <tr> <td><input type="checkbox"></td> <td>${s.count}</td> <td>${user.name}</td> <td>${user.gender}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td><a class="btn btn-default btn-sm" href="update.html">修改</a> <a class="btn btn-default btn-sm" href="">删除</a></td> </tr> </c:forEach> </table> <div> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <span style="font-size: 25px;margin-left: 5px"> 共15条记录,共4页 </span> </ul> </nav> </div> </div> </body> </html>
登录功能:
1.调整页面,加入验证码功能
导入checkCodeServlet类随机生成验证码
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>管理员登录</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <script type="text/javascript"> //切换验证码 function refreshCode() { //1.获取验证码图片对象 let vcode = document.getElementById("vcode"); //2.设置src属性,加时间戳 vcode.src = "${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime(); } </script> </head> <body> <div class="container" style="width: 400px;"> <h3 style="text-align: center;">管理员登录</h3> <form action="${pageContext.request.contextPath}/loginServlet" method="post"> <div class="form-group"> <label for="user">用户名:</label> <input type="text" name="username" class="form-control" id="user" placeholder="请输入用户名"/> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/> </div> <div class="form-inline"> <label for="vcode">验证码:</label> <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder="请输入验证码" style="width: 120px;"/> <a href="javascript:refreshCode()"> <img src="${pageContext.request.contextPath}/checkCodeServlet" title="看不清点击刷新" id="vcode"/> </a> </div> <hr/> <div class="form-group" style="text-align: center;"> <input class="btn btn btn-primary" type="submit" value="登录"> </div> </form> <!-- 出错显示的信息框 --> <div class="alert alert-warning alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" > <span>×</span></button> <strong>${login_msg}</strong> </div> </div> </body> </html>
登录功能代码实现
UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); public User1 findUserByUsernameAndPassword(String username,String password); }
UserDaoImpl类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.util.JDBCUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "select * from user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } @Override public User1 findUserByUsernameAndPassword(String username, String password) { try { String sql = "select * from user1 where username = ? and password = ?"; User1 user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class), username, password); return user1; } catch (DataAccessException e) { e.printStackTrace(); return null; } } }
UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { public List<User1> findAll(); User1 login(User1 user1); }
UserServiceImpl类:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } @Override public User1 login(User1 user1) { return dao.findUserByUsernameAndPassword(user1.getUsername(),user1.getPassword()); } }
loginServlet类:
package com.example.web.servlet; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet(name = "loginServlet", value = "/loginServlet") public class loginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置编码 request.setCharacterEncoding("utf-8"); //2.获取数据 //2.1获取用户填写的验证码 String verifycode = request.getParameter("verifycode"); //验证码进行一个校验 HttpSession session = request.getSession(); String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER");//确保验证码保持唯一 if (!checkcode_server.equalsIgnoreCase(verifycode)){//比较深沉的验证码 & 用户输入的验证码 //验证码不正确 //提示信息 request.setAttribute("login_msg", "验证码错误!"); //跳转登录页面 request.getRequestDispatcher("/login.jsp").forward(request, response); return; } //3.封装User1对象 Map<String, String[]> map = request.getParameterMap(); User1 user1 = new User1(); try { BeanUtils.populate(user1, map); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } //4.调用Service查询 UserService service = new UserServiceImpl(); User1 loginUser1 = service.login(user1); //5.判断是否登录成功 if (loginUser1 != null){ //登录成功 //将用户存入session session.setAttribute("user1", loginUser1); //跳转页面(request域中没有共享数据 采用重定向) response.sendRedirect(request.getContextPath()+"/index.jsp"); }else { //登录失败 //提示信息 request.setAttribute("login_msg", "用户名或密码错误!"); //跳转登录页面 request.getRequestDispatcher("/login.jsp").forward(request, response); } } }
添加功能分析:
添加功能代码实现:
UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); public User1 findUserByUsernameAndPassword(String username,String password); void add(User1 user1); }
UserDaoImpl实现类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.util.JDBCUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "select * from user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } @Override public User1 findUserByUsernameAndPassword(String username, String password) { try { String sql = "select * from user1 where username = ? and password = ?"; User1 user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User1>(User1.class), username, password); return user1; } catch (DataAccessException e) { e.printStackTrace(); return null; } } @Override public void add(User1 user1) { //1.定义sql String sql = "insert into user1 value(null,?,?,?,?,?,?,null,null)"; //2.执行sql template.update(sql,user1.getName(),user1.getGender(),user1.getAge(),user1.getAddress(),user1.getQq(),user1.getEmail()); } }
UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { /** * 查询方法 * @return */ public List<User1> findAll(); /** * 登录方法 * @param user1 * @return */ User1 login(User1 user1); /** * 添加User1 * @param user1 */ void addUser(User1 user1); }
UserServiceImpl实现类:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } @Override public User1 login(User1 user1) { return dao.findUserByUsernameAndPassword(user1.getUsername(),user1.getPassword()); } @Override public void addUser(User1 user1) { dao.add(user1); } }
addUserServlet类:
package com.example.web.servlet; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet(name = "addUserServlet", value = "/addUserServlet") public class addUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置编码 request.setCharacterEncoding("utf-8"); //2.获取参数 Map<String, String[]> map = request.getParameterMap(); //3.封装对象 User1 user1 = new User1(); try { BeanUtils.populate(user1, map); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } //4.调用Service保存 UserService service = new UserServiceImpl(); service.addUser(user1); //5.跳转到userListServlet response.sendRedirect(request.getContextPath()+"/userListServlet"); } }
标签:功能,登录,user1,User1,public,添加,import,com,example From: https://www.cnblogs.com/qihaokuan/p/17006294.html