標籤:servlet gis tle auto encoding method ref throws cti
jsp-4 用cookie實現記住密碼
這次就有點簡單了
基本是jsp-3的代碼但是有些修改
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user=new User(); String username=req.getParameter("username"); user.setUsername(username); String password=req.getParameter("password"); user.setPassword(password); User use=userServive.login(user); if(use!=null){ String [] ischeck=req.getParameterValues("ischeck"); if(ischeck!=null&&"true".equals(ischeck[0])){ //添加cookie資訊 Cookie usernameCookie=new Cookie("username", username); Cookie passwordCookie=new Cookie("password",password ); usernameCookie.setMaxAge(60*60*24*1);//一天 passwordCookie.setMaxAge(60*60*24*1); resp.addCookie(usernameCookie); resp.addCookie(passwordCookie); } resp.sendRedirect("loginSuccess.jsp"); }else{ resp.sendRedirect("loginFail.jsp"); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;%><!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 username="";String password="";//取出CookieCookie [] cookie=request.getCookies();for(int i=0;i<cookie.length;i++){ if(cookie[i].getName().equals("username")){ username=cookie[i].getValue(); } if(cookie[i].getName().equals("password")){ password=cookie[i].getValue(); }}%><form action="<%=basePath%>/login.do" method="post"> <table border=0 cellpadding=0 cellspacing=0 style="margin:auto;border-collapse:separate; border-spacing:10px;"> <tr align="center"> <td colspan="2"> <label>登入</label> </td> </tr> <tr> <td> 使用者名稱: </td> <td> <input type="text" name="username" value="<%=username%>"> </td> </tr> <tr> <td> 密碼: </td> <td> <input type="password" name="password" value="<%=password%>"> </td> </tr> <tr> <td> <input type="checkbox" name="ischeck">記住我? </td> <td> <a href="<%=basePath%>/toRegister.do">註冊</a> </td> </tr> <tr align="center"> <td> <input type="submit" value="登入"> </td> <td> <input type="reset" value="重設"> </td> </tr> </table></form></body></html>
我就在糾結的是根據mvc的原則,cookie的操作是不是放在jsp?
jsp-4 用cookie實現記住密碼