【Web】【Servlet】【Cookie记住登陆的账号和密码存储在本地磁盘中】2016.04.02

本文介绍了一个简单的Java Web登录系统的实现过程,包括登录界面、处理登录信息及登录成功后的页面展示。系统利用Servlet接收并处理用户的登录请求,并通过Cookie保存用户信息以实现“记住密码”功能。
摘要由CSDN通过智能技术生成

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中不支持存储中文,存取的时候需要进行编解码才能存入!


转载于:https://my.oschina.net/YongfengHe/blog/651843

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值