javaweb之Session完成用户登录

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



首页



	
	欢迎您:${user.username}
	登录
	注销
	


package test.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//用户登陆servlet
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		List list = DB.getAll();
		for (User user : list) {
			// 如果用户登录成功
			if (user.getUsername().equals(username)
					&& user.getPassword().equals(password)) {
				HttpSession session = request.getSession();
				// 手动设置session的有效期为30分钟
				String sessionId = session.getId();
				Cookie cookie = new Cookie("JSESSIONID", sessionId);
				cookie.setMaxAge(60 * 30);
				cookie.setPath(request.getContextPath());
				response.addCookie(cookie);
				// 登录成功后要存入用户的登录状态,key是用户对象的String形式value就是用户对象(model)!!别的页面应该能用到
				session.setAttribute("user", user);
				// 重定向到首页,URL重写方式
				String url = response.encodeRedirectURL(request
						.getContextPath() + "/index.jsp");
				response.sendRedirect(url);
				return;
			}
		}

		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("用户名或密码错误");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

// 模拟存储用户的数据库
class DB {
	private static List list = new ArrayList();
	static {
		list.add(new User("aaa", "123"));
		list.add(new User("bbb", "123"));
		list.add(new User("ccc", "123"));
	}

	public static List getAll() {
		return list;
	}
}

package test.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//注销servlet
public class LogoutServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(false);
		if (session == null) {
			// 没登录,重定向到首页
			String url = response.encodeRedirectURL(request.getContextPath()
					+ "/index.jsp");
			response.sendRedirect(url);
			return;
		}
		// 从session中移除登录状态
		session.removeAttribute("user");
		// 重定向到首页,URL重写方式
		String url = response.encodeRedirectURL(request.getContextPath()
				+ "/index.jsp");
		response.sendRedirect(url);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



用户登录



	
用户名:
密码:


你可能感兴趣的:(自己用)