只有登录成功后,才能访问一些页面(2种方法:1.通过判断sesssion是否为空)2.过滤器Filter
2012-10-16 15:07
603 查看
1. 通过判断sesssion是否为空.在每个页面顶部加上如下代码
2. 过滤器的实现:
CheckUserFilter.java:
Web.xml:
</filter-mapping> <!-- 注册filter --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>day22BaseFilter.CharacterEncoding.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value>
</init-param> <init-param> <param-name>test2</param-name> <param-value>a2</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <!-- 拦截所有的请求 --> <url-pattern>/*</url-pattern> <!-- 拦截单个的jsp请求 <url-pattern>/a/b.jsp</url-pattern>
拦截所有jsp的请求 <url-pattern>*.jsp</url-pattern> 拦截所有的*.do请求 <url-pattern>/*</url-pattern> 拦截指定的Servlet请求 <servlet-name>LoginServlet</servlet-name> --> </filter-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>day22BaseFilter.CharacterEncoding.LoginServlet</servlet-class>
</servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
LoginServlet.java:
login.jsp同上一页
purview.jsp
purview1~4.jsp:
<% String username=(String)session.getAttribute("username"); if(username==null){ String contextPath=request.getContextPath(); response.sendRedirect(contextPath+"/login.jsp"); } %>
2. 过滤器的实现:
CheckUserFilter.java:
package day22BaseFilter.CheckUser; import java.io.IOException; import java.util.Arrays; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class CheckUserFilter implements Filter { private String checkSeesionKey; private String redirectURL; private List notCheckURLList; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { /*System.out.println("checkSeesionKey= "+this.checkSeesionKey); System.out.println("redirectURL= "+this.redirectURL); System.out.println("notCheckURLList= "+this.notCheckURLList);*/ HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse res=(HttpServletResponse)response; //获取Servlet的路径 String servletPath=req.getServletPath(); System.out.println("servletPath= "+servletPath); //获取Session HttpSession session=req.getSession(); //1.若访问路径包含/login.jsp,/LoginServlet if(this.notCheckURLList!=null&&this.notCheckURLList.contains(servletPath)){ //1.1 放行 chain.doFilter(request, response); }else{ String username=(String)session.getAttribute(this.checkSeesionKey); //2.若不包含以上2种 //2.1 从session获取用户名,若不空,放行;若空,重定向到redirectURL指向的页码 if(username!=null){ chain.doFilter(request, response); }else{ String contextPath=req.getContextPath(); res.sendRedirect(contextPath+this.redirectURL); } } } public void init(FilterConfig filterConfig) throws ServletException { // 读取web.xml文件中初始化参数 ServletContext sc=filterConfig.getServletContext(); String checkSessionKey=sc.getInitParameter("checkSessionKey"); this.checkSeesionKey=checkSessionKey; String redirectURL=sc.getInitParameter("redirectURL"); this.redirectURL=redirectURL; String[] notCheckURLListStr=sc.getInitParameter("notCheckURLList").split(","); List notCheckURLList=Arrays.asList(notCheckURLListStr); this.notCheckURLList=notCheckURLList; } }
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- <context-param> <param-name>testContext</param-name> <param-value>xxxxxxxx</param-value> </context-param> --> <context-param> <param-name>checkSessionKey</param-name> <param-value>username</param-value><!-- 相当于setAttribute中的key --> </context-param> <context-param> <param-name>redirectURL</param-name> <param-value>/login.jsp</param-value> </context-param> <context-param> <param-name>notCheckURLList</param-name> <param-value>/login.jsp,/LoginServlet</param-value> </context-param> <filter> <filter-name>CheckUserFilter</filter-name> <filter-class>day22BaseFilter.CheckUser.CheckUserFilter</filter-class> </filter> <filter-mapping> <filter-name>CheckUserFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispathcher>
<dispatcher>FORWARD</dispathcher>
</filter-mapping> <!-- 注册filter --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>day22BaseFilter.CharacterEncoding.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value>
</init-param> <init-param> <param-name>test2</param-name> <param-value>a2</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <!-- 拦截所有的请求 --> <url-pattern>/*</url-pattern> <!-- 拦截单个的jsp请求 <url-pattern>/a/b.jsp</url-pattern>
拦截所有jsp的请求 <url-pattern>*.jsp</url-pattern> 拦截所有的*.do请求 <url-pattern>/*</url-pattern> 拦截指定的Servlet请求 <servlet-name>LoginServlet</servlet-name> --> </filter-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>day22BaseFilter.CharacterEncoding.LoginServlet</servlet-class>
</servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
LoginServlet.java:
package day22BaseFilter.CharacterEncoding; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { public LoginServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HttpSession session=request.getSession(); String username=request.getParameter("username"); ServletContext sc=this.getServletContext(); String checkSessionKey=sc.getInitParameter("checkSessionKey"); if(checkSessionKey!=null){ session.setAttribute(checkSessionKey, username); //session.setAttribute("username", username); } System.out.println("username= "+username); request.getRequestDispatcher("/purview.jsp").forward(request, response); } public void init() throws ServletException { } }
login.jsp同上一页
purview.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> purview.jsp 主页面 <br/> <a href="${pageContext.request.contextPath }/purview1.jsp">purview1.jsp</a> <br/> <a href="${pageContext.request.contextPath }/purview2.jsp">purview2.jsp</a> <br/> <a href="${pageContext.request.contextPath }/purview3.jsp">purview3.jsp</a> <br/> <a href="${pageContext.request.contextPath }/purview4.jsp">purview4.jsp</a> <br/> </body> </html>
purview1~4.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String username=(String)session.getAttribute("username"); if(username==null){ String contextPath=request.getContextPath(); response.sendRedirect(contextPath+"/login.jsp"); } %>
purview1.jsp
<br/>
<a href="${pageContext.request.contextPath }/purview.jsp">purview.jsp</a>
<br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> purview2.jsp <br/> <a href="${pageContext.request.contextPath }/purview.jsp">purview.jsp</a> <br/> </body> </html>
相关文章推荐
- javaWeb项目用过滤器filter实现登陆成功后才能访问主页面,否则直接输入主页面的地址自动跳转到登陆界面
- filter 过滤器用户登录并判断是否属于电脑端或者手机端访问
- 在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)
- Servlet 过滤器Filter判断用户是否登陆(jsp页面)
- JS实现微信里判断页面是否被分享成功的方法
- 权限判断——用户登录成功是才能查看相应的信息,登录失败不能查看并且返回登录页面
- web 开发,个人中心每个请求,判断用户是否登录,若没有登录,则跳转到登录页面,登录成功后返回之前页面
- 过滤器(filter)判断用户是否登录
- 通过文本检查点判断是否登录成功
- 通过 JS 判断页面是否有滚动条的简单方法
- 通过过滤器Filter来完成登录访问权限限制
- 只有经过验证的用户才能访问某个页面实现方法
- javaweb过滤器filter-判断用户是否登录
- 过滤器用来拦截用户是否登录访问权限的问题(通过session来进行拦截)
- php 用户访问菜单页面,必须登录,判断用户是否登录
- 判断用户是否已登录,未登录用户禁止访问任何页面或action,自动跳转到登录页面
- 通过 JS 判断页面是否有滚动条的实现方法
- php 用户访问菜单页面,必须登录,判断用户是否登录
- 通过页面验证hadoop是否安装成功的方法
- 判断用户是否登录,用户在没有登录访问页面时,自动跳转到登录页面(二)