You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by mike niemaz <ni...@xrce.xerox.com> on 2001/02/15 09:40:39 UTC

one session per user?

Hi,
    I'm having troubles to understand how really sessions are working.
    I'm using a servlet to handle every client requests and I'm using
jsp
    & session to display & various objects between frames, such as:

    HttpSession session = req.getSession(false);
    Toto toto = session.getAttribute("toto");
    if (toto.getName() == "mike")
        toto.setInfotoDisplay("Hi mike, waza?");
    else if (toto.getName() == "alain")
        toto.setInfotoDisplay("Tcho alain, ca boom?");
    ...
    session.setAttribute("toto", toto);

    My concern is that I really thought that the object I
    get with "getAttribute" on sessions were attached
    to the according user.
    But apparently not, it looks like my object Toto is shared
    amongst my clients and thus everything is f**** up ;-(

    Any idea how I could stick a session per user?

thanx,

--mike


Re: one session per user?

Posted by mike niemaz <ni...@xrce.xerox.com>.
fumitada wrote:

> Hi there,
>
> First, Do u know how to code Java ? (No offence)
> I give u some tips...

;-) been coding java for ages ...
Although you're right my examples are full
of mistakes but I wrote it quick & the main
goal was to give the idea of the process.

--mike

nb: '==' is quicker to write than equals ;-)

>
> >HttpSession session = req.getSession(false);
> >    Toto toto = session.getAttribute("toto");
>      U didn't cast up there.
>      // should be Toto toto = (Toto) session.getAttribute("toto");
> >    if (toto.getName() == "mike")
>      == is for C language if you wanna know whether both letters are
> same.
>
>      // should be toto.getName().equals("mike");
> >        toto.setInfotoDisplay("Hi mike, waza?");
> >    else if (toto.getName() == "alain")
>      Here too.
> >        toto.setInfotoDisplay("Tcho alain, ca boom?");
> >    ...
> >    session.setAttribute("toto", toto);
>
> Maybe your problem is not session, but your coding.
> Probably you gotta learn syntax first...(No offence)
>
> Fumitada
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org


Re: one session per user?

Posted by fumitada <fu...@earthlink.net>.
Hi there,

First, Do u know how to code Java ? (No offence)
I give u some tips...
>HttpSession session = req.getSession(false);
>    Toto toto = session.getAttribute("toto");
     U didn't cast up there.
     // should be Toto toto = (Toto) session.getAttribute("toto");
>    if (toto.getName() == "mike")
     == is for C language if you wanna know whether both letters are
same.

     // should be toto.getName().equals("mike");
>        toto.setInfotoDisplay("Hi mike, waza?");
>    else if (toto.getName() == "alain")
     Here too. 
>        toto.setInfotoDisplay("Tcho alain, ca boom?");
>    ...
>    session.setAttribute("toto", toto);

Maybe your problem is not session, but your coding.
Probably you gotta learn syntax first...(No offence)

Fumitada

Re: one session per user?

Posted by Kief Morris <ki...@bitbull.com>.
mike niemaz typed the following on 09:40 AM 2/15/2001 +0100
>Hi,
>    I'm having troubles to understand how really sessions are working.
>    I'm using a servlet to handle every client requests and I'm using
>jsp
>    & session to display & various objects between frames, such as:
>
>    HttpSession session = req.getSession(false);
>    Toto toto = session.getAttribute("toto");
>    if (toto.getName() == "mike")
>        toto.setInfotoDisplay("Hi mike, waza?");
>    else if (toto.getName() == "alain")
>        toto.setInfotoDisplay("Tcho alain, ca boom?");
>    ...
>    session.setAttribute("toto", toto);
>
>    My concern is that I really thought that the object I
>    get with "getAttribute" on sessions were attached
>    to the according user.
>    But apparently not, it looks like my object Toto is shared
>    amongst my clients and thus everything is f**** up ;-(
>
>    Any idea how I could stick a session per user?

Normally sessions do work just fine per user. However it's easy to make
mistakes with scope which can produce the kinds of problems you're
talking about: static variables, class variables in a servlet class or
<%! Toto toto = new Toto() %> in JSP pages, or reusing the same
object by changing its values and inserting it into a session for a
different user, etc. For example, if you have

    Toto toto = new Toto();

    doGet(...) {
        toto.setName = ...
        session.setAttribute("toto", toto);
    }

In this case, the Toto object _will_ be shared between users, because
only one object is ever created.

But it's hard to say what the problem is without seeing the real code, or at
least a boiled down example of the code which you have compiled and verified 
still has the problem.

Kief