cookie記錄使用者名稱
阿新 • • 發佈:2018-11-01
login
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定編碼 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter();//伺服器到瀏覽器寫出 //用servlet寫登入頁面 out.print("<form action='"+request.getContextPath()+"/servlet/doLogin' method=''>"); out.print("使用者名稱:<input type='text' name='username'><br/>"); out.print("密碼:<input type='password' name='password'><br/>"); out.print("<input type='checkbox' name='remember'>記住使用者名稱<br/>"); //remember 記住密碼 out.print("<input type='submit' value='登入'>"); out.print("</form>"); }
dologin
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter();//伺服器到瀏覽器寫出 //1.獲取前端資料 String username = request.getParameter("username"); String password = request.getParameter("password"); //2.處理業務邏輯 if("admin".equals(username)&&"123456".equals(password)){ out.print("登入成功"); }else{ //3.分發轉向 out.print("登入失敗"); // 2秒後 重定向路徑 動態獲取專案名 response.setHeader("refresh", "2;url="+request.getContextPath()+"/servlet/login"); } }
login
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定編碼 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter();//伺服器到瀏覽器寫出 //+++ String username=""; String checked=""; Cookie[] cookies=request.getCookies(); for(int i=0;cookies!=null&&i<cookies.length;i++){ if("username".equals(cookies[i].getName())){ //getname username = cookies[i].getValue(); // getvalue (name,value) checked="checked='checked'"; } } //用servlet寫登入頁面 out.print("<form action='"+request.getContextPath()+"/servlet/doLogin' method=''>"); out.print("使用者名稱:<input type='text' name='username' value='"+username+"'><br/>"); out.print("密碼:<input type='password' name='password'><br/>"); out.print("<input type='checkbox' name='remember' value='"+checked+"'>記住使用者名稱<br/>"); //remember 記住密碼 out.print("<input type='submit' value='登入'>"); out.print("</form>"); }
dologin
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();//伺服器到瀏覽器寫出
//1.獲取前端資料
String username = request.getParameter("username");
String password = request.getParameter("password");
//++1
String remember=request.getParameter("remember");
//++2 建立一個cookie
Cookie cookie=new Cookie("username",username);
//2.處理業務邏輯
if("admin".equals(username)&&"123456".equals(password)){
//登陸成功後選擇是否記錄使用者名稱
if(remember!=null){ //checkbox 不打√ null
//記住使用者名稱
cookie.setMaxAge(Integer.MAX_VALUE); //設定cookie儲存時間
}else{
cookie.setMaxAge(0); //清除cookie
//回寫到客戶端
response.addCookie(cookie);
}
out.print("登入成功");
}else{
//3.分發轉向
out.print("登入失敗"); // 2秒後 重定向路徑 動態獲取專案名
response.setHeader("refresh", "2;url="+request.getContextPath()+"/servlet/login");
}
}