Avatar billede fredand Forsker
30. september 2004 - 07:49 Der er 8 kommentarer og
1 løsning

How is the session id handled with out cookies?

Hello!

I have a question about Sessions with Servlets. I'm using the orion webbserver but I do not think that matters.

I disabled the cookies in the browser , booth IE and Konquer, like "Block".
And then I put an object in the session.

To get it to work I rewrite every link with: <a href="MyServlet;jsession=ASGFGD..." I think you know what I mean.

That did the trick. After this I could use my session even though the cookies was blocked by the Browser.

And now you guys perhaps wondering what I'm whining about....

In the servlet MyServlet I tested this:

System.out.println( "sessionid: " + request.getRequestedSessionId() );
System.out.println( "from cookie: " + request.isRequestedSessionIdFromCookie() );
System.out.println( "from url: " + request.isRequestedSessionIdFromURL() );

AND BOOTH the 2 last lines prints "false";

Then, how is the server handling the sessionids?????

I thought that it would use either one of these 2 methods.

So if any one could explaine this it would be great.

Best regards
Fredrik
Avatar billede arne_v Ekspert
30. september 2004 - 07:58 #1
There are only to ways: cookies and URL rewriting.

But I somehow suspect that the app-server has not recognized that you are using
URL rewriting.

You are not supposed to write:
  <a href="MyServlet;jsession=ASGFGD..."

But instead use:
  response.encodeURL("MyServlet")
Avatar billede fredand Forsker
30. september 2004 - 08:20 #2
Hm! Sounds interesting.

Yes I must say that my method of url-rewritting is a lot of work.

Perhaps you could give me an example cause what I have is a servlet that through PrintWriter sends a HTML response. I do not like to use JSP.

Suppose we have something like this:

response.setContentType("text/html");
PrintWriter out = new PrintWriter(response.getOutputStream());
String string = "<html><head></head><body><a href=\"MyServlet\">MyServlet</>";
out.println(string);
out.flush();

Is it possible to use response.encodeURL here at all?

/Fredrik
Avatar billede arne_v Ekspert
30. september 2004 - 08:35 #3
Yes.

response.setContentType("text/html");
PrintWriter out = new PrintWriter(response.getOutputStream());
String string = "<html><head></head><body><a href=\"" + response.encodeURL("MyServlet") + "\">MyServlet</>";
out.println(string);
out.flush();
Avatar billede arne_v Ekspert
30. september 2004 - 08:35 #4
BTW I find it often usefull to use single quotes for HTML in Java.

response.setContentType("text/html");
PrintWriter out = new PrintWriter(response.getOutputStream());
String string = "<html><head></head><body><a href='" + response.encodeURL("MyServlet") + "'>MyServlet</>";
out.println(string);
out.flush();
Avatar billede arne_v Ekspert
30. september 2004 - 08:36 #5
You should ofcouse be aware of that for the very first page in a session
the app-server does not know whether it is to use cookies or URL rewriting.
Avatar billede fredand Forsker
30. september 2004 - 08:38 #6
So it should be that easy!

I will try that at once and comming back during the day. Just give a "svar" so I can give you the points!

Fredrik
Avatar billede arne_v Ekspert
30. september 2004 - 08:39 #7
.
Avatar billede arne_v Ekspert
30. september 2004 - 08:41 #8
It is.

The only thing is that for servlets you need to create the session manually with

request.getSession(true)

where JSP creates it automatically.
Avatar billede fredand Forsker
30. september 2004 - 10:47 #9
Hello!

I tried what you said and it really seems that my orion server do not understand that the session id is comming from the url. So my guess is that this is a bug in orion 1.6. That is not the latest orion so perhaps this is fixed. I will try to find out and get back to you.

Thanks for your help!

Best regards
Fredrik

Below is 2 Servlet that I tried this with and it stills write false for booth methods.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestServlet1 extends HttpServlet
{
    PrintWriter out;
    HttpSession session;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        try
        {
            response.setContentType("text/html");
            out = new PrintWriter(response.getOutputStream());
            session = request.getSession();
        }
        catch( IOException ioe )
        {
        }

        System.out.println( "sessionid: " + request.getRequestedSessionId() );
        System.out.println( "from cookie: " + request.isRequestedSessionIdFromCookie() );
        System.out.println( "from url: " + request.isRequestedSessionIdFromURL() );

        String output = "<html><head></head><body><a href=\"" + response.encodeURL("TestServlet2") + "\">TestServlet2</a></body></html>";

        out.println(output);
        out.flush();
        out.close();
    }
}


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestServlet2 extends HttpServlet
{
    PrintWriter out;
    HttpSession session;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        try
        {
            response.setContentType("text/html");
            out = new PrintWriter(response.getOutputStream());
            session = request.getSession();
        }
        catch( IOException ioe )
        {
        }

        System.out.println( "sessionid: " + request.getRequestedSessionId() );
        System.out.println( "from cookie: " + request.isRequestedSessionIdFromCookie() );
        System.out.println( "from url: " + request.isRequestedSessionIdFromURL() );

        String output = "<html><head></head><body><a href=\"" + response.encodeURL("TestServlet1") + "\">TestServlet1</a></body></html>";

        out.println(output);
        out.flush();
        out.close();
    }
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester