zoukankan      html  css  js  c++  java
  • jsp第七次作业

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

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

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

    1.Stu.java

    package com.bean;
    
    public class Stu {
        private int sid;
        private String uname;
        private String upwd;
        public int getSid() {
            return sid;
        }
        public void setSid(int sid) {
            this.sid = sid;
        }
        public String getUname() {
            return uname;
        }
        public void setUname(String name) {
            this.uname = uname;
        }
        public String getUpwd() {
            return upwd;
        }
        public void setUpwd(String upwd) {
            this.upwd = upwd;
        }
        public Stu(int sid, String name, String upwd) {
            super();
            this.sid = sid;
            this.uname =uname;
            this.upwd = upwd;
        }
        public Stu() {
            super();
        }
        
    
    }

    2.StuDao.java

    package com.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import com.bean.Stu;
    
    
    public class StuDao {
        //学生数据访问类
        
            //添加学生
            public int addStu(Stu s){
                int i=0;
                
                try {
                    //加载驱动
                    Class.forName("com.mysql.jdbc.Driver");
                    //建立连接
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");            
                    //写SQL语句
                    String sql="insert into stu values(?,?,?)";            
                    //执行
                    PreparedStatement ps=con.prepareStatement(sql);
                    ps.setInt(1, s.getSid());
                    ps.setString(2, s.getUname());
                    ps.setString(3, s.getUpwd());
                    i=ps.executeUpdate();
                    
                    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return i;
                
                
                
                
                
                
                
            }
            
            //登录
            public int dlStu(Stu s){
                int i=0;
                ResultSet rs =null;
                try {
                    //加载驱动
                    Class.forName("com.mysql.jdbc.Driver");
                    //建立连接
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");            
                    //写SQL语句
                    String sql="select uname,upwd from stu where uname=? and upwd=?";            
                    //执行
                    PreparedStatement ps=con.prepareStatement(sql);
                    ps.setInt(1, s.getSid());
                    ps.setString(2, s.getUname());
                    ps.setString(3, s.getUpwd());
                    i=ps.executeUpdate();
                    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;
                
                
                
                
                
                
                
            }
           
    
    }

    3.登录界面index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <form name="control.jsp"  method="post" action="" >
            <table>
                <tr>
                <td>编号:</td>
                <td><input type="text" name="sid" id="userNmae"></td>
                </tr>
                <tr>
                <td>用户名:</td>
                <td><input type="text" name="uname" id="userNmae"></td>
                </tr>
                <tr>
                <td>输入密码:</td>
                <td><input type="password" name="upwd" id="pwd"></td>
                </tr>
                <tr>
                    <td colspan="2"  align="center">
                    <input type="submit" value="登录">&nbsp;&nbsp;&nbsp;<button type="button" name="button2" value="注册" onClick="zhuce.jsp">注册</button>
                    </td>
                </tr>
                
            </table>
        </form>
      </body>
    </html>

    4.注册界面zhuce.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      
        
        <title>My JSP 'zhuce.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <form name="zccontrol.jsp"  method="post" >
            <table>
                <tr>
                <td>用户名:</td>
                <td><input type="text" name="uname" id="userNmae"></td>
                </tr>
                <tr>
                <td>输入密码:</td>
                <td><input type="text" name="upwd" id="pwd"></td>
                </tr>
                <tr>
                <td>请再次确认密码:</td>
                <td><input type="text" name="upwd" id="userNmae"></td>
                </tr>
                <tr>
                <td colspan="2"><input type="submit" value="注册"></td>
                </tr>
                
            </table>
        </form>
      </body>
    </html>

    5.登陆成功dlcg.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'zccg.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <%
            String s1 = (String) session.getAttribute("sid");
            String s2 = (String) session.getAttribute("uname");
            String s3 = (String) session.getAttribute("upwd");
        %>
        <div align="center">
            <h1>WECLOME</h1>
            <h1>欢迎登录</h1>
            你输入的登录信息
            <table>
                <tr>
                    <td>编号:</td>
                    <td>
                        <h2>
                            <span><%=s1%></span>
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>用户名:</td>
                    <td>
                        <h2>
                            <span><%=s2%></span>
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><%=s3%></td>
                </tr>
            </table>
    
        </div>
    </body>
    </html>

    6.注册成功zhuce.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'zccg.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <h1>注册成功</h1>
        <button type="button" name="button1" value="退出" onClick="index.jsp">返回登录</button>
      </body>
    </html>

    7.control.jsp

    <%@page import="com.dao.StuDao"%>
    <%@page import="com.bean.Stu"%>
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
        Stu s = new Stu();
        int sid = Integer.parseInt(request.getParameter("sid"));
        s.setSid(sid);
        String uname = request.getParameter("uname");
        s.setUname(uname);
        String upwd = request.getParameter("upwd");
        s.setUpwd(upwd);
    
        StuDao sd = new StuDao();
        if (sd.dlStu(s) > 0) {
            //跳转登录成功页面
            session.setAttribute("id",sid);
            session.setAttribute("uname", uname);
            session.setAttribute("upwd", upwd);
            response.sendRedirect("dlcg.jsp");
        } else {
            //错误页面
            out.print("登陆失败");
        }
    %>

    8.zccontrol.jsp

    <%@page import="com.dao.StuDao"%>
    <%@page import="com.bean.Stu"%>
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    
        <%
        Stu s = new Stu();
        int id = Integer.parseInt(request.getParameter("sid"));
        s.setSid(id);
        String uname = request.getParameter("uname");
        s.setUname(uname);
        String upwd = request.getParameter("upwd");
        s.setUpwd(upwd);
    
        StuDao sd = new StuDao();
        if (sd.addStu(s) > 0) {
            //跳转注册成功页面
            session.setAttribute("uname", uname);
            session.setAttribute("upwd", upwd);
            
            response.sendRedirect(request.getContextPath() + "/zhuce.jsp");
        } else {
            out.print("注册失败!");
        }
    %>h

     

     

     

  • 相关阅读:
    常见浏览器兼容性问题与解决方案
    [WinForm] VS2010发布、打包安装程序
    前端和后台对时间数值的增减操作(JavaScript和C#两种方法)
    C#类的继承,方法的重载和覆写
    web.config设置和取值
    linux nginx安装
    java Arrays.asList()和Collections.addAll()
    mysql数据库乱码
    Mysql不区分大小写
    ueditor
  • 原文地址:https://www.cnblogs.com/FALEDA/p/14671881.html
Copyright © 2011-2022 走看看