1 题目说明
采用MVC模式实现用户注册和管理员查询功能
用户直接注册,以手机号作为主键,不允许重复
管理员可以看到所有注册的用户列表
注册信息至少包括:手机号、姓名、密码、邮箱
2 实验设计
2.1 表设计
2.2 工程结构
工程结构:采用MVC模式,使用jsp+servlet+javabean。后端还使用jdbc的数据库。M是业务模型。业务模型定义了用户类,使用java bean,java bean中的User类有每个用户类模型的定义、getter、setter。在数据库中,用户有多个字段,分别为手机号(不能重复)、密码、邮箱、姓名,以及数据库后端隐藏的id号作为主键。V是用户视图。用户前端中,register.jsp是用户进行信息填写与注册的前端页面。若用户注册成功,则跳转到res.jsp页面。若用户注册失败(因为手机号重复),则跳转到duplicate.jsp页面。在register.jsp页面点击“管理用户名单”,跳转到manage.jsp并查看用户信息。
C是控制器。使用servlet,用户注册时交给AddServlet处理,查看用户名单时交给ShowServlet处理。每个Servlet都会将与数据库相关的业务服务通过调用UserService进行处理。UserService会链接数据库,调用DBConn,利用jdbc链接mysql数据库,并执行sql语句进行进一步处理。
3 运行界面截图与说明
在register.jsp中,可以进行注册。注册时输入手机号、密码、用户名、邮箱,并点击“提交”。也可以不注册,直接查看管理用户名单。
在提交注册表单后,系统会根据手机号信息查询,若查到数据库中已有重复手机号,则提示用户手机号重复且注册失败,跳转到duplicate.jsp。
在提交注册表单后,系统会根据手机号信息查询,若查到数据库中没有重复手机号,则提示用户注册成功,并向数据库中添加相应信息。
在提交注册表单时,若填写的信息不全,有空的数据项,则系统跳出提示“注册信息不得为空!”在前端的jsp页面和后端执行数据库的插入时都会判断一次,以保证数据库的安全性与完整性,不出现空信息。
(此部分涉及隐私信息,不放出)
在点击“管理用户名单”后,可以查看每个用户的手机号、密码、姓名与邮箱等完整信息。实现了管理员查询的功能。
4 代码
4.1 User.java
package bean; public class User { private int id; private String username; private String password; private String cellphone; private String email; public int getId() { return id; } public void setId(int i) { this.id = i; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getCellphone() { return cellphone; } public void setCellphone(String cellphone) { this.cellphone=cellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email=email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4.2 DBConn.java
package db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConn { public Connection getConnection() { String URL = "jdbc:mysql://localhost:3306/lab6?characterEncoding=utf-8"; String USER = "root"; String PASSWORD = "111111"; // 1.加载驱动程序 try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return null; } }
4.3 UserService.java
package service; import java.sql.*; import java.util.*; import db.DBConn; import bean.User; public class UserService { // ****************************************登录service********************************************** public boolean checkLogin(User bean) { // 从JavaBean中获得属性值 String cellphone=bean.getCellphone(); String password=bean.getPassword(); // 创建连接 try { DBConn db=new DBConn(); Connection conn = db.getConnection(); PreparedStatement pre=conn.prepareStatement("select * from lab6table where cellphone='"+cellphone+"' and password='" + password+"'"); ResultSet rs=pre.executeQuery(); if(rs.next()){ pre.close(); rs.close(); return true; } else return false; } catch (SQLException e) { System.out.println("数据库连接出错!"); return false; } } // ****************************************注册和查询service********************************************** public boolean add(User bean) { String cellphone=bean.getCellphone(); String email=bean.getEmail(); String username=bean.getUsername(); String password=bean.getPassword(); try { DBConn db=new DBConn(); Connection conn = db.getConnection(); PreparedStatement pre=conn.prepareStatement("select * from lab6table where cellphone=?"); pre.setString(1,cellphone); ResultSet rs=pre.executeQuery(); if(!rs.next()){ PreparedStatement pre1=conn.prepareStatement("insert into lab6table(cellphone,password,username,email) VALUES(?,?,?,?)"); pre1.setString(1,cellphone); pre1.setString(2,password); pre1.setString(3,username); pre1.setString(4,email); pre1.executeUpdate(); conn.close(); pre.close(); pre1.close(); return true; } else return false; } catch (SQLException e) { System.out.println("数据库连接出错!"); return false; } } public List<User> select() { try { List<User> list=new LinkedList<User>(); DBConn db=new DBConn(); Connection conn=db.getConnection(); PreparedStatement pre=conn.prepareStatement("select * from lab6table"); ResultSet resultSet=pre.executeQuery(); while(resultSet.next()){ String cellphone=resultSet.getString(1); String password=resultSet.getString(2); String username=resultSet.getString(3); String email=resultSet.getString(4); int id=resultSet.getInt(5); User bean=new User(); bean.setCellphone(cellphone); bean.setPassword(password); bean.setUsername(username); bean.setEmail(email); list.add(bean); } return list; }catch(SQLException e) { System.out.println("数据库链接出错!"); return null; } } }
4.4 AddServlet.java
package servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import db.DBConn; import bean.User; import service.UserService; public class AddServlet extends HttpServlet { @Override public void init() throws ServletException { super.init(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String email=req.getParameter("email"); String cellphone=req.getParameter("cellphone"); if(username.length()>0&&password.length()>0&&email.length()>0&&cellphone.length()>0){ // 写入JavaBean User bean=new User(); HttpSession session=req.getSession(true); session.setAttribute("bean",bean); bean.setUsername(username); bean.setPassword(password); bean.setEmail(email); bean.setCellphone(cellphone); UserService addservice = new UserService(); // 判断是否注册成功 if(addservice.add(bean)){ // 转发 RequestDispatcher dis=req.getRequestDispatcher("res.jsp"); dis.forward(req, resp); }else{ // 重定向 resp.sendRedirect("duplicate.jsp"); } }else resp.sendRedirect("register.jsp?Null=yes"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
4.5 ShowServlet.java
package servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import db.DBConn; import bean.User; import service.UserService; import java.util.*; public class ShowServlet extends HttpServlet { @Override public void init() throws ServletException { super.init(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); HttpSession session=req.getSession(true); UserService selectService = new UserService(); // 判断是否注册成功 // 转发 List<User> users=selectService.select(); req.setAttribute("users", users); RequestDispatcher dis=req.getRequestDispatcher("manage.jsp"); dis.forward(req, resp); } }
4.6 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>ch7_test</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>addServlet</servlet-name> <servlet-class>servlet.AddServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>addServlet</servlet-name> <url-pattern>/AddServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>showServlet</servlet-name> <servlet-class>servlet.ShowServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>showServlet</servlet-name> <url-pattern>/ShowServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>web.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> </web-app>
4.7 duplicate.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <jsp:useBean id="bean" class="bean.User" scope="session"/> <html> <head> <title>查询</title> </head> <body> <h2 align="center">注册失败!手机号重复!</h2> </body> </html>
4.8 manage.jsp
<%@page import="bean.*"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>manage.jsp</title> </head> <body> <% List<User> users=(List<User>)request.getAttribute("users"); %> <table border="1"> <tr> <th>手机号</th> <th>密码</th> <th>姓名</th> <th>邮箱</th> </tr> <%for(User user:users){%> <tr> <td><%=user.getCellphone()%></td> <td><%=user.getPassword()%></td> <td><%=user.getUsername()%></td> <td><%=user.getEmail()%></td> <tr> <%}%> </table> </body> </html>
4.9 register.jsp
<%@ page contentType="text/html;charset=UTF-8" %> <html> <head> <title>register.jsp</title> </head> <body> <font size=3> <h1>请注册</h1> <form action="AddServlet" method="post"> <table> <tr> <td>手机号:</td> <td><input type="text" name="cellphone"></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>姓 名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>邮 箱:</td> <td><input type="text" name="email"></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table> </form> </font> <form action="ShowServlet" method="post"> <input type="submit" value="管理用户名单"> </form> <script> var Null= '<%=request.getParameter("Null")%>'; if(Null=='yes'){ alert("注册信息不得为空!"); } </script> </body> </html>
4.10 res.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <jsp:useBean id="bean" class="bean.User" scope="session"/> <html> <head> <title>查询</title> </head> <body> <h2 align="center">注册成功!</h2> </body> </html>