转载: http://www.zhangriguang.cn/blog/20121010123838.html
曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助!
前期准备:创建一个项目,并配置好一些需要的文件。
创建好项目后就创建一个类RequestFilter。
创建RequestFilter类是需要继承一接口,必须是javax.servlet包下的Filter接口。
创建好后在RequestFilter中创建线程:
//创建线程
threadLocalRequest、threadLocalResponse
在doFilter()方法中写
//把request、response传入
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;
public class RequestFilter implements Filter {
//创建线程
public static ThreadLocal<HttpServletRequest> threadLocalRequest = new ThreadLocal<HttpServletRequest>();
public static ThreadLocal<HttpServletResponse> threadLocalResponse = new ThreadLocal<HttpServletResponse>();
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
threadLocalRequest.set((HttpServletRequest) arg0);
threadLocalResponse.set((HttpServletResponse) arg1);
arg2.doFilter(arg0, arg1);
}
public void destroy() {}
public void init(FilterConfig arg0) throws ServletException {}
}
在类RequestFilter中写好代码后在WEB-INF目录下的web.xml文件中注册此类,
<filter>
<filter-name>RequestFilter</filter-name>
<filter-class>架包名.RequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
注册好后就可以在Java类中轻松获取自己在Action或页面上保存在Session中值,具体调用
//获取request
HttpServletRequest request = RequestFilter.threadLocal.get();
request.getSession().getAttribute("所保存的名称");
HttpServletRequest request = RequestFilter.threadLocal.get();这句话一定要放在方法里面,不能放在方法外面。
只是简单的一些操作,希望对你有一定的帮助!
曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助!
前期准备:创建一个项目,并配置好一些需要的文件。
创建好项目后就创建一个类RequestFilter。
创建RequestFilter类是需要继承一接口,必须是javax.servlet包下的Filter接口。
创建好后在RequestFilter中创建线程:
//创建线程
threadLocalRequest、threadLocalResponse
在doFilter()方法中写
//把request、response传入
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;
public class RequestFilter implements Filter {
//创建线程
public static ThreadLocal<HttpServletRequest> threadLocalRequest = new ThreadLocal<HttpServletRequest>();
public static ThreadLocal<HttpServletResponse> threadLocalResponse = new ThreadLocal<HttpServletResponse>();
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
threadLocalRequest.set((HttpServletRequest) arg0);
threadLocalResponse.set((HttpServletResponse) arg1);
arg2.doFilter(arg0, arg1);
}
public void destroy() {}
public void init(FilterConfig arg0) throws ServletException {}
}
在类RequestFilter中写好代码后在WEB-INF目录下的web.xml文件中注册此类,
<filter>
<filter-name>RequestFilter</filter-name>
<filter-class>架包名.RequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
注册好后就可以在Java类中轻松获取自己在Action或页面上保存在Session中值,具体调用
//获取request
HttpServletRequest request = RequestFilter.threadLocal.get();
request.getSession().getAttribute("所保存的名称");
HttpServletRequest request = RequestFilter.threadLocal.get();这句话一定要放在方法里面,不能放在方法外面。
只是简单的一些操作,希望对你有一定的帮助!