二、cookie自動填充

需求:實現(xiàn)cookie在首次打開瀏覽器情況下的自動填充。
register.jsp

<%@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=ISO-8859-1">
<title>用戶注冊頁面</title>
</head>
<body>
<%
    String name = null;
    String pwd = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (int i = 0 ;i < cookies.length; i++) {
            if (cookies[i].getName().equals("name")) {
                String name_pwd = cookies[i].getValue();
                name = name_pwd.split("-")[0];
                name = URLDecoder.decode(name, "utf-8");
                pwd = name_pwd.split("-")[1];
                break;
            }
        }
    }
 %>
<form action="doRegister.jsp" method="get">
    <p>姓名:<input type="text" name="name" <%if (name != null) {%>value=<%=name %> <%} %>/><p/>
    <p>密碼:<input type="password" name="password" <%if (pwd != null) {%>value=<%=pwd %> <%} %>/><p/>
    <p>愛好:<input type="checkbox" name="interest" value="talking">聊天</input>
           <input type="checkbox" name="interest" value="basketball">籃球</input>
           <input type="checkbox" name="interest" value="swimming">游泳</input>
           <input type="checkbox" name="interest" value="football">足球</input>
    <p/>
    <p><input type="submit" name="submitButton" value="提交"/><p/>
</form>
<%
    String mess = (String)request.getAttribute("mess");
    if (!(mess==null)) {
 %>
 <%=mess %>
 <%} %>
</body>
</html>

doRegister.jsp

<%@page import="java.net.URLEncoder"%>
<%@ 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>
    <%
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("password");
        String[] interest = request.getParameterValues("interest");
    %>
    <%=name %>
    <%=pwd %>
    <%
        for(int i = 0; i <interest.length; i++) {
            out.print(interest[i]);
        } 
    %>
    <%
        if (name.equals("張三")) {
            request.setAttribute("mess", "登錄失敗");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        } else {
            session.setAttribute("msg", "登錄成功");
            session.setAttribute("name", name);
            session.setAttribute("pwd", pwd);
            String name_pwd = name+"-"+pwd;
            name_pwd = URLEncoder.encode(name_pwd, "utf-8");
            Cookie cookie = new Cookie("name", name_pwd);
            cookie.setPath("/");
            cookie.setMaxAge(100*60);
            response.addCookie(cookie);
            response.sendRedirect("success.jsp");
        }
     %>
    
</body>
</html>

結(jié)果分析:在關閉了瀏覽器的自動填充功能之后(不要關閉cookie功能),上面的代碼就可以自動填充了。但是上面的代碼只能實現(xiàn)一個用戶的自動填充,新來用戶的時候會把之前的用戶覆蓋掉。并且在第一次訪問,即之前沒有登錄過的時候不會報錯。

補:response只有addCookie,request只有getCookie。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,598評論 19 139
  • 目錄Cookie機制什么是CookieCookie的不可跨域名性Unicode編碼:保存中文BASE64編碼:保存...
    Tomatoro閱讀 17,044評論 7 186
  • 會話(Session)跟蹤是Web程序中常用的技術,用來跟蹤用戶的整個會話。常用的會話跟蹤技術是Cookie與Se...
    chinariver閱讀 5,786評論 1 49
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,943評論 1 92
  • 近年來,跨境電商發(fā)展迅速,但是物流模式一直都是跨境電商的痛,那么跨境電商國際物流模式到底有哪幾種?到底怎么樣的跨境...
    羅聚客閱讀 576評論 1 3

友情鏈接更多精彩內(nèi)容