• jsp第七周作业


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

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

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

    1.index.jsp    登录页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
    
      <script type="text/javascript">
        function changeActionToRegister(){
          var s = document.getElementById("idForm")
          s.setAttribute("action", "doRegister.jsp");
        }
        function changeActionToLogin(){
          var s = document.getElementById("idForm")
          s.setAttribute("action", "doLogin.jsp");
        }
      </script>
    
      <body>
    
      <form id="idForm" method="post">
        用户名:<input type="text" name="username"/>
        <br>
        密码:<input type="password"  name="password"/>
        <br>
      <input type="submit" onclick="changeActionToLogin()" value="登录">
      <input type="submit" onclick="changeActionToRegister()" value="注册">
      </form>
    
      </body>
    </html>
    

      2.dolgoin.jsp   登录验证

    <%@ page import="com.bean.lty.UserBean" %>
    <%@ page import="com.dao.lty.Dao" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%
            UserBean userBean = new UserBean();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            userBean.setUname(username);
            userBean.setUpassword(password);
            Dao dao = new Dao();
            int i = dao.UserLogin(userBean);
            if(i > 0){
                HttpSession session1 = request.getSession();
                session1.setAttribute("username",username);
                session1.setAttribute("password",password);
                request.getRequestDispatcher("success.jsp").forward(request,response);
            }else{
                response.sendRedirect("fail.jsp");
            }
    
        %>
    </body>
    </html>
    

      2.注册验证

    <%@ page import="com.bean.lty.UserBean" %>
    <%@ page import="com.dao.lty.Dao" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    <%
        UserBean userBean = new UserBean();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        userBean.setUname(username);
        userBean.setUpassword(password);
        Dao dao = new Dao();
        int i = dao.UesrRegister(userBean);
        if(i > 0){
            HttpSession session1 = request.getSession();
            session1.setAttribute("username",username);
            session1.setAttribute("password",password);
            request.getRequestDispatcher("ok.jsp").forward(request,response);
        }else{
            response.sendRedirect("error.jsp");
        }
    
    %>
    
    </body>
    </html>
    

      实体类代码

    package com.bean.lty;
    
    public class UserBean {
    
        private String uname;
        private String upassword;
    
        public UserBean() {
    
        }
    
        public UserBean(String uname, String upassword) {
            this.uname = uname;
            this.upassword = upassword;
        }
    
        public String getUname() {
            return uname;
        }
    
        public void setUname(String uname) {
            this.uname = uname;
        }
    
        public String getUpassword() {
            return upassword;
        }
    
        public void setUpassword(String upassword) {
            this.upassword = upassword;
        }
    }
    

      数据访问层

    package com.dao.lty;
    
    import com.bean.lty.UserBean;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Dao {
    
        UserBean userBean = new UserBean();
    
        // 获取连接
        protected static Connection getConnection() throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/user_info?useUnicode=true&characterEncoding=utf8", "root", "root");
    
            return conn;
        }
    
        // 关闭连接
        public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (conn != null)
                conn.close();
        }
    
    
        public int UesrRegister(UserBean userBean) throws Exception {
                int i = 0;
                Connection conn = getConnection();
                String sql = "insert into user(uname,upassword) values(?,?)";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, userBean.getUname());
                ps.setString(2, userBean.getUpassword());
                return i = ps.executeUpdate();
        }
    
    
        public int UserLogin(UserBean userBean) throws Exception {
            int flag = 0;
            Connection conn = getConnection();
            String sql = "select * from user where uname = ? and upassword = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, userBean.getUname());
            ps.setString(2, userBean.getUpassword());
            ResultSet rs = ps.executeQuery();
    
            while (rs.next() == true){
                flag = 1;
            }
    
            return flag;
        }
    }
    

      注册成功页面以及失败页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    您已成功注册!注册信息:用户名:<%=request.getSession().getAttribute("username")%>
    密码:<%=request.getSession().getAttribute("password")%>
    </body>
    </html>
    

      

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    注册信息不正确,请重新进行注册!!!
    </body>
    </html>
    

      登录成功页面以及失败页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    登录成功!欢迎您,用户名:<%=request.getSession().getAttribute("username")%>
    密码:<%=request.getSession().getAttribute("password")%>
    </body>
    </html>
    

      

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    登录失败,用户名不存在!
    </body>
    </html>
    

      

  • 相关阅读:
    处理字符串拼接成想要的数组
    json 压缩中文不转码
    MySQL主从同步机制及同步中的问题处理
    mysql主从复制亲测,以及注意事项
    windows下mysql和linux下mysql主从配置
    拯救U盘之——轻松修复U盘“无法访问”的故障
    MUI框架开发HTML5手机APP(一)--搭建第一个手机APP
    UI之富文本编辑器-UEditor
    弹性布局详解——5个div让你学会弹性布局
    内置函数:min 用法
  • 原文地址:https://www.cnblogs.com/Zzzhqh/p/14675556.html
Copyright © 2020-2023  润新知