方式一 :利用ServletRequestListener实现
package com.fawa.test;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
public class RequestHolder implements ServletRequestListener {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder =
new ThreadLocal<HttpServletRequest>();
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
httpServletRequestHolder.set(request); // 绑定到当前线程
}
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
httpServletRequestHolder.remove(); // 清理资源引用
}
public static HttpServletRequest getHttpServletRequest() {
return httpServletRequestHolder.get();
}
public static void main(String[] args) {
HttpServletRequest httpServletRequest = RequestHolder.getHttpServletRequest();
}
}
方式二:利用Filter实现
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;
public class RequestHolder implements Filter {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder =
new ThreadLocal<HttpServletRequest>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
httpServletRequestHolder.set((HttpServletRequest) request); // 绑定到当前线程
try {
chain.doFilter(request, response);
} catch (Exception e) {
throw e;
} finally {
httpServletRequestHolder.remove(); // 清理资源引用
}
}
@Override
public void destroy() {
}
public static HttpServletRequest getHttpServletRequest() {
return httpServletRequestHolder.get();
}
}
方式三:利用SpringMVC的拦截器实现
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class RequestHolder extends HandlerInterceptorAdapter {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder =
new ThreadLocal<HttpServletRequest>();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
httpServletRequestHolder.set(request); // 绑定到当前线程
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex)
throws Exception {
httpServletRequestHolder.remove(); // 清理资源引用
}
public static HttpServletRequest getHttpServletRequest() {
return httpServletRequestHolder.get();
}
}
方式四:org.springframework.web.context.request.RequestContextHolder
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
Object attribute = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getAttribute("key", RequestAttributes.SCOPE_SESSION);