JAVAWEB-注册页面设计
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html > 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>注册页面</title> 8 <script type="text/javascript"> 9 function check(form) { 10 with (form) { 11 if (username.value == "") { 12 alert("用户名不能为空"); 13 return false; 14 } else if (password.value == "" || password1.value == "") { 15 alert("密码不能为空"); 16 return false; 17 } else 18 return true; 19 } 20 } 21 </script> 22 <style> //**********************************网页格式设置**************************************** 23 .a { 24 border: 2px solid #a1a1a1; 25 padding: 10px 40px; 26 /* background:#dddddd; */ 27 width: px; 28 border-radius: 25px; 29 } 30 #bk { 31 border: 3px solid #a1a1a1; 32 padding: 10px 60px; 33 /* background:#dddddd; */ 34 width: px; 35 border-radius: 25px; 36 } 37 .button { 38 background-color: #ff8433; 39 border: none; 40 color: white; 41 padding: 5px 10px; 42 text-decoration: none; 43 display: inline-block; 44 font-size: 16px; 45 margin: 2px 2px; 46 cursor: pointer; 47 } 48 table{ 49 margin: auto; 50 } 51 </style> 52 </head> 53 <body> 54 <br> 55 <br> 56 <br> 57 <h3 align="center">注册页面</h3> 58 <hr> 59 <form action="Rservlet" method="post" 60 onsubmit="return check(this);"> 61 <div style="text-align: center;"> 62 <table> 63 <tr> 64 <td>用户名</td> 65 <td><input id="bk" type="text" name="username" placeholder="6-12字母数字|字母开头" ></td> 66 </tr> 67 <tr> 68 <td>密码</td> 69 <td><input id="bk" type="password" name="password"placeholder="由8位及以上英文和数字组成"></td> 70 </tr> 71 <tr> 72 <td>性别</td> 73 <td><input type="radio" name="sex" value="男">男 74 <input type="radio" name="sex" value="女">女</td> 75 </tr> 76 <tr> 77 <td>姓名</td> 78 <td><input id="bk" type="text" name="name"></td> 79 </tr> 80 <tr> 81 <td>学号</td> 82 <td><input id="bk" type="text" name="stunumber"></td> 83 </tr> 84 <tr> 85 <td>邮箱</td> 86 <td><input id="bk" type="text" name="email"></td> 87 </tr> 88 <tr> 89 <td>所在学院</td> 90 <td><input id="bk" type="text" name="academy"></td> 91 </tr> 92 <tr> 93 <td>所在系</td> 94 <td><input id="bk" type="text" name="department"></td> 95 </tr> 96 <tr> 97 <td>所在班级</td> 98 <td><input id="bk" type="text" name="classx"></td> 99 </tr> 100 <tr> 101 <td>入学年份</td> 102 <td><input id="bk" type="text" name="timeschool"></td> 103 </tr> 104 <tr> 105 <td>生源地</td> 106 <td><input id="bk" type="text" name="stuplace"></td> 107 </tr> 108 <tr> 109 <td>备注</td> 110 <td><input id="bk" type="text" name="remark"></td> 111 </tr> 112 <tr> 113 <td></td> 114 <td><input class ="button" type="submit" value="添加"></td> 115 </tr> 116 </table> 117 </div> 118 </form> 119 </body> 120 </html>
1 package util; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 public class DBUtil { 8 9 private static String URL = "jdbc:mysql://localhost:3306/a?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"; 10 11 private static String UserName = "root"; 12 13 private static String Password ="root"; 14 public static Connection getConnection() { 15 Connection Conn=null; 16 17 try { 18 19 Class.forName("com.mysql.cj.jdbc.Driver"); 20 21 System.out.println("鍔犺浇椹卞姩鎴愬姛!!!"); 22 } catch (ClassNotFoundException e) { 23 // TODO: handle exception 24 e.printStackTrace(); 25 } 26 27 try { 28 29 Conn = DriverManager.getConnection(URL,UserName,Password); 30 System.out.println("杩炴帴鏁版嵁搴撴垚鍔�!!!"); 31 32 //return Conn; 33 34 } catch (SQLException e) { 35 // TODO: handle exception 36 e.printStackTrace(); 37 } 38 return Conn; 39 } 40 public static void main(String[] args)throws SQLException { 41 Connection conn = getConnection(); 42 PreparedStatement pstmt = null; 43 ResultSet rs = null; 44 String sql ="select * from user"; 45 pstmt = conn.prepareStatement(sql); 46 rs = pstmt.executeQuery(); 47 System.out.println(getConnection()); 48 while(rs.next()){ 49 System.out.println("绌�"); 50 } 51 52 } 53 54 }
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.regex.Matcher; import java.util.regex.Pattern; 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.servlet.http.HttpSession; import dao.UserDao; import service.User; import util.DBUtil; /** * Servlet implementation class Rservlet */ @WebServlet("/Rservlet") public class Rservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Rservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); String name = request.getParameter("name"); String stunumber= request.getParameter("stunumber"); String email = request.getParameter("email"); String academy = request.getParameter("academy"); String department = request.getParameter("department"); String classx = request.getParameter("classx"); String timeschool = request.getParameter("timeschool"); String stuplace = request.getParameter("stuplace"); String remark = request.getParameter("remark"); System.out.println(username); System.out.println(password); Pattern p = null; Pattern s = null; Pattern t = null; Pattern stunum=null; Matcher m = null; Matcher n = null; Matcher y = null; Matcher snum=null; boolean dnum=false; boolean b = false; boolean c = false; boolean u = false; stunum=Pattern.compile("2018[0-9]{4}"); snum=stunum.matcher(stunumber); dnum=snum.matches(); p = Pattern.compile("^[A-Za-z0-9]{8,}$"); m = p.matcher(password); b = m.matches(); s= Pattern.compile("^[A-Za-z][A-Za-z0-9]{5,11}$") ; n = s.matcher(username); c =n.matches(); t= Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$") ; y = t.matcher(email); u =y.matches(); if(!c){ PrintWriter out = response.getWriter(); out.print("<script>alert('用户名错误!');window.history.go(-1)</script>"); }else if(!b) { PrintWriter out = response.getWriter(); out.print("<script>alert('密码格式错误!');window.history.go(-1)</script>"); }else if(!dnum) { PrintWriter out = response.getWriter(); out.print("<script>alert('学号格式错误!');window.history.go(-1)</script>"); } else if(!u){ PrintWriter out = response.getWriter(); out.print("<script>alert('电子邮箱格式错误!');window.history.go(-1)</script>"); }else { Connection conn = DBUtil.getConnection(); Statement state = null; String sql="select username from user where username='"+username+"'"; try { state = conn.createStatement(); ResultSet a= state.executeQuery(sql); if(a.next()) { PrintWriter out = response.getWriter(); out.println("<script>alert('用户名重复!');</script>"); out.println("<script>window.history.go(-1)</script>"); }else { User us=new User(); us.setPassword(password); us.setUsername(username); us.setSex(sex); us.setName(name); us.setStunumber(stunumber); us.setEmail(email); us.setAcademy(academy); us.setDepartment(department); us.setClassx(classx); us.setTimeshool(timeschool); us.setStuplace(stuplace); us.setRemark(remark); UserDao user = new UserDao(); user.addu(us); PrintWriter out = response.getWriter(); out.println("<script>alert('添加成功!');</script>"); //out.println("<script>window.history.go(-1)</script>"); request.getRequestDispatcher("Register.jsp").forward(request, response); } }catch(Exception e) { e.printStackTrace(); } } } }
1 package service; 2 3 public class User { 4 private String username; 5 private String password; 6 private String sex; 7 private String name; 8 private String stunumber; 9 private String email; 10 private String academy; 11 private String department; 12 private String classx; 13 private String timeschool; 14 private String stuplace; 15 private String remark; 16 17 public String getRemark() { 18 return remark; 19 } 20 public void setRemark(String remark) { 21 this.remark = remark; 22 } 23 public String getTimeschool() { 24 return timeschool; 25 } 26 public void setTimeshool(String timeshool) { 27 this.timeschool = timeshool; 28 } 29 public String getClassx() { 30 return classx; 31 } 32 public void setClassx(String classx) { 33 this.classx = classx; 34 } 35 public String getDepartment() { 36 return department; 37 } 38 public void setDepartment(String department) { 39 this.department = department; 40 } 41 public String getAcademy() { 42 return academy; 43 } 44 public void setAcademy(String academy) { 45 this.academy = academy; 46 } 47 public String getUsername() { 48 return username; 49 } 50 public void setUsername(String username) { 51 this.username = username; 52 } 53 public String getPassword() { 54 return password; 55 } 56 public void setPassword(String password) { 57 this.password = password; 58 } 59 public String getName() { 60 return name; 61 } 62 public void setName(String name) { 63 this.name = name; 64 } 65 public String getSex() { 66 return sex; 67 } 68 public void setSex(String sex) { 69 this.sex = sex; 70 } 71 public String getStunumber() { 72 return stunumber; 73 } 74 public void setStunumber(String stunumber) { 75 this.stunumber = stunumber; 76 } 77 public String getStuplace() { 78 return stuplace; 79 } 80 public void setStuplace(String stuplace) { 81 this.stuplace = stuplace; 82 } 83 public String getEmail() { 84 return email; 85 } 86 public void setEmail(String email) { 87 this.email = email; 88 } 89 }