protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkbox = request.getParameter("checkbox");
//验证码
//密码是否正确
//添加是否自动保存用户名密码
CookieUtil.addCookie(username, password, checkbox, request, response);
//跳转
request.getRequestDispatcher("/success.jsp").forward(request, response);
}
3. [代码]CookieUtil工具类
public class CookieUtil {
private static final int MAXTIME = 60*60*24*7; //最长cookie保存一个星期
public static void addCookie(String username,String password,String checkbox,HttpServletRequest request, HttpServletResponse response){
String path = request.getContextPath()+"/";
System.out.println("cookiePath:"+path);
//对用户名,密码进行加密
try {
//可以自己设定加密规则,见EncryptionCode中的encryptionRules
username = EncryptionCoder.encryptBASE64(username.getBytes());
password = EncryptionCoder.encryptBASE64(password.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
//创建cookie