JSP第六次作业

1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据

2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在

3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx

4.若不存在,跳到登录页面。

登录页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>



<html>
<head>
    <title>登录页面</title>
</head>
<body style="background:url(image/why.png)">
    <form action="MyJsp.jsp" method="post">
    

      <div align="center">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="uname"  size="18"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="upwd"  size="19"></td>
            </tr>
           <tr>
            <td>  验证码: </td>
            <td> <input type="text" name="code" size="18"><br/> </td>
            <td> <img  src="num.jsp"  width="60px" height="40px" /></td>
            <td>  <a href="num.jsp" >换一张</a><br></td>
     
          </tr>
            <tr>
                <td colspan="2"  align="center">
                <input type="submit" value="登录">
                </td>
            </tr>
        </table>
        </div>
    </form>
</body>
</html>

 

 

连接数据库

 

<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.CallableStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>连接数据库/title>
</head>
<body>


    <%
        request.setCharacterEncoding("utf-8");
        String s1=request.getParameter("uname");
        String s2=request.getParameter("upwd");
        String s3 = (String)session.getAttribute("code");
        String DBDriver="com.mysql.jdbc.Driver";
        String DBUrl="jdbc:mysql://localhost:3306/homweb?useUnicode=true&characterEncoding=utf-8";
        String DBUser="root";
        String DBPass="root";
        boolean flag=false;
    %>
    <%
    try {
        Class.forName(DBDriver);
        Connection connection = DriverManager.getConnection(DBUrl, DBUser, DBPass);
        String sql="select uname,upwd from boss where uname=? and upwd=?";
        CallableStatement stmt = connection.prepareCall(sql);
        stmt.setString(1,s1);
        stmt.setString(2,s2);
        ResultSet rs = stmt.executeQuery();//查询语句
        //判断是否查询成功
        if (rs.next()){
            flag=true;
            
            System.out.println("用户登录成功");
        }
        rs.close();
        stmt.close();
        connection.close();

    }catch (Exception e){
        e.printStackTrace();
    }
    %>
    <%
        if (flag){      //登陆成功
            session.setAttribute("uname",s1);
            session.setAttribute("upwd",s2);
            //重定向到success
            //request.getContextPath()  
            response.sendRedirect(request.getContextPath()+"/seccess.jsp");

        }else{
            if (s1 ==""||s2==""){
                session.setAttribute("flag","用户名或密码不能为空");
                session.setAttribute("uname",s1);
                session.setAttribute("upwd",s2);
                response.sendRedirect(request.getContextPath()+"/fail.jsp");
            }else{
                session.setAttribute("flag","登陆失败请检查下面的信息");
                session.setAttribute("uname",s1);
                session.setAttribute("upwd",s2);
                response.sendRedirect(request.getContextPath()+"/fail.jsp");
            }

        }
    %>
 <%  

    String incode = (String)request.getParameter("code");   
    String rightcode = (String)session.getAttribute("rCode");
      
    if(incode != null && rightcode != null){  
        if(incode.equals(rightcode)){  
           session.setAttribute("code", incode);
    //       pageContext.forward("seccess.jsp");
            
        }else{  
            out.println("验证码输入不正确,请重新输入!");  
        }  
    }  
  %>  
</body>
</html>

 

数据库

 登录成功

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<style>
    h1{
        color: blue;
    }
    span{
        color: green;
    }
</style>
<script type="text/javascript">
    function zX(){
        
        sessionStorage.removeItem("uname");
        
        
        sessionStorage.removeItem("upwd");
        
        window.location.href = "index.jsp";
        
        
    }
 
 
 
</script>

<head>
    <title>welcome</title>
</head>

<body  bgcolor=#ffccff>
 
    <%
        // String s1 = (String)session.getAttribute("uname");
        //String s2 = (String)session.getAttribute("upwd"); 
        String s3 = (String)session.getAttribute("code");
        
    
        
    //    if(s1 != null){
            
            /* out.println("欢迎用户: " + s1 + "<br>");
            out.println(" 密码:      " + s2 + "<br>");
            out.println(" 验证码:  " + s3 + "<br>"); */
            
            /* out.println("<a href='logout.jsp'>" + "点击这里进行用户的注销" + "</a>"); */
            
            
    //    }else{
            
            
    //        out.println("您还没有进行登陆<br>");
            
    //        out.println("<a href='index.jsp'>" + "点击这里进行用户登陆" + "</a>");
            
    //    }   
    //    
    
    %>
 <div align="center">
    <h1>WECLOME</h1>
    <h1> 欢迎登录</h1>
    你输入的登录信息
        <table>
            <tr>
                <td width="80" height="20">用户名:</td>
                <td> <h2><span><%=request.getSession().getAttribute("uname")%></span></h2></td>
            </tr>
            <tr>
                <td height="20">密&nbsp;&nbsp;&nbsp;&nbsp;码:</td>
                <td> <h2><span><%=request.getSession().getAttribute("upwd")%></span></h2></td>
            </tr>
            <tr>
                <td height="20">验证码:</td>
                <td><%=s3%></td>
            </tr>
        </table>
        <button type="button" name="button1" value="退出" onClick="zX()">退出</button>
    </div>
    
</body>
</html>

 

 

   

 

 

 用户不存在

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<style>
    h1{
        color: red;
    }
    h2{
        color: blue;
    }
</style>
<head>
    <title>fail</title>
   
    <script type="text/javascript">
    function zX(){
        
        sessionStorage.removeItem("uname");
        
        
        sessionStorage.removeItem("upwd");
        
        window.location.href = "index.jsp";
        
        
    }
 
 
 
</script>
</head>
<body>
<div align="center">

    <h1>ERROR:<%=request.getSession().getAttribute("flag")%></h1>
    <h1>请检查信息</h1>
    <h2>用户名:<%=request.getSession().getAttribute("uname")%></h2>
    <h2>密码:<%=request.getSession().getAttribute("upwd")%></h2>
    <button type="button" name="button1" value="注销" onClick="zX()">注销</button>



</div>
</body>
</html>

 

     

 

posted @ 2021-04-12 18:36  wsw4  阅读(72)  评论(0)    收藏  举报