javaweb之添加学生信息
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
add.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <script type="text/javascript"> function checkid(form1) { var userid = form1.userid.value; if (userid == "" || !isUsername(userid)) { alert("用户名要求由6到12位字母、数字、下划线组成,只有字母可以开头!"); form1.userid.value=""; form1.userid.focus(); return false; } } function checkpsw(form1) { var userpsw = form1.userpsw.value; if(userpsw=="") return true; if (!isUserpsw(userpsw)) { alert("密码要求八位以上字母、数字组成。"); form1.userpsw.value=""; form1.userpsw.focus(); return false; } } function checknum(form1) { var usernum = form1.usernum.value; if (!usernum.substring(0, 4) == "2018" || usernum.length != 8) { alert("学号要求八位数字组成,前四位为“2018”开头"); form1.usernum.value=""; form1.usernum.focus(); return false; } return true; } function isUsername(userid) { if (!((userid.charAt(0) >= 'a' && userid.charAt(0) <= 'z') || (userid .charAt(0) >= 'A' && userid.charAt(0) <= 'Z'))) { return false; } for (var i = 1; i < userid.length; i++) { if (((userid.charAt(i) >= 'a' && userid.charAt(i) <= 'z') || (userid .charAt(i) >= 'A' && userid.charAt(i) <= 'Z')) || (userid.charAt(i) >= '0' && userid.charAt(i) <= '9')) { continue; } else return false; } return true; } function isUserpsw(str) { if (str.length < 8){ return false; } for (var i = 0; i < str.length; i++) { if ((str.charAt(i) >= '0' && str.charAt(i)<= '9') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')){ continue; } else return false; } return true; } </script> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <form name="form1" action="UserServlet?method=add" method="post" onsubmit="return check(form1);"> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <% } %> <h2 align="center">请注册</h2> <table align="center"> <tr> <td>登录账号:</td> <td><input type="text" id="userid" name="userid" onchange="checkid(form1)" placeholder="请输入账号" /></td> </tr> <tr> <td>登录密码:</td> <td><input type="password" id="userpsw" name="userpsw" onchange="checkpsw(form1)" placeholder="请输入密码" /></td> </tr> <tr> <td>性别:</td> <td><select id="usersex" name="usersex"> <option>男</option> <option>女</option> </select></td> </tr> <tr> <td>姓名:</td> <td><input type="text" id="username" name="username" placeholder="请输入姓名" /></td> </tr> <tr> <td>学号:</td> <td><input type="text" id="usernum" name="usernum" onchange="checknum(form1)" placeholder="请输入学号" /></td> </tr> <tr> <td>电子邮件:</td> <td><input type="email" id="useremail" name="useremail" placeholder="请输入邮箱" /></td> </tr> <tr> <td>所在学院:</td> <td><input type="text" id="userxy" name="userxy" placeholder="请输入学院" /></td> </tr> <tr> <td>所在系:</td> <td><input type="text" id="userxi" name="userxi" placeholder="请输入系" /></td> </tr> <tr> <td>所在班级:</td> <td><input type="text" id="userclass" name="userclass" placeholder="请输入班级" /></td> </tr> <tr> <td>入学年份(届):</td> <td><select id="useryear" name="useryear"> <option>2017</option> <option>2018</option> <option>2019</option> </select>届</td> </tr> <tr> <td>生源地:</td> <td><input type="text" id="userdizhi" name="userdizhi" placeholder="请输入生源地" /></td> </tr> <tr> <td>备注:</td> <td><textarea rows="8" id="userbeizhu" name="userbeizhu" cols="40">备注</textarea></td> </tr> <tr> <td><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
user.java
package com.Studentweb; public class User { private int id; private String userid; private String userpsw; private String usersex; private String username; private String usernum; private String useremail; private String userxy; private String userxi; private String userclass; private String useryear; private String uesrdizhi; private String uesrbeizhu; public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUserpsw() { return userpsw; } public void setUserpsw(String userpsw) { this.userpsw = userpsw; } public String getUsersex() { return usersex; } public void setUsersex(String usersex) { this.usersex = usersex; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUsernum() { return usernum; } public void setUsernum(String usernum) { this.usernum = usernum; } public String getUseremail() { return useremail; } public void setUseremail(String useremail) { this.useremail = useremail; } public String getUserxy() { return userxy; } public void setUserxy(String userxy) { this.userxy = userxy; } public String getUserxi() { return userxi; } public void setUserxi(String userxi) { this.userxi = userxi; } public String getUserclass() { return userclass; } public void setUserclass(String userclass) { this.userclass = userclass; } public String getUseryear() { return useryear; } public void setUseryear(String useryear) { this.useryear = useryear; } public String getUesrdizhi() { return uesrdizhi; } public void setUesrdizhi(String uesrdizhi) { this.uesrdizhi = uesrdizhi; } public String getUesrbeizhu() { return uesrbeizhu; } public void setUesrbeizhu(String uesrbeizhu) { this.uesrbeizhu = uesrbeizhu; } public User(String userid, String userpsw, String usersex, String username, String usernum, String useremail, String userxy, String userxi, String userclass, String useryear, String uesrdizhi, String uesrbeizhu) { super(); this.userid = userid; this.userpsw = userpsw; this.usersex = usersex; this.username = username; this.usernum = usernum; this.useremail = useremail; this.userxy = userxy; this.userxi = userxi; this.userclass = userclass; this.useryear = useryear; this.uesrdizhi = uesrdizhi; this.uesrbeizhu = uesrbeizhu; } }
userdao.java
package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import util.DBUtil; import com.Studentweb.User; public class userdao { public boolean add(User user) { String sql = "insert into text(userid,userpsw,usersex,username,usernum,useremail,userxy,userxi,userclass,useryear,userdizhi,userbeizhu)" + "values('" + user.getUserid() + "','" + user.getUserpsw()+ "','" + user.getUsersex() + "','" + user.getUsername()+ "','" + user.getUsernum()+ "','" + user.getUseremail()+ "','" + user.getUserxy()+ "','" + user.getUserxi()+ "','" + user.getUserclass()+ "','" + user.getUseryear()+ "','" + user.getUesrdizhi()+ "','" + user.getUesrbeizhu()+ "')"; Connection conn = DBUtil.getConn();//调用方法连接数据库 Statement state = null; boolean f = false; int a = 0 ; try { //监视大括号内的代码 state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { //捕获错误 e.printStackTrace(); } finally { //关闭z 连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } }
DBUtil.java
package util; //数据库连接 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Zheng * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/text?useSSL=false"; public static String db_user = "root"; public static String db_pass = "fengge666"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
userservlet.java
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.Studentweb.User; import Dao.userdao; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UserServlet() { super(); // TODO Auto-generated constructor stub } userdao dao = new userdao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String userid = req.getParameter("userid"); String userpsw = req.getParameter("userpsw"); String username=req.getParameter("username"); String usersex=req.getParameter("usersex"); String usernum=req.getParameter("usernum"); String useremail=req.getParameter("useremail"); String userxy=req.getParameter("userxy"); String userxi=req.getParameter("userxi"); String userclass=req.getParameter("userclass"); String useryear=req.getParameter("useryear"); String userdizhi=req.getParameter("userdizhi"); String userbeizhu=req.getParameter("userbeizhu"); User user= new User(userid,userpsw,username,usersex,usernum,useremail,userxy,userxi,userclass,useryear,userdizhi,userbeizhu); dao.add(user); req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("add.jsp").forward(req, resp); } }
最终界面:
输入完相应对话框,会自动判断是否满足要求,并给出提示信息
个人总结:在做极限测试,出现了几个问题,第一个对判断的语句没有弄清楚,课下对javascript里的字符串语句进行了学习,掌握了基本的判断字符串的信息,第二个问题是:onblur对浏览器一直保持定位和刷新,导致无法添加信息,最终改为onchange的方法,成功在浏览器上运行。