第十周作业

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
    <form name="formName" action="dologin.jsp">
         用户名:<input type="text" name="uname"><br> 密码:<input
             type="text" name="password"><br>

        <input type="button" value="登录" onclick="Login()">
       <input type="button" value="注册" onclick="Zhuce()">
     </form>
     <script type="text/javascript">
         function Zhuce() {
             window.location.href = "zhuce.jsp";
         }
         function Login() {
             formName.submit();
         }
    </script>
     <br>
 </body>
 </html>
<%@page import="zuoye6.1.user"%>
  <%@page import="zuoye6.1.user1"%>
  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <!DOCTYPE HTML>
  <html>
  <head>
  <meta charset="utf-8">
 </head>
 <body>
     <%
         String uname = request.getParameter("uname");
         String password = request.getParameter("password");
         UserDao userDao = new UserDao();
         User user = userDao.getUserByUname(uname);
         if (password.equals(user.getPassword())) {
             session.setAttribute("uname", uname);
             request.getRequestDispatcher("main.jsp").forward(
                     request, response);
         } else {
     %>
     <script type="text/javascript">
         alert("登录失败");
     </script>
     <%
         request.getRequestDispatcher("login.jsp").forward(request,response);
         }
     %>
 </body>
 </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <!DOCTYPE HTML>
  <html>
  <head>
  <meta charset="utf-8">
  </head> 
  <body>
     <form name="regForm" action="submit.jsp">
         用户名:<input type="text" name="uname"><br> 密码:<input
             type="password" name="password"><br> 确认密码:<input
             type="password"><br> 年龄:<input type="text" name="age"><br>
     </form>
    <button onclick="reg()">登录</button>
     <script type="text/javascript">
         function reg() {
             regForm.submit();
        }
     </script>
     <br>
 </body>
 </html>
<%@page import="zuoye6.1.user"%>
  <%@page import="zuoye6.1.user1"%>
  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  
  <!DOCTYPE HTML>
  <html>
  <head>
  <meta charset="utf-8">
  <title>My JSP 'index.jsp' starting page</title>
 </head> 
 <body>
     <%
         request.setCharacterEncoding("utf-8");
         response.setCharacterEncoding("utf-8");
         String uname = request.getParameter("uname");
         String password = request.getParameter("password");
         Integer age = Integer.parseInt(request.getParameter("age"));
         User user = new User(uname, password, age);
         UserDao userDao = new UserDao();
         userDao.regUser(user);
         response.sendRedirect("login.jsp");
     %>
 </body>
 </html>
package zuoye6.1;

public class user{
    int id;
    String name;
    String password;
    Integer age;
    public user(String uname, String password, Integer age) {
         super();
         this.uname = uname;
         this.password = password;
         this.age = age;
       }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    } 
    
}
package zuoye6.1;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  public class user2 {
      static {
          try {
             Class.forName("com.mysql.jdbc.Driver");
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         }
     }
 
     public static Connection getCon() {
         Connection con = null;
         try {
             con = DriverManager.getConnection(
                     "jdbc:mysql://localhost:3306/test", "root", "123456");
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return con;
     }
 }
package zuoye6.1;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException; 
  import javax.annotation.PreDestroy;
  
 import zuoye6.1.user;
 import zuoye6.1.user2;
 public class user1 {
     public void regUser(User user) {
         String sql = "insert into stu(uname,password,age) value(?,?,?)";
         Connection con = DbUtil.getCon();
        try {
            PreparedStatement pred = con.prepareStatement(sql);
            pred.setString(1, user.getUname());
             pred.setString(2, user.getPassword());
             pred.setInt(3, user.getAge());
             pred.executeUpdate();
             pred.close();
         } catch (SQLException e) {
             e.printStackTrace();
         } finally {
             try {
                con.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }

     }

    public User getUserByUname(String uname) {
        User user = new User();
         String sql = "select * from stu where uname = ?";
         Connection con = DbUtil.getCon();
         try {
             PreparedStatement pred = con.prepareStatement(sql);
             pred.setString(1, uname);
            ResultSet res = pred.executeQuery();
             while (res.next()) {
                 user.setStuId(res.getInt("stuId"));
                 user.setUname(uname);
                user.setPassword(res.getString("password"));
                 user.setAge(res.getInt("age"));
             }
         } catch (SQLException e) {
             e.printStackTrace();
        }
         return user;
     }
 }

 

 

 

 

posted @ 2022-05-08 22:10  cccccccU-  阅读(5)  评论(0编辑  收藏  举报