使用cookie記錄登入名稱,下次登入時能夠記得上次的登入名稱,使用session記住登入資訊並驗證是否登入,防止利用url開啟網站,並實現退出登入功能

來源:互聯網
上載者:User

標籤:highlight   src   驗證   str   write   ddc   strong   系統   log   

<%@ 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><%String name=null;Cookie[] cks = request.getCookies() ;//獲得cookie的集合if(cks != null){      // 如果已經設定了cookie , 則得到它的值,將該值放在登入名文字框的value中        for(int i=0; i<cks.length; i++)        {               if(cks[i].getName().equals("name"))            name = cks[i].getValue();        }     }%><form  action="ShoppCheck.jsp" mothod="post">使用者名稱:<input type="text"name="name"  value="<% if(name != null) out.println(name); %>"><br>密碼:&nbsp&nbsp&nbsp&nbsp<input type= "password"name="password"><br><input type="submit" value="登入"></form></body></html>

  

<%@page import="com.hanqi.web.ShoppDAO"%><%@ 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><%//設定不快取頁面面//response.setHeader("Cache_Control", "no-cache");//接受資料String name =request.getParameter("name");String password =request.getParameter("password");if(name==null || password == null||name.equals("")||password.equals("")){//排除使用者名稱和密碼為null或 為空白字串的情況out.write("請正確登入系統");}else{//檢查登入資訊ShoppDAO  sd= new ShoppDAO ();//導包if(sd.checkLogin(name, password)){//out.write("登入成功");//無緩衝的直接發送response.getWriter().write("登入成功");//建立cookieCookie ck= new Cookie("name", name);//設定到期時間ck.setMaxAge(10*24*60*60);//發送response.addCookie(ck);//建立sessionsession.setAttribute("name", name);//頁面跳轉response.sendRedirect("xtMain.jsp");}else{out.write("登入失敗");//2秒後跳回登入頁面response.setHeader("refresh", "2;URL=shoppMain.jsp");}}%></body></html>

  

<%@page import="java.net.URLDecoder"%><%@ 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>系統首頁面<br><%//檢查cookie//獲得cookie集合Cookie[] cks=  request.getCookies();for(Cookie ck: cks){//解碼out.write(ck.getName()+"="+URLDecoder.decode(ck.getValue()) +"<br>");}//判斷sessionString cairnumber = session.getAttribute("name").toString();out.print("name="+cairnumber);%><a href="success.jsp">測試是否已登入的頁面</a></body></html>

  

<%@ 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>測試是否登入顯示的頁面<br><%Object obj =session.getAttribute("name");//判斷session是否存在if(obj==null){ out.print("您沒有登入"); response.setHeader("refresh", "2;URL=shoppMain.jsp");//若2秒鐘無操作自動退回主介面}else{out.print("name="+obj.toString());//銷毀所有session//session.invalidate();//移除某個屬性session.removeAttribute("name");//response.sendRedirect("tuichu.jsp");%><a href="tuichu.jsp">退出</a><% }%></body></html>

  

<%@ 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>系統將退出。。。。。</body></html>

  

如果兩秒鐘未任何操作,就會被退出,並自動返回主介面,防止利用URL訪問網站。

注意:在JSP中,所有的架包要放在WEB_INF下的lib 檔案夾下,

 

使用cookie記錄登入名稱,下次登入時能夠記得上次的登入名稱,使用session記住登入資訊並驗證是否登入,防止利用url開啟網站,並實現退出登入功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.