连接数据库已经进行判断
要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
DBUtil.java文件
<%@ 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> <!-- onsubmit="return chack()" --> <form action="UserServlet?method=add" method="post" onsubmit="return chack()" > <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> 账户:<input type="text" id="zhanghu" name="zhanghu" placeholder="请输入账户名" onblur="checkZhanghao()" /><br><br> <span style="font-size:13px" id="zhanghuspan"></span> <!-- <script type="text/javascript"> function limit1(value) { var phone = value; var w=phone.length; if(w<6||w>12) { alert("账号位数非法,确定后重新输入..."); document.all.phone.value = ""; document.all.phone.focus(); return false; } else { for(var a=0;a<w;a++) { if(phone.charAt[a]=="_") { continue; } if(phone.charAt[a]>=48&&phone.charAt[a]<=57) { continue; } if(phone.charAt[a]>=65&&phone.charAt[a]<=90) { continue; } if(phone.charAt[a]>=97&&phone.charAt[a]<=122) { continue; } } } } </script> --> 密码:<input type="password" id="psw" name="psw" placeholder="请输入密码" onblur="checkPassword()" /><br><br> <span style="font-size:13px" id="pswspan"></span> 性别:<select id="sex" name="sex"> <option>男</option> <option>女</option> </select> <br><br> 姓名:<input type="text" id="name" name="name" placeholder="请输入姓名" /><br><br> <!-- 性别:<input type="radio" id="usersex" name="usersex" value="男" checked="checked" />男 <input type="radio" name="usersex" value="女" />女 <br><br>--> 学号:<input type="text" id="xuehao" name="xuehao" placeholder="请输入学号" onblur="checkId()" /><br><br> <span style="font-size:13px" id="xuehaospan"></span> 邮箱:<input type="email" id="mail" name="mail" placeholder="请输入邮箱" /><br><br> 所在学院:<input type="text" id="xueyuan" name="xueyuan" placeholder="请输入学院" /><br><br> 所在系:<input type="text" id="xi" name="xi" placeholder="请输入所在系" /><br><br> 所在班级:<input type="text" id="class1" name="class1" placeholder="请输入所在班级" /><br><br> 入学年份:<select id="year" name="year"> <option>1998</option> <option>1999</option> <option>2000</option> <option>2001</option> <option>2002</option> <option>2003</option> <option>2004</option> <option>2005</option> <option>2006</option> <option>2007</option> <option>2008</option> <option>2009</option> <option>2010</option> <option>2011</option> <option>2012</option> <option>2013</option> <option>2014</option> <option>2015</option> <option>2016</option> <option>2017</option> <option>2018</option> <option>2019</option>届 </select> <br><br> 所在生源地:<input type="text" id="sheng" name="sheng" placeholder="请输入所在生源地" /><br><br> 备注:<input type="text" id="beizhu" name="beizhu" placeholder="请输入备注" /><br><br> <input type="submit" value="添加"> <input type="reset" value="重置"> <!-- 手机号:<input type="text" id="userphone" name="userphone" maxlength="11" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="请输入手机号" onblur="limit(this.value)"/> <script language="javaScript"> function limit(value){ var phone = value; var reg = /[1][3,4,5,7,8]+[0-9]{9}/; if(phone.length != 11 || reg.test(phone) == false){ alert("输入的手机号非法,确定后重新输入..."); document.all.phone.value = ""; document.all.phone.focus(); return false; } } </script> --> <br/> <br/> <script type="text/javascript"> //重置按钮处添加onclick调用formReset %> function checkZhanghao() { var zhanghao = document.getElementById("zhanghu"); var zhanghao1=zhanghao.value; var spanNode = document.getElementById("zhanghuspan"); //编写邮箱的正则 var reg = /^[a-z][a-z0-9A-Z]{6}/; if(reg.test(zhanghao1)){ //符合规则 spanNode.innerHTML = "".fontcolor("green"); return true; }else{ //不符合规则 alert('账户格式错误'); zhanghao.value = ""; zhanghao.focus(); return false; } } function checkPassword() { var password = document.getElementById("psw"); if((password.value).length!=8){ alert('密码应为8位英文或数字'); userpsw.focus(); return false; } } function checkId() { var id = document.getElementById("xuehao").value; var spanNode = document.getElementById("xuehaospan"); //学号的正确形式 // var reg = /^[0-9a-zA-Z]{6}/; if((id<"20180000")|| (id>"20189999")) { spanNode.innerHTML = "学号格式不正确,请重新填写!".fontcolor("red"); return false; } else{ //不符合规则 spanNode.innerHTML = "".fontcolor("green"); return true; } } </script> <script type="text/javascript"> function chack1() { var zhanghu1=document.getElementById("zhanghu"); var psw1=document.getElementById("psw"); var xuehao1=document.getElementById("xuehao"); var xi=document.getElementById("xi"); var class1=document.getElementById("class1"); var year=document.getElementById("year"); var sheng=document.getElementById("sheng"); var beizhu=document.getElementById("beizhu"); var zhanghu=zhanghu1.value; var xuehao=xuehao1.value; var psw=pse1.value; var zhi; var xue; var wen; wen=xuehao.value.length(); xue=zhanghu.value.length(); zhi=psw.value.length(); if(xue<6||xue>12) { alert("账号位数错误"); zhanghu.focus(); return false; } for(var a=0;a<xue;a++) { if(zhanghu.charAt[a]="_") { alert("账号类型错误"); zhanghu.focus(); return false; } if(zhanghu.value.charAt[a]>=48&&zhanghu.charAt[a]<=57) { continue; } if(zhanghu.charAt[a]>=65&&zhanghu.charAt[a]<=90) { continue; } if(zhanghu.charAt[a]>=97&&zhanghu.charAt[a]<=122) { continue; } alert("账号类型错误"); zhanghu.focus(); return false; } if(zhi<8) { alert("密码位数错误"); psw.focus(); return false; } for(var b=0;b<zhi;b++) { if(psw.charAt[b]>=48&&psw.charAt[b]<=57) { continue; } if(psw.charAt[b]>=65&&psw.charAt[b]<=90) { continue; } if(psw.charAt[b]>=97&&psw.charAt[b]<=122) { continue; } alert("密码类型错误"); psw.focus(); return false; } if(wen!=8) { alert("学号位数错误"); xuehao.focus(); return false; } if(xuehao.charAt[0]!=2||xuehao.charAt[1]!=0||xuehao.charAt[2]!=1||xuehao.charAt[3]!=8) { alert("学号开头不是2018"); xuehao.focus(); return false; } } } </script> </form> </body> </html>
User.servlet
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 javax.swing.JOptionPane; import com.newjavaweb.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); } else if ("search".equals(method)) { search(req, resp); } } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String zhanghu = req.getParameter("zhanghu"); String psw = req.getParameter("psw"); String rpassword = dao.search(zhanghu); if(psw.equals(rpassword)) { //JOptionPane.showMessageDialog(null,"登陆成功!"); req.setAttribute("message", "登陆成功!"); req.getRequestDispatcher("NewFile.jsp").forward(req,resp); } else { //JOptionPane.showMessageDialog(null,"账号不存在或密码错误!"); req.setAttribute("message", "账号不存在或密码错误!"); req.getRequestDispatcher("login.jsp").forward(req,resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String zhanghu = req.getParameter("zhanghu"); String psw = req.getParameter("psw"); String sex=req.getParameter("sex"); String name=req.getParameter("name"); String xuehao=req.getParameter("xuehao"); String mail=req.getParameter("mail"); String xueyuan=req.getParameter("xueyuan"); String xi=req.getParameter("xi"); String class1=req.getParameter("class1"); String year=req.getParameter("year"); String sheng=req.getParameter("sheng"); String beizhu=req.getParameter("beizhu"); User user= new User(zhanghu,psw,sex,name,xuehao,mail,xueyuan,xi,class1,year,sheng,beizhu); String rusername = dao.find(zhanghu); if(!zhanghu.equals(rusername)) { dao.add(user); req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("login.jsp").forward(req, resp); }else { req.setAttribute("message", "账户重复,请重新输入!"); req.getRequestDispatcher("zc.jsp").forward(req, resp); } } }
usedao.java
package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import util.DBUtil; import com.newjavaweb.User; public class userdao { public boolean add(User user) { String sql = "insert into text(zhanghu,psw,sex,name,xuehao,mail,xueyuan,xi,class1,year,sheng,beizhu)" + "values('" + user.getZhanghu() + "','" + user.getPsw()+ "','" + user.getSex() + "','" + user.getName()+ "','" + user.getXuehao()+ "','" + user.getMail()+ "','" + user.getXueyuan()+ "','" + user.getXi()+ "','" + user.getClass1()+ "','" + user.getYear()+ "','" + user.getSheng()+ "','" + user.getBeizhu()+ "')"; 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; } public String find(String zhanghu) { String sql = "select * from text where "; if (zhanghu!= "") { sql += "zhanghu like '%" + zhanghu + "%'"; } Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; String username2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { username2 = rs.getString("zhanghu"); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return username2; } public String search(String zhanghu) { String sql = "select * from text where "; if (zhanghu!= "") { sql += "zhanghu like '%" + zhanghu + "%'"; } Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; String password2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { password2 = rs.getString("psw"); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return password2; } }
User.java
package com.newjavaweb; public class User { private String zhanghu; private String psw; private String sex; private String name; private String xuehao; private String mail; private String xueyuan; private String xi; private String class1; private String year; private String sheng; private String beizhu; public String getZhanghu() { return zhanghu; } public void setZhanghu(String zhanghu) { this.zhanghu = zhanghu; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getXuehao() { return xuehao; } public void setXuehao(String xuehao) { this.xuehao = xuehao; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getXueyuan() { return xueyuan; } public void setXueyuan(String xueyuan) { this.xueyuan = xueyuan; } public String getXi() { return xi; } public void setXi(String xi) { this.xi = xi; } public String getClass1() { return class1; } public void setClass1(String class1) { this.class1 = class1; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getSheng() { return sheng; } public void setSheng(String sheng) { this.sheng = sheng; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } public User(String zhanghu, String psw, String sex, String name, String xuehao, String mail, String xueyuan, String xi, String class1, String year, String sheng, String beizhu) { super(); this.zhanghu = zhanghu; this.psw = psw; this.sex = sex; this.name = name; this.xuehao = xuehao; this.mail = mail; this.xueyuan = xueyuan; this.xi = xi; this.class1 = class1; this.year = year; this.sheng = sheng; this.beizhu = beizhu; } }
zc.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> <!-- onsubmit="return chack()" --> <form action="UserServlet?method=add" method="post" onsubmit="return chack()" > <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> 账户:<input type="text" id="zhanghu" name="zhanghu" placeholder="请输入账户名" onblur="checkZhanghao()" /><br><br> <span style="font-size:13px" id="zhanghuspan"></span> <!-- <script type="text/javascript"> function limit1(value) { var phone = value; var w=phone.length; if(w<6||w>12) { alert("账号位数非法,确定后重新输入..."); document.all.phone.value = ""; document.all.phone.focus(); return false; } else { for(var a=0;a<w;a++) { if(phone.charAt[a]=="_") { continue; } if(phone.charAt[a]>=48&&phone.charAt[a]<=57) { continue; } if(phone.charAt[a]>=65&&phone.charAt[a]<=90) { continue; } if(phone.charAt[a]>=97&&phone.charAt[a]<=122) { continue; } } } } </script> --> 密码:<input type="password" id="psw" name="psw" placeholder="请输入密码" onblur="checkPassword()" /><br><br> <span style="font-size:13px" id="pswspan"></span> 性别:<select id="sex" name="sex"> <option>男</option> <option>女</option> </select> <br><br> 姓名:<input type="text" id="name" name="name" placeholder="请输入姓名" /><br><br> <!-- 性别:<input type="radio" id="usersex" name="usersex" value="男" checked="checked" />男 <input type="radio" name="usersex" value="女" />女 <br><br>--> 学号:<input type="text" id="xuehao" name="xuehao" placeholder="请输入学号" onblur="checkId()" /><br><br> <span style="font-size:13px" id="xuehaospan"></span> 邮箱:<input type="email" id="mail" name="mail" placeholder="请输入邮箱" /><br><br> 所在学院:<input type="text" id="xueyuan" name="xueyuan" placeholder="请输入学院" /><br><br> 所在系:<input type="text" id="xi" name="xi" placeholder="请输入所在系" /><br><br> 所在班级:<input type="text" id="class1" name="class1" placeholder="请输入所在班级" /><br><br> 入学年份:<select id="year" name="year"> <option>1998</option> <option>1999</option> <option>2000</option> <option>2001</option> <option>2002</option> <option>2003</option> <option>2004</option> <option>2005</option> <option>2006</option> <option>2007</option> <option>2008</option> <option>2009</option> <option>2010</option> <option>2011</option> <option>2012</option> <option>2013</option> <option>2014</option> <option>2015</option> <option>2016</option> <option>2017</option> <option>2018</option> <option>2019</option>届 </select> <br><br> 所在生源地:<input type="text" id="sheng" name="sheng" placeholder="请输入所在生源地" /><br><br> 备注:<input type="text" id="beizhu" name="beizhu" placeholder="请输入备注" /><br><br> <input type="submit" value="添加"> <input type="reset" value="重置"> <!-- 手机号:<input type="text" id="userphone" name="userphone" maxlength="11" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="请输入手机号" onblur="limit(this.value)"/> <script language="javaScript"> function limit(value){ var phone = value; var reg = /[1][3,4,5,7,8]+[0-9]{9}/; if(phone.length != 11 || reg.test(phone) == false){ alert("输入的手机号非法,确定后重新输入..."); document.all.phone.value = ""; document.all.phone.focus(); return false; } } </script> --> <br/> <br/> <script type="text/javascript"> //重置按钮处添加onclick调用formReset %> function checkZhanghao() { var zhanghao = document.getElementById("zhanghu"); var zhanghao1=zhanghao.value; var spanNode = document.getElementById("zhanghuspan"); //编写邮箱的正则 var reg = /^[a-z][a-z0-9A-Z]{6}/; if(reg.test(zhanghao1)){ //符合规则 spanNode.innerHTML = "".fontcolor("green"); return true; }else{ //不符合规则 alert('账户格式错误'); zhanghao.value = ""; zhanghao.focus(); return false; } } function checkPassword() { var password = document.getElementById("psw"); if((password.value).length!=8){ alert('密码应为8位英文或数字'); userpsw.focus(); return false; } } function checkId() { var id = document.getElementById("xuehao").value; var spanNode = document.getElementById("xuehaospan"); //学号的正确形式 // var reg = /^[0-9a-zA-Z]{6}/; if((id<"20180000")|| (id>"20189999")) { spanNode.innerHTML = "学号格式不正确,请重新填写!".fontcolor("red"); return false; } else{ //不符合规则 spanNode.innerHTML = "".fontcolor("green"); return true; } } </script> <script type="text/javascript"> function chack1() { var zhanghu1=document.getElementById("zhanghu"); var psw1=document.getElementById("psw"); var xuehao1=document.getElementById("xuehao"); var xi=document.getElementById("xi"); var class1=document.getElementById("class1"); var year=document.getElementById("year"); var sheng=document.getElementById("sheng"); var beizhu=document.getElementById("beizhu"); var zhanghu=zhanghu1.value; var xuehao=xuehao1.value; var psw=pse1.value; var zhi; var xue; var wen; wen=xuehao.value.length(); xue=zhanghu.value.length(); zhi=psw.value.length(); if(xue<6||xue>12) { alert("账号位数错误"); zhanghu.focus(); return false; } for(var a=0;a<xue;a++) { if(zhanghu.charAt[a]="_") { alert("账号类型错误"); zhanghu.focus(); return false; } if(zhanghu.value.charAt[a]>=48&&zhanghu.charAt[a]<=57) { continue; } if(zhanghu.charAt[a]>=65&&zhanghu.charAt[a]<=90) { continue; } if(zhanghu.charAt[a]>=97&&zhanghu.charAt[a]<=122) { continue; } alert("账号类型错误"); zhanghu.focus(); return false; } if(zhi<8) { alert("密码位数错误"); psw.focus(); return false; } for(var b=0;b<zhi;b++) { if(psw.charAt[b]>=48&&psw.charAt[b]<=57) { continue; } if(psw.charAt[b]>=65&&psw.charAt[b]<=90) { continue; } if(psw.charAt[b]>=97&&psw.charAt[b]<=122) { continue; } alert("密码类型错误"); psw.focus(); return false; } if(wen!=8) { alert("学号位数错误"); xuehao.focus(); return false; } if(xuehao.charAt[0]!=2||xuehao.charAt[1]!=0||xuehao.charAt[2]!=1||xuehao.charAt[3]!=8) { alert("学号开头不是2018"); xuehao.focus(); return false; } } } </script> </form> </body> </html>