程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> web.xml-新手求助:filter過濾器的問題

web.xml-新手求助:filter過濾器的問題

編輯:編程解疑
新手求助:filter過濾器的問題

web.xml配置

<filter>
        <filter-name>loginInterceptor</filter-name>
        <filter-class>com.cbt.Interceptor.LoginInterceptor</filter-class>
        <init-param>
            <param-name>loginPage</param-name>
            <param-value>main_login</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>loginInterceptor</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>loginInterceptor</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>loginInterceptor</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>cbtconsole</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cbtconsole</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

springmvc配置

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 資源映射:因為servlet-mapping會攔截所有請求.需要過濾掉靜態資源請求 -->
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />

過濾器代碼

@Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        String urlPath = httpServletRequest.getRequestURL().toString();
        String value = jedisClient.get("key1");
        System.out.println(urlPath+"---"+value);
        if (value !=null) {
            chain.doFilter(request, response);
        }else{
            httpServletResponse.sendRedirect(urlPath + loginPage);
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        //context-param –> listener –> filter –> servlet
        loginPage = config.getInitParameter("loginPage");
        ServletContext context = config.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        jedisClient = (JedisClient) ctx.getBean("jedisClient");  
    }

輸入localhost:8080進入過濾器後直接死循環了.
求大神指點

最佳回答:


是不是大神都不屑回答這種問題.小白真心求解答

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved