1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接

package com.gd.entity;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class BaseDao {
 
 
    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false","root","123456");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
    }   
     
 
     
     
    //关闭连接
    protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){       
    try {
        if(rs != null)
            rs.close();
        if(ps != null)
            ps.close();
        if(con != null)
            con.close();
         
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
     
}
package com.gd.entity;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class StuDao extends BaseDao {
 
    public boolean login(String name, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }
 
    public void reg(String uname, String upwd) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(uname,upwd) values(?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
 
    }
 
}
<body>
        <script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd1.value==""){
                alert("密码不能为空!");
                return;
            }
            if(loginForm.upwd1.value!=loginForm.upwd2.value){
                alert("密码不一致,请重新输入!");
                return;
            }
            loginForm.submit();
        }
    </script>
 
 
    <form name="loginForm" action="dozhuce.jsp" method="post">
         
    用户名:<input type="text" name="uname" ><br>
    密码: <input  type="password" name="upwd1"  ><br>
    确认密码: <input  type="password" name="upwd2"  >
        <input type="button" value="注册" onClick="validate()">   
 
 
    </form>
  </body>
 
 
<body>
        <%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd1");
    sd.reg(uname, upwd);
    session.setAttribute("uname", uname);
    out.print("注册失败,即将跳回登陆页.....");
    request.getRequestDispatcher("index.jsp").forward(request, response);
 
 %>
  </body>
<body>
        <script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
 
 
    <form name="loginForm" action="dologin.jsp" method="post">
         
    用户名:<input type="text" name="uname" ><br>
    密码: <input  type="password" name="upwd"  >
     
        <input type="button" value="登录" onClick="validate()">   
 
 
    </form>
  </body>
 
 
<body>
        <%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    if (sd.login(uname, upwd)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else{
        out.print("登陆失败,即将跳回登陆页.....");
        response.setHeader("refresh", "3;url=index.jsp");
    }
 
 
 %>
  </body>
 
 
<body>
<%
    request.setCharacterEncoding("utf-8");
    String uname=(String)session.getAttribute("uname");
 %>
登录成功!欢迎你<%=uname %>  
  </body>

 

 posted on 2022-05-08 17:40  莉莉娅king  阅读(7)  评论(0编辑  收藏  举报