Filter过滤,未登录的用户页面跳转

package com.dog.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class OnlineFilter extends HttpServlet implements Filter {

	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	
		/*RequestDispatcher dispatcher = request
				.getRequestDispatcher("../index.jsp");*/
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
	    String path = req.getContextPath();
	    String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;
		HttpSession session = req.getSession(true);
		String username = (String) session.getAttribute("username");
 		if (username == null || "".equals(username)) {
			
			resp.setHeader("Cache-Control", "no-store");
			resp.setDateHeader("Expires", 0);
			resp.setHeader("Prama", "no-cache");
			resp.sendRedirect(basePath+"/index.jsp");
		} else {
			chain.doFilter(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");
	}

}


package com.dog.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class OnlineFilter extends HttpServlet implements Filter {

	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	
		/*RequestDispatcher dispatcher = request
				.getRequestDispatcher("../index.jsp");*/
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
	    String path = req.getContextPath();
	    String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;
		HttpSession session = req.getSession(true);
		String username = (String) session.getAttribute("username");
 		if (username == null || "".equals(username)) {
			
			resp.setHeader("Cache-Control", "no-store");
			resp.setDateHeader("Expires", 0);
			resp.setHeader("Prama", "no-cache");
			resp.sendRedirect(basePath+"/index.jsp");
		} else {
			chain.doFilter(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");
	}

}


<filter>
	 	<filter-name>OnlineFilter</filter-name>
	 	<filter-class>com.dog.filter.OnlineFilter</filter-class>
	 </filter>
	 <filter-mapping>
	 	<filter-name>OnlineFilter</filter-name>
	 	<url-pattern>/main/*</url-pattern>
	 </filter-mapping>

上午在完成这部分功能的时候遇到一个问题。Filter过滤的太多了。点击提交按钮,不是先提交到action.而是先提交到filter。 这样filter的session就取不到值,无论填写什么信息都会因为session是null而跳转到index.jsp产生循环跳转的问题。
解决方法就是 将登录过滤的filter的url加上路径,然后将需要过滤的页面放到相应的文件夹下,这样就能过滤指定的url地址啦。 访问index.jsp的时候就不会过滤。 不错。 又解决了一个问问。慢慢来。切莫浮躁。

你可能感兴趣的:(filter)