struts2、jsp的简单路径的简单拦截

  <filter>
<filter-name>UsersFilter</filter-name>
<filter-class>com.web.UsersFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UsersFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
package com.web;
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 UsersFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 设置请求的字符编码
request.setCharacterEncoding("UTF-8");
// 设置返回请求的字符编码
response.setCharacterEncoding("UTF-8");
// 转换ServletRequest为 HttpServletRequest
HttpServletRequest req = (HttpServletRequest) request;
// 转换ServletResponse为HttpServletRequest
HttpServletResponse res = (HttpServletResponse) response;
// 获取Session
HttpSession session = req.getSession();
// 获取Session中存储的对象
Object o = session.getAttribute("user");
// 获取当前请求的URI
String url = req.getRequestURI();
// 判断Session中的对象是否为空;判断请求的URI是否为不允许过滤的URI
if (o==null && url.indexOf("test.jsp")<0
&& url.indexOf("comment.jsp")<0
&& url.indexOf("link.jsp")<0) {
chain.doFilter(request, response);
res.setHeader("Cache-Control","no-store");
res.setDateHeader("Expires",0);
res.setHeader("Pragma","no-cache");
res.flushBuffer();
}
} public void destroy() {
// TODO Auto-generated method stub } public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub } }

****************************************************************************************

struts2、jsp的简单路径的简单拦截

上一篇:Java 之外,是 Scala 还是 Groovy?【转载】


下一篇:MyBatis学习总结(七)——Mybatis缓存