赞
踩
先写一个filter:(注意要在登录页面的servlet中保存一下sesson)
- package Easyui_Filter;
-
- import java.io.IOException;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- 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 LoginFilter implements Filter{
- private FilterConfig config = null;
- public void destroy() {
- this.config = null;
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpSession session = ((HttpServletRequest) request).getSession();
- String a = String.valueOf(session.getAttribute("name"));
- //System.out.println(a);
- //System.out.println("进入过滤器");
- if (a.equals(new String("null"))) {
- //System.out.println("跳转前");
- ((HttpServletResponse)response).sendRedirect("../test/login.jsp");
- //System.out.println("跳转后");
- } else {
- chain.doFilter(request, response);
- }
- }
- public void init(FilterConfig config) throws ServletException {
- this.config = config;
- }
- }
然后在xml文件中添加配置:
- <filter>
- <filter-name>LoginFilter</filter-name>
- <filter-class>Easyui_Filter.LoginFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>LoginFilter</filter-name>
- <url-pattern>/test/Panel.jsp</url-pattern>
- </filter-mapping>
其中url是你需要过滤的网页的url,如果对所有的jsp文件过滤, 则写成:*.jsp,若对某一文件夹下文件进行过滤,则写成:/文件夹名/*
此例中注意这句:a.equals(new String("null"))
因为valueOf得到的name是object类型的,所以如果name为null,a得到的值应该是"null" 而不是null,所以就不能用a==null,a.equals(null)来判断了。
而本例中也不能用a=="null" a.equals("null") ,z至于这是为什么,笔者也不知道了,鉴于笔者很菜,希望知道的大侠告诉我一下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。