×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Mar 2006
    Posts
    76

    Question Problem while maintaining the Session

    Hi friends,
    I'm building chatServer in j2me using Servlet/Jsp.So while doing so i want to maintain a session.

    For that i've invoke the following method,

    HttpSession session = request.getSession(true);(in server)

    But from that same MIDlet when i again send some text it again create new session id.
    How could i resolve this issue as i just want to use same MIDlet when sending data to server?
    Do i use diffrent Servlet/Jsp( at server side) when i going to send data after my session maintain?
    But how could this new Servlet/Jsp( at server side) page get the same session id ?
    Is there any other to resolve this ( want use only Session Tracking API)?

    Regards,
    Sachin Warang.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Problem while maintaining the Session

    Hi Sachin,

    In JEE, you can use cookies, URL-rewritting or hidden form fields for session tracking. I think you can use URL-rewritting...the doc below has some discussions related to it.

    An Introduction to Secure MIDlet Communication in MIDP 1.0
    http://forum.nokia.com/info/sw.nokia..._0_en.pdf.html

    "URL-rewriting is a way to track users who have cookies disabled while they are at your site. It is implemented by adding extra path information to the end of each URL, but the servlet session-tracking API takes care of the details automatically. If you return any URLs to the clients that reference your site and URL-rewriting is being used, you need to attach the session information to the URLs.

    If you are using URL-rewriting for session tracking and you send a URL that references your site to the client, you need to explicitly add on the session data. Since the servlet will automatically switch to URL-rewriting when cookies aren’t supported by the client, you should routinely encode all URLs that reference your site. There are two possible places you might use URLs that refer to your own site. The first is when the URLs are embedded in the Web page that the servlet generates. These URLs should be passed through the encodeURL method of HttpServletResponse. The method will determine if URL-rewriting is currently in use and append the session information only
    if necessary. The URL is returned unchanged otherwise."

    Hope it helps!

    BR,

    Juarez Jr

  3. #3
    Regular Contributor
    Join Date
    Mar 2006
    Posts
    76

    Re: Problem while maintaining the Session

    Hi friends,
    I've used following code at server side for setting Cookies in run method

    public void run()
    {

    cSending = true;
    while(cSending)
    {
    display("Obtaining HttpConnection object..." );

    try {
    HttpConnection conn = HttpConnectionHelper.connect(url, this );

    display("Connecting to the server..." );

    int rc = conn.getResponseCode();

    if( rc == HttpConnection.HTTP_OK )
    {

    if (mSessionIDCookie != null)
    conn.setRequestProperty("Cookie", mSessionIDCookie);

    InputStream in = conn.openInputStream();

    String cookie = conn.getHeaderField("Set-Cookie");
    if (cookie != null) {
    int semicolon = cookie.indexOf(';');
    mSessionIDCookie = cookie.substring(0, semicolon);
    }

    int length = (int)conn.getLength();
    System.out.println("Length : "+length);
    byte[] raw = new byte[length];
    in.read(raw);

    String s = new String(raw);
    Alert a = new Alert("Response", s, null, null);
    a.setTimeout(Alert.FOREVER);
    mDisplay.setCurrent(a, mMainForm);
    done("Response is:\n" +
    s.toString() );
    sendData = false;

    in.close();
    conn.close();

    } else {
    done(
    "Unexpected return code: " + rc );
    }

    //wait before sending another request
    //Thread.currentThread().sleep(1000);
    //Continuosly sending request to server at regular interval
    Thread.currentThread().sleep(15000);

    }
    catch( IOException ioe ){
    done( "Exception " + ioe +
    " trying to connect." );
    }
    catch (Exception e)
    {
    done( "Exception " + e +
    " trying to connect." );
    }
    }


    }



    While at server side used the following code

    HttpSession session = request.getSession(true);
    // session.setMaxInactiveInterval(-1);
    ;
    Object user = session.getAttribute("User-Information");
    System.out.println("User : "+((String)user));
    if (user == null)
    {
    // Retrieve the user ID which is passed to us as a parameter.
    String id = request.getParameter("user");
    System.out.println("Id : "+id);
    // Now look up the corresponding user information.
    user = lookupUser(id);

    // Put the user information into the session.
    session.setAttribute("User-Information", user);
    }

    // Now process the request, using the user information we
    // just looked up to customize our behavior. Write the
    // response back to the client.

    // For now, just compose a suitable reply message.
    String message;
    if (session.isNew())
    {
    message = "New session created: " + session.getId();

    }
    else
    {
    message = "Used existing session: " + session.getId();

    }
    // Send the message to the client.
    response.setContentType("text/plain");
    response.setContentLength(message.length());
    response.setStatus( response.SC_OK);
    PrintWriter out = response.getWriter();
    out.println(message);
    out.close();


    Where it is always created new session id .
    While returning to client it throws IOException ?

    What would be the possible reason?and could i resolved this issue?

    Regards,
    Sachin Warang.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Problem while maintaining the Session

    Hi Sachin Warang,

    By using HttpSession session = request.getSession(true); you'll always get a new session if there's not one in place. From the Javadocs:

    public HttpSession getSession(boolean create)

    "Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
    If create is false and the request has no valid HttpSession, this method returns null."


    Note that this method is overloaded, so you can use the other version:

    public HttpSession getSession()


    "Returns the current session associated with this request, or if the request does not have a session, creates one."

    Hope it helps!

    BR,

    Juarez Junior

  5. #5
    Regular Contributor
    Join Date
    Mar 2006
    Posts
    76

    Question Re: Problem while maintaining the Session

    Hi friend,
    I've change the
    HttpSession session = request.getSession(true); to
    HttpSession session = request.getSession();

    the problem remains as it is?

    On the client side my Client.java class uses HttpConnectionHelper.java class to connect to specified server which opens the connection with server.
    Does this cause problem while maintaing the session?
    If so what will be the solution?
    And if not wht would be cause that every time it creates new session id?

    Regards,
    Sachin Warang.

Similar Threads

  1. problem when stop playing video on 3650 and try to play again
    By niko86 in forum Mobile Java General
    Replies: 5
    Last Post: 2007-08-03, 07:40
  2. A problem of installing a session observer from inside MDL?
    By hesham.galal in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-01-01, 19:02
  3. problem with session object
    By sash_mca in forum Browsing and Mark-ups
    Replies: 2
    Last Post: 2003-06-20, 10:03
  4. 8310, 6310,i drops data session over IR after approx 2 min
    By Gumby22 in forum PC Suite API and PC Connectivity SDK
    Replies: 1
    Last Post: 2002-06-19, 15:10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×