JSP第十周作业
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接
login
dologin
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="com.sykjxy.dao.UserDao"%> 3 <%@page import="com.sykjxy.dao.EntityDao"%> 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title></title> 8 </head> 9 <body> 10 <% 11 String stuName = request.getParameter("stuName"); 12 String stuPwd = request.getParameter("stuPwd"); 13 UserDao userDao = new UserDao(); 14 EntityDao u = new EntityDao(); 15 u.setStuPwd(stuPwd); 16 %> 17 <% 18 if(stuPwd.equals(u.getStuPwd())){ 19 request.getRequestDispatcher("main.jsp").forward(request,response); 20 } else { 21 %> 22 <script type="text/javascript"> 23 alert("登录失败"); 24 </script> 25 <% 26 request.getRequestDispatcher("login.jsp").forward(request,response); 27 } 28 %> 29 30 </body> 31 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title></title> 7 </head> 8 <body> 9 <script type="text/javascript"> 10 function validate(){ 11 if(regForm.stuName.value == ""){ 12 alert("账号不能为空!"); 13 return; 14 } 15 if(regForm.stuPwd.value == ""){ 16 alert("密码不能为空!"); 17 return; 18 } 19 if(regForm.stuAge.value == ""){ 20 alert("年龄不能为空!"); 21 return; 22 } 23 regForm.submit(); 24 } 25 </script> 26 <form action="register-analyse.jsp" method="post" name="regForm"> 27 <table> 28 <tr> 29 <td>用户名:</td> 30 <td><input type="text" name="stuName"></td> 31 </tr> 32 <tr> 33 <td>密码:</td> 34 <td><input type="password" name="stuPwd"></td> 35 </tr> 36 <tr> 37 <td>年龄:</td> 38 <td><input type="text" name="stuAge"></td> 39 </tr> 40 </table> 41 <input type="button" value="提交" onclick="validate()"> 42 <input type="reset" value="重置"> 43 </form> 44 </body> 45 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ page import="com.sykjxy.dao.EntityDao"%> 3 <%@ page import="com.sykjxy.dao.UserDao"%> 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title></title> 8 </head> 9 <body> 10 <% 11 String stuName = request.getParameter("stuName"); 12 String stuPwd = request.getParameter("stuPwd"); 13 int stuAge = Integer.parseInt(request.getParameter("stuAge")); 14 15 EntityDao u = new EntityDao(); 16 u.setStuName(stuName); 17 u.setStuPwd(stuPwd); 18 u.setStuAge(stuAge); 19 20 UserDao user = new UserDao(); 21 int r = user.reg(u); 22 if (r >= 1) { 23 request.getRequestDispatcher("login.jsp").forward(request,response); 24 } 25 %> 26 </body> 27 </html>
1 package com.sykjxy.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class BaseDao { 10 public Connection conn = null; 11 PreparedStatement ps = null; 12 ResultSet rs = null; 13 14 //获取连接 15 protected Connection getConnection(){ 16 17 try { 18 //加载驱动 19 Class.forName("com.mysql.jdbc.Driver"); 20 //建立连接 21 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","123456"); 22 } catch (Exception e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 return conn; 27 } 28 29 //关闭连接 30 protected void closeAll(){ 31 try{ 32 if(rs != null) 33 rs.close(); 34 if(ps != null) 35 ps.close(); 36 if(conn != null) 37 conn.close(); 38 }catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 }
1 package com.sykjxy.dao; 2 3 public class EntityDao { 4 private Integer stuId; 5 private String stuName; 6 private String stuPwd; 7 private Integer stuAge; 8 9 public EntityDao() { 10 super(); 11 } 12 13 public EntityDao(Integer stuId, String stuName, String stuPwd, Integer stuAge) { 14 super(); 15 this.stuId = stuId; 16 this.stuName = stuName; 17 this.stuPwd = stuPwd; 18 this.stuAge = stuAge; 19 } 20 21 public Integer getStuId() { 22 return stuId; 23 } 24 25 public void setStuId(Integer stuId) { 26 this.stuId = stuId; 27 } 28 29 public String getStuName() { 30 return stuName; 31 } 32 33 public void setStuName(String stuName) { 34 this.stuName = stuName; 35 } 36 37 public String getStuPwd() { 38 return stuPwd; 39 } 40 41 public void setStuPwd(String stuPwd) { 42 this.stuPwd = stuPwd; 43 } 44 45 public Integer getStuAge() { 46 return stuAge; 47 } 48 49 public void setStuAge(Integer stuAge) { 50 this.stuAge = stuAge; 51 } 52 53 54 }
1 package com.sykjxy.dao; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 public class UserDao extends BaseDao { 7 8 public EntityDao log(String stuName,String StuPwd){ 9 EntityDao u = null; 10 try{ 11 Connection conn = getConnection(); 12 String sql = "select * from stu where stuName = ? and stuPwd = ?"; 13 ps = conn.prepareStatement(sql); 14 ps.setString(1, stuName); 15 ps.setString(2, StuPwd); 16 rs = ps.executeQuery(); 17 while(rs.next()) 18 u = new EntityDao(); 19 u.setStuId(rs.getInt(1)); 20 u.setStuName(rs.getString(2)); 21 u.setStuPwd(rs.getString(3)); 22 u.setStuAge(rs.getInt(4)); 23 }catch (SQLException e) { 24 e.printStackTrace(); 25 }finally{ 26 closeAll(); 27 } 28 return u; 29 } 30 31 public int reg(EntityDao u){ 32 int row = 0; 33 try{ 34 super.getConnection(); 35 String sql = "insert into stu(stuName,stuPwd,stuAge) value (?,?,?)"; 36 ps = conn.prepareStatement(sql); 37 ps.setString(1, u.getStuName()); 38 ps.setString(2,u.getStuPwd()); 39 ps.setInt(3, u.getStuAge()); 40 row = ps.executeUpdate(); 41 }catch (SQLException e) { 42 e.printStackTrace(); 43 }finally{ 44 super.closeAll(); 45 } 46 return row; 47 } 48 }




浙公网安备 33010602011771号