問題1 (isUseCookies != null && isUseCookies.length > 0 )這個地方有問題,isUseCookies 是返回值,相當於布爾值,其實不需要這么判斷,因為不會出現isUseCookies="",這種情況,這個是針對username或者密碼的。
默認勾選返回值是on,未勾選的默認返回值是null。不需要用字符數組。
問題2 request.getParameterValues在這里,只有一個選擇框,不需要使用數組來接收吧。 request.getParameter即可。數組太浪費.
僅僅為了深度求學,才提出問題供大家探討,希望老師能看到,指導。
<% request.setCharacterEncoding("utf-8"); //首先判斷用戶是否選擇了記錄登錄狀態 /* jsp容器會為客戶端的一個請求生成一個對應的request對象 */ String[] isUseCookies = request.getParameterValues("isUseCookie"); /* for (String c : isUseCookies) { out.println(c);} */ /* for(int i=0;i<isUseCookies.length;i++) { out.println(isUseCookies[i]); } */ out.println(isUseCookies.length); /* getParameterValues(String name) public class javax.servlet包 ServletRequestWrapper類, request對象就是該類的實例 提供 ServletRequest 接口的便捷實現, getParameterValues(String name) 此方法的默認行為是返回對包裝的請求對象調用 getParameterValues(String name) 的結果。 javax.servlet.ServletRequest 接口 中的方法,jsp容器生成的request對象實現了該接口 ServletRequest 對象提供包括參數名稱、參數值、屬性和輸入流的數據。 擴展 ServletRequest 的接口可提供其他特定於協議的數據 返回包含給定請求參數擁有的所有值的 String 對象數組,如果該參數不存在,則返回 null。 如果該參數只有一個值,則數組的長度為 1。 name zh_cn 包含請求其值的參數的名稱的 String return zh_cn 包含參數值的 String 對象數組 See also getParameter */ /* 問題1:這個地方為什么是一個字符串數組,不是選項框嗎 */ if (isUseCookies != null && isUseCookies.length > 0 ) { /* isUseCookies=""時,isUseCookies.length=0且不為null */ //把用戶名和密碼保存在Cookie對象里 String username = URLEncoder.encode( request.getParameter("username"), "utf-8"); String password = URLEncoder.encode( request.getParameter("password"), "utf-8"); Cookie usernameCookie = new Cookie("username", username); Cookie passwordCookie = new Cookie("password", password); usernameCookie.setMaxAge(864000); passwordCookie.setMaxAge(864000);//設置最大生存期限為10天 response.addCookie(usernameCookie); response.addCookie(passwordCookie); } else { Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie c : cookies) { if (c.getName().equals("username") || c.getName().equals("password")) { c.setMaxAge(0); //設置Cookie失效 response.addCookie(c);//重新保存 } } } } %>