package servlet;
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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(
name = "ToLoginLServlet",
urlPatterns = "/toLogin"
)
public class ToLoginLServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String msg = "";//复选框被勾住
//2.获取客户端cookie中的username
Cookie[] cookies = req.getCookies();
String username = "";//用户名
//判断是否勾选 ,如果勾了就是on,刚开始肯定没有,就不会进if
//如果勾了,就进去找对应的Cookie,并修改值。
boolean cRemember = false;
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if ("cRemember".equals(cookie.getName())) {
if ("on".equals(cookie.getValue())) {
//代表上次勾了记住密码,前台要显示勾住
cRemember = true;
break;
}
}
}
//如果勾了,用户名回显操作
if (cRemember) {
msg = "checked=\"checked\"";
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
username = cookie.getValue();
break;
}
}
}
}
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
"<title>登陆</title>\n" +
"<script type=\"text/javascript\">\n" +
"\tfunction checkLogin(){\n" +
"\t\t//1.获取用户名\n" +
"\t\tvar tel = document.getElementById(\"tel\").value;\n" +
"\t\t//2.获取密码\n" +
"\t\tvar pwd = document.getElementById(\"pwd\").value;\n" +
"\t\t//3.判断\n" +
"\t\tif(tel==null || tel.length==0){\n" +
"\t\t\talert(\"用户名不能为空\");\n" +
"\t\t\treturn false;\n" +
"\t\t}\n" +
"\t\tif(pwd==null || pwd.length==0){\n" +
"\t\t\talert(\"密码不能为空\");\n" +
"\t\t\treturn false;\n" +
"\t\t}\n" +
"\t\treturn true;\n" +
"\t}\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
"\t<form action=\"login\" method=\"post\" onsubmit=\"return checkLogin()\">\n" +
"\t\t用户名:<input value=\"" + username + "\" type=\"text\" name=\"username\" id=\"tel\"><br/>\n" +
"\t\t密码:<input type=\"password\" name=\"password\" id=\"pwd\"><br/>\n" +
"\t\t<input type=\"checkbox\" name=\"remember\" " + msg + ">记住密码<br/><input type=\"submit\" value=\"登陆\">\n" +
"\t</form>\n" +
"</body>\n" +
"</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
package servlet;
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;
import java.io.IOException;
@WebServlet(
name = "LoginServlet",
urlPatterns = "/login"
)
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//获得用户名
String username = req.getParameter("username");
//获得密码
String password = req.getParameter("password");
//获取是否勾选
String remember = req.getParameter("remember");
//对接,用户名密码是否正确,如果是第一次,下面都不会进入!!!
if ("root".equals(username) && "52Java".equals(password)) {
//成功,判断用户是否勾选,如果勾了就存入Cookie
if ("on".equals(remember)) {
//1.准备一个Cookie
Cookie cUsername = new Cookie("username", username);
Cookie cRemember = new Cookie("cRemember", "on");
//存最大年龄 秒
cUsername.setMaxAge(60 * 10);
cRemember.setMaxAge(60 * 10);
//2.响应到客户端
resp.addCookie(cUsername);
resp.addCookie(cRemember);
} else {//如果没有勾,就清除掉
Cookie[] cookies = req.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
cookie.setMaxAge(0);
}
if ("cRemember".equals(cookie.getName())) {
cookie.setMaxAge(0);
}
resp.addCookie(cookie);
}
}
}
//3.转发,这个页面随意,代表登录成功要看到的页面,比如显示所有。。。
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
//重定向到登陆页,这说明用户名,密码不匹配。
resp.sendRedirect("toLogin");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}