LoginSevlet.java
package com.wj.servlettest;
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;
/**
* 用户登录
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected 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 password="";
Cookie[] cookies=request.getCookies();
for (int i = 0;cookies!=null&& i < cookies.length; i++) {
if("username".equals(cookies[i].getName())) {
username = cookies[i].getValue();
}
if("password".equals(cookies[i].getName())) {
password = cookies[i].getValue();
}
}
//判断cookie中的用户名和密码是否正确匹配
if("admin".equals(username)&&"123456".equals(password)) {
out.write("登录成功!");
}else {
out.write("<html><head></head><body>");
out.write("<form action='"+ request.getContextPath()+"/DoLoginServlet' method='post'>");
out.write("用户名:<input type='text' name='username'>");
out.write("<br/>");
out.write("密码:<input type='password' name='password'>");
out.write("<br/>");
out.write("<input type='checkbox' name='remember'>十天免登录");
out.write("<br/>");
out.write("<input type='submit' value='登录'></form>");
out.write("</body></html>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
DoLoginServlet.java
package com.wj.servlettest;
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;
/**
*用户登录
*/
@WebServlet("/DoLoginServlet")
public class DoLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
String remember = request.getParameter("remember");
//判断用户是否输入了正确的用户名和密码
if("admin".equals(username)&&"123456".equals(password)) {
out.write("登录成功!");
//用户勾选了十天内自动登录
if(remember!=null) {
Cookie cookie1=new Cookie("username",username);
Cookie cookie2=new Cookie("password",password);
//设置cookie失效时间为10天
cookie1.setMaxAge(60*60*240);
cookie2.setMaxAge(60*60*240);
//设置cookie的绑定路径
cookie1.setPath("/");
cookie2.setPath("/");
//将cookie发送给客户端
response.addCookie(cookie1);
response.addCookie(cookie2);
}
}else {
out.write("登录失败,用户名或密码错误");
//设置两秒后跳转到重新登录
response.setHeader("refresh", "2;url="+request.getContextPath()+"/LoginServlet");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
测试效果
正确的用户名与密码是 admin 123456
刷新之后直接登录了。