用AJAX實現“防止未登陸人員訪問指定頁面”的功能

來源:互聯網
上載者:User

標籤:return   pen   forward   location   dex   int   html   瀏覽器   高版本   

為了保護科協官網不被未登入的同學訪問,用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";//跳轉

 

用AJAX實現“防止未登陸人員訪問指定頁面”的功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.