jsp-4 用cookie實現記住密碼

來源:互聯網
上載者:User

標籤: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>                &nbsp;&nbsp;<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實現記住密碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.