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();
}
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With