6

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();
}

3 Answers 3

16

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>
Sign up to request clarification or add additional context in comments.

8 Comments

do i have to use chain.doFilter(request, response); for further process B'cause i get blank browser screen.
sorry, my fault (i fully forgot that), I have just corrected it :)
-1: not threadsafe. Threads are pooled by the container and you're not properly releasing the threadlocal variable.
added removal of the request once the request is finished
I love you <3 you've just saved my life :)
|
7

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 a Filter (and clean it afterwards)
  • pass it as argument - either in constructor (if the object is instantiated on each request) or as method argument.

3 Comments

Passing the information of interest (and thus not the session itself!) is absolutely the best way.
But how do you set things in the session?
@ mjaggard doSomeBusinessLogic will return the map to store back in session.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.