JavaWeb應用Cookie模仿完成主動登錄功效(不需用戶名和暗碼)。本站提示廣大學習愛好者:(JavaWeb應用Cookie模仿完成主動登錄功效(不需用戶名和暗碼))文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb應用Cookie模仿完成主動登錄功效(不需用戶名和暗碼)正文
個中包括兩個jsp文件,分離為login.jsp和index.jsp
代碼以下:
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>登錄界面</title> </head> <body> <form action="index.jsp" method="post"> 用戶名:<input type="text" name="name"/> <input type="submit" value="提交"/> </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>迎接您</title> </head> <body> <% String name = request.getParameter("name"); if(name != null && !name.trim().equals("")){ Cookie cookie = new Cookie("name",name); cookie.setMaxAge(30); //設置cookie有用期為30s response.addCookie(cookie); }else{ Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 0){ for(Cookie cookie:cookies){ String cookieName = cookie.getName(); if("name".equals(cookieName)){ String val = cookie.getValue(); name = val; } } } } if(name != null && !name.trim().equals("")){ out.print("hello: " + name); }else{//不然重定向到登錄界面 response.sendRedirect("login.jsp"); } %> </body> </html>
以上所述是小編給年夜家引見的JavaWeb應用Cookie模仿完成主動登錄功效,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!