首页 > 技术文章 > 用AJAX实现“防止未登陆人员访问指定页面”的功能

sovagxa 2017-07-29 12:13 原文

为了保护科协官网不被未登录的同学访问,用ajax进行判断和跳转

小知识:用ajax访问的servlet的任何跳转都是无效的,所以跳转只能在js代码中进行

以下是servlet代码

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        // 若胆敢用首页url想直接进入首页,则进入get方法,然后重定向回登陆页面
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("KXlogin_username".equals(cookie.getName())) {//找到登陆过的证据(cookie)
                    //request.getRequestDispatcher("/kxpro_homePage/index.html").forward(request,response);// 转发请求至首页(隐藏首页url)
                    //response.sendRedirect("/KXpro/kxpro_homePage/index.html");
                    out.print("AnUser");break;
                } else {
                }
            }
        }else{
            out.print("NoCookie");
            //response.sendRedirect("/KXpro/kxpro_login/index.html");// 重定向回登陆页面
        }
            
    }

说明:从浏览器拿到cookie进行判断是否登录,有登陆就有cookie,没登陆就没有cookie,当看见这个cookie时马上out一串"AnUser",然后break出循环结束访问服务器,那么服务器就返回"AnUser"。

没有登陆过的有两种情况一种是浏览器有cookie但没有登录信息的cookie,另一种情况是根本就没有cookie,这两种情况前者返回空串,后者返回"NoCookie"串,然后以此为凭据在js中判断。

 

==================================================================================================================================

下面是js代码

<body onload="homePageLoad()">



    function createXMLHttpRequest() {
        var xmlHttp;
        // 适用于大多数浏览器,以及IE7和IE更高版本
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            // 适用于IE6
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                // 适用于IE5.5,以及IE更早版本
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                }
            }
        }
        return xmlHttp;
    }

    function homePageLoad() {
        var xmlHttp = createXMLHttpRequest();
        xmlHttp.open("GET", "/KXpro/servlet/LoginServlet", true);
        xmlHttp.send(null);
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                //到这里就表示通讯成功,可以任意拿到text或者xml
                //使用var txt=xmlHttp.responseText;  获得text,text可以使json串
                //使用var xmls=xmlHttp.responseXML; 获得xml,不过解析起来比较麻烦
                var txt = xmlHttp.responseText;
                if (txt == "AnUser")//判断是否有登陆过的cookie
                {
                    //有登陆过,什么也不做
                } else {
                    window.location.href = "/KXpro/kxpro_login/index.html";//跳转到首页
                }
            }
        }
    }

 

 整个ajax大框架在    http://www.cnblogs.com/sovagxa/articles/7241671.html   说的很清楚,就不再赘述了

拿到text串之后直接判断是否要跳转就行  跳转代码:

window.location.href = "/KXpro/kxpro_login/index.html";//跳转

 

推荐阅读