登录时使用过滤器以及遇上的样式失效问题

java代码:

package com.tci.login;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 
 * @author dell
 */
public class LoginFilter implements Filter {

    private String encoding;

	public FilterConfig filterConfig;

    public void init(FilterConfig config) throws ServletException {
		encoding = config.getInitParameter("requestEncoding");
		if (encoding == null) {
			encoding = "UTF-8";
		}
		filterConfig = config;
	}

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain next) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
//		response.setContentType("text/html;charset=" + encoding);
		HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);

		// check login
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(true);
		User user = (User) session.getAttribute("user");
		String url = req.getRequestURI();

		String loginStrings = filterConfig.getInitParameter("loginStrings");
		String includeStrings = filterConfig.getInitParameter("includeStrings");
		String redirectPath = filterConfig.getInitParameter("redirectPath");
		String disabletestfilter = filterConfig.getInitParameter("disabletestfilter");

		redirectPath = req.getContextPath() + redirectPath;
		if (disabletestfilter.toUpperCase().equals("Y")) {
			next.doFilter(request, response);
			return;
		}
		String[] loginList = loginStrings.split(";");
		String[] includeList = includeStrings.split(";");
		if (!(url.contains("css")||url.contains("js"))) {
			response.setContentType("text/html;charset=utf-8");
		}

		if (!this.isContains(url, includeList)) {// 只对指定过滤参数后缀进行过滤
			next.doFilter(request, response);
			return;
		}

		if (this.isContains(url, loginList)) {// 对登录页面不进行过滤
			next.doFilter(request, response);
			return;
		}

		if (user == null) {
			wrapper.sendRedirect(redirectPath);
			return;
		}else {
			next.doFilter(request, response);
			return;
		}


//		if(url.endsWith("login.jsp")
			|| url.endsWith("login.html")
//				|| url.endsWith("login.action")
			|| url.endsWith("login.ajax")
//				|| url.equals("/")){
//			next.doFilter(request, response);
		return;
//		}else if(url.endsWith(".css")
//				|| url.endsWith(".js")
//				|| url.endsWith(".jpg")
//				|| url.endsWith(".gif")
//				|| url.endsWith(".png")
//				|| user != null){
//			next.doFilter(request, response);
//		}else {
//			request.getRequestDispatcher("/login.jsp").forward(request, response);
		return;
//		}
    }
	public static boolean isContains(String container, String[] regx) {
		boolean result = false;

		for (int i = 0; i < regx.length; i++) {
			if (container.indexOf(regx[i]) != -1) {
				return true;
			}
		}
		return result;
	}
    public void destroy() {
		this.filterConfig = null;
	}
}

web.xml

     <filter>
        <description>登录过滤器</description>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.tci.login.LoginFilter</filter-class>
        <init-param>
            <param-name>requestEncoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>loginStrings</param-name><!-- 对登录页面不进行过滤 -->
            <param-value>/login.jsp;/login.action</param-value>
        </init-param>
        <init-param>
            <param-name>includeStrings</param-name><!-- 只对指定过滤参数后缀进行过滤 -->
            <param-value>.jsp;.html;.htm;.action;.ajax</param-value>
        </init-param>
        <init-param>
            <param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 -->
            <param-value>/login.jsp</param-value>
        </init-param>
        <init-param>
            <param-name>disabletestfilter</param-name><!-- Y:过滤无效 -->
            <param-value>N</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

解决登录之后出现的样式问题的代码是:

if (!(url.contains("css")||url.contains("js"))) {
          response.setContentType("text/html;charset=utf-8");
}

其实在doFilter方法里就设置过这行代码,但是仍然是样式不生效,后来根据代码搜了下css和js的内容类型,结果发现如下:

文件扩展名Content-Type(Mime-Type)
.jsapplication/x-javascrip
.csstext/css

直接设置response.setContentType("text/html;charset=utf-8")的话,js和css估计就不生效了。。

设置session过期时间,单位是分钟,位置在web.xml中:

<session-config>
    <session-timeout>5</session-timeout>
</session-config>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值