jsp第七次作业

用户表:  uid (主键,自动增长)   uname  upwd

使用分层实现注册。(必做)

使用分层实现登录。(选做)

stu.java

package com.xz.beans;

public class stu {
    private int id;
    private String uname;
    private String upwd;
    //访问器
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
    //构造器
    public stu(int id, String uname, String upwd) {
        super();
        this.id = id;
        this.uname = uname;
        this.upwd = upwd;
    }
    public stu() {
        super();
    }
}

 


stuDao.java

package com.xz.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class stuDao {
    //用户数据访问类
    
        //添加用户
        public int addBean(stuDao b){
            int i=0;
            
            try {
                //加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //建立连接
                Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/homweb", "root", "root");            
                //写SQL语句
                String sql="insert into boss(uname,upwd) values(?,?)";            
                //执行
                PreparedStatement ps=con.prepareStatement(sql);
                //ps.setInt(1, b.getId());
                ps.setString(1, b.getUname());
                ps.setString(2, b.getUpwd());
                i=ps.executeUpdate();
                
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return i;
        }//登录
        private String getUpwd() {
            // TODO Auto-generated method stub
            return null;
        }
        private String getUname() {
        // TODO Auto-generated method stub
        return null;
    }
        public int denBean(stuDao b){
            int i=0;
           ResultSet rs =null;
            try {
                //加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //建立连接
                Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/homweb", "root", "root");            
                //写SQL语句
                String sql="select uname,upwd from boss where uname=? and upwd=?";            
                //执行
                PreparedStatement ps=con.prepareStatement(sql);
                //ps.setInt(1, b.getId());
                ps.setString(1, b.getUname());
                ps.setString(2, b.getUpwd());
                rs = ps.executeQuery();
                if(rs.next()){
                    System.out.print("用户登录成功");
                    i=1;
                }else{
                    System.out.print("用户登录失败");
                    i=0;
                }
                
                rs.close();
                ps.close();
                con.close();
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return i;
        }
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body>
    <h2 align="center">
        <font color=red>用户登录页面</font>
    </h2>
    <form action="success.jsp" method="post">
        <table align="center" border="1">
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td>密&nbsp;&nbsp;码:</td>
                <td><input type="text" name="password">
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="登录" name="login">
                </td>
                <td><input type="reset" value="重置" name="reset">
                </td>
            </tr>
        </table>
        <p align="center">
            <a href="register.jsp" color=blue>注册用户</a>
        </p>
    </form>

</body>
</html>

register.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 align="center">
    <h2>新用户注册</h2>
    <form action="seccess.jsp" method="post">
        <table align="center">
            <tr align="center">
                 <td>请输入用户名:</td>
                <td><input type="text" name=name autofocus="autofocus"></td>
            </tr>
            <tr align="right">
                <td>请输入密码:</td>
                <td><input type="text" name=password></td>
            </tr>
            <tr align="right">
                <td>请输入确认密码:</td>
                <td><input type="text" name=refill></td>
            </tr>
        </table>
            <input type="submit" name=register value="注册" >
            <input type="reset" name=refill value="重填" >
        </form>
     
        
</body>
</html>

判断是否成功

<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断登录</title>
</head>
<body>
    <%
        Context ctx = null;
        DataSource ds = null;
        Statement stmt =null;
        ResultSet rs = null;
        Connection con = null;
        String name = request.getParameter("name").trim();
        String password = request.getParameter("password").trim();
        try{
            ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时自己创建的名字  语句功能找到配置的数据库
            con = ds.getConnection();//创建数据库连接
            stmt = con.createStatement();
            rs=stmt.executeQuery("select * from user where name='"+name+"'");
            if(rs.next()){
                rs=stmt.executeQuery("select * from user where name='"+name+"' and password='"+password+"'");
                if(rs.next()){
                    out.print(name+"登录成功");
                }else{
                    out.print("密码输入错误!!!<br>"+"重新<a href=\"login.jsp\">登录</a>");
                }
            }else{
                out.print("<font color=red>"+name+"</font>用户不存在!!!<br>"+"请点击<a href=\"register.jsp\">注册</a>");
            }
        }catch(Exception e){
            out.print(e);
        }finally{
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
    %>
    
</body>
</html>

posted @ 2021-04-18 18:08  拾掇拾掇  阅读(36)  评论(0编辑  收藏  举报