get HttpSession|Request from simple java class not servlet class
33,209
Solution 1
I don't think it's possible, to directly access session and request object. What you can do is pass the session and/or request object from servlet to a Java class either in some method or in constructor of the Java class.
Solution 2
call this:
RequestFilter.getSession();
RequestFilter.getRequest();
on your custom filter:
public class RequestFilter implements Filter {
private static ThreadLocal<HttpServletRequest> localRequest = new ThreadLocal<HttpServletRequest>();
public static HttpServletRequest getRequest() {
return localRequest.get();
}
public static HttpSession getSession() {
HttpServletRequest request = localRequest.get();
return (request != null) ? request.getSession() : null;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (servletRequest instanceof HttpServletRequest) {
localRequest.set((HttpServletRequest) servletRequest);
}
try {
filterChain.doFilter(servletRequest, servletResponse);
} finally {
localRequest.remove();
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
that you'll register it into your web.xml file:
<filter>
<filter-name>RequestFilter</filter-name>
<filter-class>your.package.RequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Solution 3
There are multiple ways to do that, but.. don't. Only your web layer should have access to the session. The other layers should only get the parameters from the session that it needs. For example:
service.doSomeBusinessLogic(
session.getAttribute("currentUser"),
session.getAttribute("foo"));
The options that you have to obtain the request, and from it - the session in a non-servlet class, that is still in the web layer:
- store the request in a
ThreadLocal
in aFilter
(and clean it afterwards) - pass it as argument - either in constructor (if the object is instantiated on each request) or as method argument.
Author by
Ravi Parekh
Updated on July 09, 2022Comments
-
Ravi Parekh almost 2 years
i want session Object not in servlet class but ordinary from we application.
WEB.XML
<listener> <listener-class>com.abc.web.ApplicationManager</listener-class> </listener> <listener> <listener-class>com.abc.web.SessionManager</listener-class> </listener>
ViewPrices.java
public class ViewPrices implements Cloneable, Serializable { Session session = request.getSession(); servletContext.getSession() anyWay.getSession(); }
-
Ravi Parekh about 13 yearsdo i have to use chain.doFilter(request, response); for further process B'cause i get blank browser screen.
-
Matej Tymes about 13 yearssorry, my fault (i fully forgot that), I have just corrected it :)
-
BalusC about 13 years-1: not threadsafe. Threads are pooled by the container and you're not properly releasing the threadlocal variable.
-
BalusC about 13 yearsPassing the information of interest (and thus not the session itself!) is absolutely the best way.
-
Matej Tymes about 13 yearsadded removal of the request once the request is finished
-
Nicolas HENAUX almost 10 yearsI love you <3 you've just saved my life :)
-
mjaggard over 8 yearsBut how do you set things in the session?
-
bigfoot almost 8 yearsWorked well for me. One change I made was to use InheritableThreadLocal instead, so that it works with children threads, too.
-
Ravi Parekh over 6 years@ mjaggard
doSomeBusinessLogic
will return the map to store back in session. -
Keshan Fernando over 6 yearsIt's working. But I use Primefaces, so that I can also get session from ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession();