servlet小练习:实现十天免登录功能

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
在这里插入图片描述在这里插入图片描述
刷新之后直接登录了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值