JavaWeb

JavaWeb-020-Cookie自动登录

2017-11-29  本文已影响67人  53b3f4658edc

步骤

测试代码

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
        
        <script src="js/jquery-1.10.1.js" type="text/javascript" charset="utf-8"></script>
        <style type="text/css">
        </style>        
        <script type="text/javascript">
            $(function() {
                $("div:hidden").show(2000);
                $("input[type=submit]").css("width","30%");
            });
        </script>
        
    </head>
    
    <body>
        <form action="index.jsp" method="post">
        <div style="display:none">
        <table >
            <tr>
                <td>用户名:</td>
                <td><input type="text"  name="username" /></td>
            </tr>
            
            <tr align="center">
                <td colspan="2" height="80px">
                    <input type="submit" value="登录"/>
                </td>
            </tr>
        </table>
        </div>
        </form>
    </body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    
    <body>
        <%
            boolean isLog = false;
            //1.获取输入信息,如果有输入信息:存入Cookie,设置有效时间30s,发送给客户端
                //1.1获取用于输入信息
            String username = request.getParameter("username");
            if( username != null && !username.equals("")) {
                //1.2创建Cookie
                Cookie cookie = new Cookie("username",username);
                out.print("欢迎 :" + username + "!");
                //1.3设置有效时间
                cookie.setMaxAge(30);
                //1.4将Cookie发送给客户端
                response.addCookie(cookie);
                isLog = true;
            } else {
            //2.没有输入信息,那么看客户端有没有发送Cookie
                //2.1获取客户端发送过来的所有Cookie
                Cookie[] cookies = request.getCookies();
                if( cookies != null && cookies.length > 0 ) {
                    for( Cookie aux : cookies ) {
                        //2.2查找我们所需要的Cookie
                        if( aux.getName().equals("username") ) {
                            out.print("欢迎 :" + aux.getValue() + "!");
                            isLog = true;
                            break;
                        }
                    }
                }
                
                //3.没有进行过登录,也没有Cookie存在
                if( isLog == false ) {
                    //进行重定向
                    response.sendRedirect("login.jsp");
                }
            }
        %>
    </body>
</html>

效果

微信公众号:JavaWeb架构师

其它

关注下方公众号,回复:javaweb_course.code
完整教程PDF版本下载
上一篇 下一篇

猜你喜欢

热点阅读