检验用户登录信息(没有Filter,比较麻烦)——1.0版
login.jsp
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><form action="login-process.jsp" method="post">name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% //处理用户登录的 JSP//1. 获取用户的登录信息, nameString name = request.getParameter("name") ;//2. 把登录信息存储 Session 中. //需要判断用户是否直接访问当前页面或在 login.jsp 页面没有输入任何信息就登录if(name != null && !name.trim().equals("")) {//OK, 在 Session 中保存用户信息, 转发到 /login/articles.jspsession.setAttribute("user", name) ;request.getRequestDispatcher("/login/articles.jsp").forward(request, response) ;}else {//直接登录或没有输入登录信息, 重定向到 /login/login.jspresponse.sendRedirect(request.getContextPath() + "/login/login.jsp") ;}%>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><a href="article1.jsp">Article111 Page</a><br /><br /><a href="article2.jsp">Article222 Page</a><br /><br /><a href="article3.jsp">Article333 Page</a><br /><br /><a href="article4.jsp">Article444 Page</a><br /><br /><a href="logout.jsp">Logout</a></body></html>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><% //判断用户是否登录: 即判断 Session 中有没有登录信息Object obj = session.getAttribute("user") ;//若没有登录信息, 则把页面重定向到 /login/login.jspif(null == obj) {response.sendRedirect(request.getContextPath() + "/login/login.jsp") ;}//若有登录信息, OK, 可以访问当前页面%>Article 111</body></html>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><% //判断用户是否登录: 即判断 Session 中有没有登录信息Object obj = session.getAttribute("user") ;//若没有登录信息, 则把页面重定向到 /login/login.jspif(null == obj) {response.sendRedirect(request.getContextPath() + "/login/login.jsp") ;}//若有登录信息, OK, 可以访问当前页面%>Article 222</body></html>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><% //判断用户是否登录: 即判断 Session 中有没有登录信息Object obj = session.getAttribute("user") ;//若没有登录信息, 则把页面重定向到 /login/login.jspif(null == obj) {response.sendRedirect(request.getContextPath() + "/login/login.jsp") ;}//若有登录信息, OK, 可以访问当前页面%>Article 333</body></html>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><% //判断用户是否登录: 即判断 Session 中有没有登录信息Object obj = session.getAttribute("user") ;//若没有登录信息, 则把页面重定向到 /login/login.jspif(null == obj) {response.sendRedirect(request.getContextPath() + "/login/login.jsp") ;}//若有登录信息, OK, 可以访问当前页面%>Article 444</body></html>