Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get HttpSession|Request from simple java class not servlet class

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();
}
like image 210
Ravi Parekh Avatar asked May 03 '11 08:05

Ravi Parekh


People also ask

How do I find HttpSession?

How to get the HttpSession object ? The HttpServletRequest interface provides two methods to get the object of HttpSession: public HttpSession getSession():Returns the current session associated with this request, or if the request does not have a session, creates one.

Where is HttpSession stored Java?

Show activity on this post. HttpSession is a high level interface built on top of cookies and url-rewriting, which means that there is only a session ID is stored in client side and the data associated with it is stored in server side.

How does HttpSession work in Java?

Interface HttpSession. Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. The servlet container uses this interface to create a session between an HTTP client and an HTTP server.


1 Answers

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>
like image 88
Matej Tymes Avatar answered Sep 23 '22 02:09

Matej Tymes