jsp10

 1 package dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import util.DbUtil;
 9 import entity.Stu;
10 
11 public class StudentDao {
12     public void stuReg(Stu stu) {
13         Connection con = DbUtil.getCon();
14         try {
15             PreparedStatement pred = con
16                     .prepareStatement("insert into stu(uname,password,age)value ('"
17                             + stau.getUname()
18                             + "','"
19                             + stu.getPassword()
20                             + "','" + stu.getAge() + "')");
21             pred.executeUpdate();
22             pred.close();
23         } catch (SQLException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }
27         try {
28             con.close();
29         } catch (SQLException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         }
33 
34     }
35 
36     public Stu getStudentByName(String uname) {
37 
38         Connection con = null;
39         PreparedStatement pred = null;
40         ResultSet res = null;
41         con = DbUtil.getCon();
42         try {
43             pred = con.prepareStatement("select * from stu where uname='"
44                     + uname + "'");
45             res = pred.executeQuery();
46             Stu stu=null;
47             if (res.next()) {
48                  stu = new Stu(res.getString("uname"),
49                         res.getString("password"), res.getInt("age"));
50             }
51             return stu;
52         } catch (SQLException e) {
53             // TODO Auto-generated catch block
54             e.printStackTrace();
55         } finally {
56             try {
57                 res.close();
58             } catch (SQLException e) {
59                 // TODO Auto-generated catch block
60                 e.printStackTrace();
61             }
62             try {
63                 pred.close();
64             } catch (SQLException e) {
65                 // TODO Auto-generated catch block
66                 e.printStackTrace();
67             }
68             try {
69                 con.close();
70             } catch (SQLException e) {
71                 // TODO Auto-generated catch block
72                 e.printStackTrace();
73             }
74         }
75 
76         return null;
77     }
78 }
 1 package entity;
 2 
 3 public class Stu {
 4     private int stuId;
 5     private String uname;
 6     private String password;
 7     private int age;
 8 
 9     public Stu() {
10         super();
11     }
12 
13     public Stu(String uname, String password, int age) {
14         super();
15         this.uname = uname;
16         this.password = password;
17         this.age = age;
18     }
19 
20     public int getStuId() {
21         return stuId;
22     }
23 
24     public void setStuId(int stuId) {
25         this.stuId = stuId;
26     }
27 
28     public String getUname() {
29         return uname;
30     }
31 
32     public void setUname(String uname) {
33         this.uname = uname;
34     }
35 
36     public String getPassword() {
37         return password;
38     }
39 
40     public void setPassword(String password) {
41         this.password = password;
42     }
43 
44     public int getAge() {
45         return age;
46     }
47 
48     public void setAge(int age) {
49         this.age = age;
50     }
51 
52 }
 1 package util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 public class DbUtil {
 8     static {
 9 
10         try {
11             Class.forName("com.mysql.jdbc.Driver");
12         } catch (ClassNotFoundException e) {
13             // TODO Auto-generated catch block
14             e.printStackTrace();
15         }
16     }
17 
18     public static Connection getCon(){
19         Connection con = null;
20         try {
21             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
22                     "root", "123456");
23         } catch (SQLException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }
27         return con;
28     }
29 }
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5   <head>  
 6     <title></title>
 7   </head>
 8   
 9   <body> 
10   <form  name="formName"  action="doLogin.jsp" method="post">
11     用户名:<input type="text" name="uname"><br> 
12    密码: <input type="password"name="password"><br> 
13    <input type="button" value="登录"onclick="toDoLogin()">
14    <input type="button"value="注册" onclick="toReg()">
15    
16    </form>
17    <script>
18    function toReg(){
19    window.location.href="reg.jsp";
20    
21    }
22    function toDoLogin(){
23    if(formName.uname.value==""||formName.password.value==""){
24    alert("用户名或密码不能为空");
25    
26    }else{formName.submit();
27    
28    }
29    
30    }
31    </script>
32   </body>
33 </html>
 1 <%@page import="entity.Stu"%>
 2 <%@page import="dao.StudentDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML>
 6 <html>
 7 <head>
 8 <title></title>
 9 </head>
10 
11 <body>
12     <%
13         request.setCharacterEncoding("UTF-8");
14         response.setCharacterEncoding("UTF-8");
15         String uname = request.getParameter("uname");
16         String password = request.getParameter("password");
17         StudentDao studentDao = new StudentDao();
18         Stu stu=studentDao.getStudentByName(uname);
19         if(password.equals(stu.getPassword())){
20         response.sendRedirect("index.jsp");
21         }else{
22         response.sendRedirect("login.jsp");
23         }
24     %>
25     
26 </body>
27 </html>
 1 <%@page import="dao.StudentDao"%>
 2 <%@page import="entity.Stu"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML>
 6 <html>
 7   <head>  
 8     <title></title>
 9   </head>
10   
11   <body>
12     <%
13     request.setCharacterEncoding("UTF-8");
14     response.setCharacterEncoding("UTF-8");
15     String uname = request.getParameter("uname");
16     String password = request.getParameter("password");
17     Integer age = Integer.parseInt(request.getParameter("age"));
18     Stu stu = new Stu(uname,password,age);
19     StudentDao studentDao = new StudentDao();
20     studentDao.stuReg(stu);
21     response.sendRedirect("login.jsp");
22      %>
23   </body>
24 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5   <head>  
 6     <title>注册</title>
 7   </head>
 8   
 9   <body> 
10    <form action="doreg.jsp" method="post">
11    用户名:<input type="text" name="uname"><br>
12    密码: <input type="password" name="password"><br>
13    年龄:<input type="text" name="age"><br>
14    <input type="submit" value="提交">
15    </form>
16   </body>
17 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5   <head> 
 6     <title>首页</title>
 7   </head>
 8   
 9   <body>
10     登陆成功 <br>
11   </body>
12 </html>

 

posted @ 2022-05-07 15:33  计算机1905geng  阅读(27)  评论(0编辑  收藏  举报