1.创建一个Servlet来实现登陆界面
package com.web.he0401rememberpassword;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletLoginUI
*/
@WebServlet("/ServletLoginUI")
public class ServletLoginUI extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletLoginUI() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
String username="";
String password="";
if (request.getAttribute("error")==null) {
Cookie[] cookies=request.getCookies();
if (cookies!=null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie=cookies[i];
if (cookie.getName().equals("username")) {
username=cookie.getValue();
}
if (cookie.getName().equals("password")) {
password=cookie.getValue();
}
}
}
}else {
printWriter.write("<font size=5 color=red>账号或密码错误</font><br>");
}
printWriter.write("<form action='ServletLoginCookie' method='get'>");
printWriter.write("账号:<input type='text' name='account' value='"+username
+ "'><br>");
printWriter.write("密码:<input type='password' name='password' value='"+password
+ "'><br>");
printWriter.write("<input type='checkbox' name='checkbox' value='on'>记住用户名和密码两周<br>");
printWriter.write("<input type='submit' value='提交'>");
printWriter.write("</form>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2.登陆成功的界面
package com.web.he0401rememberpassword;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginSucess
*/
@WebServlet("/LoginSucess")
public class LoginSucess extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginSucess() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
String username=(String) request.getAttribute("username");
String password=(String) request.getAttribute("password");
printWriter.write("<font size='5' color='green'>您的登录信息如下</font><br>");
printWriter.write("账号:"+username);
printWriter.write("<br>密码:"+password);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.处理登陆信息的Servlet
package com.web.he0401rememberpassword;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletLoginCookie
*/
@WebServlet("/ServletLoginCookie")
public class ServletLoginCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletLoginCookie() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
String username=request.getParameter("account");
String password=request.getParameter("password");
String checkbox=request.getParameter("checkbox");
System.out.println("账号:"+username);
System.out.println("密码:"+password);
System.out.println(checkbox);
// if ("on".equals(checkbox)) {
// if (username.equals("123")&&password.equals("456")) {
// Cookie name=new Cookie("username", username);
// Cookie pass=new Cookie("password", password);
// name.setMaxAge(Integer.MAX_VALUE);
// pass.setMaxAge(Integer.MAX_VALUE);
// response.addCookie(name);
// response.addCookie(pass);
// request.setAttribute("username", username);
// request.setAttribute("password", password);
// request.getRequestDispatcher("/LoginSucess").forward(request, response);
// }else {
// //response.sendRedirect(request.getContextPath()+"/LoginFailed");
// request.setAttribute("error", "账号或密码错误");
// request.getRequestDispatcher("/ServletLoginUI").forward(request, response);
// }
// }else{
// Cookie name=new Cookie("username", username);
// Cookie pass=new Cookie("password", password);
// name.setMaxAge(0);
// pass.setMaxAge(0);
// response.addCookie(name);
// response.addCookie(pass);
// request.getRequestDispatcher("/ServletLoginUI").forward(request, response);
// }
Cookie name=new Cookie("username", username);
Cookie pass=new Cookie("password", password);
if ("".equals(username)||"".equals(password)) {
printWriter.write("账号和密码不能为空!");
}
else if ("on".equals(checkbox)) {
name.setMaxAge(Integer.MAX_VALUE);
pass.setMaxAge(Integer.MAX_VALUE);
response.addCookie(name);
response.addCookie(pass);
if ("123".equals(username)&&"456".equals(password)) {
request.setAttribute("username", username);
request.setAttribute("password", password);
request.getRequestDispatcher("/LoginSucess").forward(request, response);
}else {
request.setAttribute("error", "账号或密码错误");
request.getRequestDispatcher("/ServletLoginUI").forward(request, response);
}
}else {
if ("123".equals(username)&&"456".equals(password)) {
request.setAttribute("username", username);
request.setAttribute("password", password);
request.getRequestDispatcher("/LoginSucess").forward(request, response);
}else {
request.setAttribute("error", "账号或密码错误");
request.getRequestDispatcher("/ServletLoginUI").forward(request, response);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.说明
(1).编码方面,由于我使用的是doget方式来进行提交的数据,所以服务器接收到的是浏览器默认的编码方式,如果服务器需要request中的对象信息,则需要对其进行解码。
(2).需要注意的是,cookie中不支持存储中文,存取的时候需要进行编解码才能存入!