Answering my own email :-)

On Sun, Apr 28, 2013 at 6:07 PM, Leon Rosenberg <rosenberg.l...@gmail.com>wrote:

> Hello Konstantin,
> thank you for your reply.
>
>
> > Background, I want to count sessions by top level domains. I'm doing it
>> now
>> > in a combination of filter and listener. Filter for new sessions,
>> putting a
>> > mark for already counted sessions, and listener for destroyed session.
>> > However, I would like to get rid of the Filter, if its possible somehow.
>> > For that, I need to get user's ip adress somehow.
>> >
>>
>> Just two ideas:
>>
>> 1. You can use javax.servlet.ServletRequestListener instead of a filter.
>> 2. There is HttpSession.isNew() method.
>>
>
> So you mean that I can basically perform the same checks I do in a filter
> in a servletrequestlistener, checking if there is an attached session, if
> it's new, and where it comes from.
> I haven't found it in the spec (or didn't search hard enough, at least
> haven't found it in listener section), would ServletRequestListener being
> notified prior to any Filter execution?
>

So i made a combined HttpSessionListener and ServletRequestListener.
Advantages vs. Filter/Listener solution: only one class needed.
Disadvantage: Only second request is seen, because first request is
sessionless.

HttpSession.isNew() doesn't do what I'd expect it to do, in fact multiple
requests come in with same session and new status, so I will need to
synchronize on session object.
Here a small test listener:
public class TestListener implements HttpSessionListener,
ServletRequestListener{

private AtomicLong counter = new AtomicLong(0);

@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Session created "+se.getSession().getId());
}

@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("Request "+((HttpServletRequest)
sre.getServletRequest()).getAttribute("___requestId___")+" destroyed " +
((HttpServletRequest) sre.getServletRequest()).getRequestURI());
}

@Override
public void requestInitialized(ServletRequestEvent sre) {
long requestId = counter.incrementAndGet();
System.out.println("Request "+requestId+" created
"+((HttpServletRequest)sre.getServletRequest()).getRequestURI());
((HttpServletRequest)sre.getServletRequest()).setAttribute("___requestId___",
requestId);
HttpSession session =
((HttpServletRequest)sre.getServletRequest()).getSession(false);
System.out.println("Session? "+ (session!=null));
if (session!=null){
System.out.println("Session "+session.getId()+" new? "+session.isNew());
}
}


Output for http://localhost:8080/moskitodemo/mui/mskShowAllProducers:
Request 1 created /moskitodemo/mui/mskShowAllProducers
Session created 4B842C774B30EE7886CC7243758C7D38
Request 2 created /moskitodemo/mui/mskCSS
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? true
Request 3 created /moskitodemo/img/moskito_webui_logo.gif
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? true
Request 4 created /moskitodemo/js/wz_tooltip.js
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? true
Request 5 created /moskitodemo/js/jquery-1.4.min.js
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? true
Request 6 created /moskitodemo/js/function.js
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? true
Request 6 destroyed /moskitodemo/js/function.js
Request 5 destroyed /moskitodemo/js/jquery-1.4.min.js
Request 4 destroyed /moskitodemo/js/wz_tooltip.js
Request 3 destroyed /moskitodemo/img/moskito_webui_logo.gif
Request 7 created /moskitodemo/img/ind_green.png
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? false
Request 2 destroyed /moskitodemo/mui/mskCSS
Request 7 destroyed /moskitodemo/img/ind_green.png
Request 8 created /moskitodemo/img/bgs.gif
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? false
Request 8 destroyed /moskitodemo/img/bgs.gif
Request 9 created /moskitodemo/img/vline.png
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? false
Request 10 created /moskitodemo/img/ind_off_small.png
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? false
Request 9 destroyed /moskitodemo/img/vline.png
Request 11 created /moskitodemo/img/icons.png
Session? true
Session 4B842C774B30EE7886CC7243758C7D38 new? false
Request 11 destroyed /moskitodemo/img/icons.png
Request 10 destroyed /moskitodemo/img/ind_off_small.png
Request 1 destroyed /moskitodemo/mui/mskShowAllProducers

As you see there are multiple requests with session.isNew = true. I am not
sure if its proper behavior at least the docs aren't clear enough here, but
it's definitely not good enough to ensure that each session is called only
once.

regards
Leon

P.S. The above sequence is only reproduceable in chrome, not firefox





>
> regards
> Leon
>
>
>>
>> Best regards,
>> Konstantin Kolinko
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>
>>
>

Reply via email to