一、题目要求
根据所给素材完善系统,具体要求:
链接:https://pan.baidu.com/s/15NdAt-aiv-X9sRbMSfXYXQ
提取码:7agw
素材:
链接:https://pan.baidu.com/s/1Af33mSflqFHj-1gj4sOOpw
提取码:wpye
二、源代码
login.html 只在script标签内增加了如下部分,用于接受servlet返回值及提示用户。
window.οnlοad=function(){ var s = location.search; //获取url中"?"符后的字串 if(s.indexOf("?") != -1) { var str = s.split("="); if(str[1] == "1") alert("密码错误,请重新录入!"); if(str[1] == "2") alert("该用户不存在!"); } }
zhu/zhuce.jsp 这是新增的一个页面,用于注册。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/zhuceServlet" method="post" onsubmit="return check(this)"> 用户姓名:<input type="text" id="user" name="user"/><br> 密码:<input type="password" name="password" id="password"/><br> 重复密码:<input type="password" name="password2" id="password2"/><br> 手机号码:<input type="text" id="number" name="number" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/><br> 所属单位:<input type="text" id="belongto" name="belongto"/><br> <input type="submit" value="注册" /> </form> </body> <script> window.onload=function(){ var url = location.search; //获取url中"?"符后的字串 if(url.indexOf("?") != -1) { var str = url.split("="); if(str[1] == "0") alert("已存在该用户,请重新注册!"); if(str[1] == "1") alert("注册成功!"); } } function check(form){ if(form.user.value=="" || form.password.value=="" || form.password2.value=="" || form.belongto.value=="") alert("请将注册信息填写完整!"); else if(form.user.value.length > 20 || form.user.value.length < 6) alert("用户名应在6位到20位之间!"); else if(form.password.value.length > 20 || form.password.value.length < 6) alert("密码应在6位到20位之间!"); else if(form.password.value != form.password2.value) alert("密码应和重复密码保持一致!"); else if(form.number.value.length != 11) alert("手机号码应为11位数字!"); else return true; return false; } </script> </html>
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_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>TRC System</display-name> <servlet> <servlet-name>Login</servlet-name> <servlet-class>dao.DoLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/dengluServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>Register</servlet-name> <servlet-class>dao.DoRegister</servlet-class> </servlet> <servlet-mapping> <servlet-name>Register</servlet-name> <url-pattern>/zhuceServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>Exit</servlet-name> <servlet-class>dao.DoExit</servlet-class> </servlet> <servlet-mapping> <servlet-name>Exit</servlet-name> <url-pattern>/tuichuServlet</url-pattern> </servlet-mapping> <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> </web-app>
dao/DoLogin.java
package dao; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.DBUtil; public class DoLogin extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Connection conn = DBUtil.getConn(); String sql="select * from user where username=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, request.getParameter("user")); ResultSet rs = ps.executeQuery(); if(rs.next()) { String password = request.getParameter("pwd"); if(password.equals(rs.getString("userpassword"))) response.sendRedirect(request.getContextPath() + "/main.html?status=0"); //0表示登陆成功 else response.sendRedirect(request.getContextPath() + "/login.html?status=1"); //1表示密码错误 } else response.sendRedirect(request.getContextPath() + "/login.html?status=2"); //2表示不存在该用户 DBUtil.close(rs, ps, conn); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
dao/DoRegister.java
package dao; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.DBUtil; public class DoRegister extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Connection conn = DBUtil.getConn(); String sql="select * from user where username=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, request.getParameter("user")); ResultSet rs = ps.executeQuery(); if(rs.next()) response.sendRedirect(request.getContextPath() + "/zhu/zhuce.jsp?status=0"); //0表示已存在该用户 else { sql= "insert into user (username,userpassword) values(?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, request.getParameter("user")); ps.setString(2, request.getParameter("password")); ps.executeUpdate(); DBUtil.close(rs, ps, conn); response.sendRedirect(request.getContextPath() + "/zhu/zhuce.jsp?status=1"); //1表示注册成功 } } catch (SQLException e) { e.printStackTrace(); } } }
util/DBUtil.java
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static String DBDRIVER = "com.mysql.cj.jdbc.Driver"; public static String DBURL = "jdbc:mysql://localhost/trc" + "?serverTimezone=GMT%2B8"; public static String DBUSER = "root"; public static String DBPASSWORD = "WJL.991018"; public static Connection getConn() { Connection conn = null; try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close(PreparedStatement state, Connection conn) { try { if(state!=null) state.close(); if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs, PreparedStatement state, Connection conn) { try { if(rs!=null) rs.close(); if(state!=null) state.close(); if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
整个项目已打包上传到云盘:
链接:https://pan.baidu.com/s/1lIHayhzOxLlCATNteAXwvg
提取码:agxv
三、总结
题目满分15分,只得了6分,这还是学长把我勉强满足要求的都算上了。只有一个感想:改别人代码真难,拿到素材后我先用了1个多小时把注册、登陆完成了,中间尤其是配置xml文件耗费了许多时间,eclipse也一直抽风,明明配置全部正确,但是就是一直404,重启tomcat无法解决,结果重启工作空间后啥也没改就能运行。之后所有时间都耗在研究为什么登陆过去了又给跳回来,知道了原因后又有了新的疑问,为什么req.status这个变量是404?直到目前此问题还是没有解决。