Cookie.Session的自动登录,注册的案例
首先先写几个简单的html页面(主页,登录,注册,个人中心)
然后,先写注册界面,需要在本地先创建一个文件,然后通过登录注册可以从本地文件读取或者写入
注册界面
@WebServlet("/register")
public class RegisterServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName=req.getParameter("userName");
String password=req.getParameter("password");
String repassword=req.getParameter("repassword");
//判断用户名密码是否为空或者是null,如果是,就转发到注册页面
if(userName==null||userName.isEmpty()||password==null||password.isEmpty()) {
req.getRequestDispatcher("pages/register.html").forward(req, resp);
return;
}
//创建输入流读取本地文件
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
//判断本地文件中的用户名是否已存在,如果存在,转发回注册页面
if(strs[0].equals(userName)) {
req.getRequestDispatcher("pages/register.html").forward(req, resp);
return;
}
}
br.close();
//判断两次输入的密码是否一致,如果一致,写入到本地文件中去,并且重定向到登录界面,如果不一致,转发会注册界面
if(password.equals(repassword)) {
BufferedWriter bw=new BufferedWriter(new FileWriter("d://demo.txt",true));
bw.write(userName+"="+password);
bw.newLine();
bw.close();
resp.sendRedirect("/day_30practise/pages/login.html");
}else {
req.getRequestDispatcher("pages/register.html").forward(req, resp);
}
}
}
登录界面
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName=req.getParameter("userName");
String password=req.getParameter("password");
//判断用户名密码是否为空或者是null,如果是,就转发到登录页面
if(userName==null||userName.isEmpty()||password==null||password.isEmpty()) {
req.getRequestDispatcher("pages/login.html").forward(req, resp);
return;
}
//创建输入流读取本地文件
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
//判断本地文件中的用户名密码是否和输入的用户名密码一致
if(strs[0].equals(userName)&&strs[1].equals(password)) {
//创建session,添加login标记
req.getSession().setAttribute("login", true);
//创建cookie对象,将用户名密码存到浏览器cookie中,7天之后自动清除
Cookie cookie=new Cookie("uap", userName+"="+password);
cookie.setMaxAge(7*24*3600);
resp.addCookie(cookie);
//重定向回主页
resp.sendRedirect("/day_30practise/pages/home.html");
return;
}
}
br.close();
//如果账号密码不一致,转发回登录界面
req.getRequestDispatcher("pages/login.html").forward(req, resp);
}
}
个人中心界面
@WebServlet("/infopage")
public class InfoPageServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//如果已经存在login标记,就直接转发到个人中心界面
if(req.getSession().getAttribute("login")!=null) {
req.getRequestDispatcher("/WEB-INF/pages/infoPage.html").forward(req, resp);
}else {
//遍历cookie
Cookie[] cookies=req.getCookies();
if(cookies!=null) {
//如果cookie不为空,遍历cookie
for (Cookie cookie : cookies) {
//判断cookie的名字是不是登录的时候创建的名字
if("uap".equals(cookie.getName())) {
String value=cookie.getValue();
String[] values=value.split("=");
BufferedReader br=new BufferedReader(new FileReader("d://demo.txt"));
String str;
while((str=br.readLine())!=null) {
String[] strs=str.split("=");
//比较cookie中存储的用户名密码是否和本地文件中的用户名密码一致,如果一致,转发到个人中心界面
if(strs[0].equals(values[0])&&strs[1].equals(values[1])) {
req.getSession().setAttribute("login", true);
req.getRequestDispatcher("/WEB-INF/pages/infoPage.html").forward(req, resp);
return;
}
}
br.close();
//如果循环完成都没有相互匹配的值,说明当前用户的密码已经修改,清除cookie,转发回登录界面重新登录
Cookie cookie2=new Cookie("uap","" );
cookie2.setMaxAge(0);
resp.addCookie(cookie2);
resp.sendRedirect("/day_30practise/pages/login.html");
return;
}
}
resp.sendRedirect("/day_30practise//pages/login.html");
}else {
//如果cookie为空,说明没有登录,重定向到登录界面
resp.sendRedirect("/day_30practise//pages/login.html");
}
}
}
}
注销界面
@WebServlet("/logout")
public class LogOutServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//移除login属性的session
req.getSession().removeAttribute("login");
//将名字为uap的cookie从浏览器中清除
Cookie cookie=new Cookie("uap", "");
cookie.setMaxAge(0);
resp.addCookie(cookie);
req.getRequestDispatcher("pages/home.html").forward(req, resp);
}
}