You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mohsen Saboorian <mo...@gmail.com> on 2006/08/09 14:04:16 UTC

Initializing session in a Filter

Hi,
I have a filter (for /*) in which I'm caching something in the
session. Here is a piece of code:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) {
	HttpServletRequest req = (HttpServletRequest) request;
	HttpSession session = req.getSession(false);
	if (session == null) {
		System.out.println("Create a new session: " + new java.util.Date());
		session = req.getSession();
	}
	// cache something
}

The first time I make a request, since req.getSession(false) returns
null, a session is created by a call to req.getSession() manually.
The second time I make a request to the server, req.getSession(false)
returns null and so a session is created again.
In all other request (third, ...) the last created session (second) is returned!

My question is why session is created two times?

Thanks in advance.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Initializing session in a Filter

Posted by Mohsen Saboorian <mo...@gmail.com>.
Better to change my question:

When a session associated with a client is created?


> Hi,
> I have a filter (for /*) in which I'm caching something in the
> session. Here is a piece of code:
>
> public void doFilter(ServletRequest request, ServletResponse response,
> FilterChain chain) {
>         HttpServletRequest req = (HttpServletRequest) request;
>         HttpSession session = req.getSession(false);
>         if (session == null) {
>                 System.out.println("Create a new session: " + new java.util.Date());
>                 session = req.getSession();
>         }
>         // cache something
> }
>
> The first time I make a request, since req.getSession(false) returns
> null, a session is created by a call to req.getSession() manually.
> The second time I make a request to the server, req.getSession(false)
> returns null and so a session is created again.
> In all other request (third, ...) the last created session (second) is returned!
>
> My question is why session is created two times?
>
> Thanks in advance.
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org