Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding Javas HttpSession
    primarykey
    data
    text
    <p>I have a little problem with HttpSession. I use jetty 8 and try to implement an embedded web server.</p> <p>For testing purpose i have created two servlets:</p> <ul> <li>FileServlet: It simply handles HTTP GET for static content, like index.html, css and other static files and prints the current session id on the console:</li> </ul> <blockquote> <p>System.out.println("File session: "+request.getSession(true).getId() + " path "+request.getPathInfo());</p> </blockquote> <ul> <li>TestServlet: Does nothing, it simply prints the current session id on the console on a doGet(...)</li> </ul> <blockquote> <p>System.out.println("Test session: "+request.getSession(true).getId() + " path "+request.getPathInfo());</p> </blockquote> <p>As far as i know the Servlets must be part of the same ServletContext to handle Sessions correctly. So i implement it programmatically this way:</p> <pre><code>Server server = new Server(8081); ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS); servletContext.setContextPath("/"); servletContext.addServlet(new ServletHolder(new FileServlet()),"/*"); servletContext.addServlet(new ServletHolder( new TestServlet()),"/servlets/test"); server.setHandler(servletContext); </code></pre> <p>Now I start my web-browser (Chrome btw.) clear browsers cookies and browsers chache and load my test site <a href="http://localhost:8081/index.html" rel="nofollow">http://localhost:8081/index.html</a> which will also request to load other static files like css and js.</p> <p>So now i get this output:</p> <pre><code>File session: ok49uty1mshn1fnhyle9ojh3n path /index.html File session: 1fuxdd50woxwv1gl9r96dfdxgl path /css/style.css File session: 1f6m5dkht4xja5ryj7dgd93wu path /css/Loading.css File session: 1qizt3gnyy58uj0u46kofbxdf path /css/photo.css File session: ikzpmgyew49uwxlnt8lr839m path /css/Login.css File session: pg6p843xuqak1uwcc68q8wnxx path /js/script.nocache.js File session: 1uv2jmvpu5u6s127rpc01ef8 path /js/script2.js </code></pre> <p>After index.html has been loaded and every other resource that is embedded in index.html (css and js) i call <a href="http://localhost:8081/servlets/test" rel="nofollow">http://localhost:8081/servlets/test</a> and i see this output:</p> <pre><code>Test session: 1uv2jmvpu5u6s127rpc01ef8 path /servlets/test </code></pre> <p>So it seems to me that it takes some time to establish a session and i am wondering why and how to let the first session that has been created by the HTTP GET of /index.html be the session for every futher server interaction. Its very strange.</p> <p>I also make a second experiment: As a said before i cleared the browsers cache and cookies before loading /index.html. This time i reload the page without clearing the cache and cookies, and now it works correctly or in other words, how i expect to work:</p> <pre><code>File session: 1uv2jmvpu5u6s127rpc01ef8 path /index.html File session: 1uv2jmvpu5u6s127rpc01ef8 path /css/style.css File session: 1uv2jmvpu5u6s127rpc01ef8 path /css/Loading.css File session: 1uv2jmvpu5u6s127rpc01ef8 path /css/photo.css File session: 1uv2jmvpu5u6s127rpc01ef8 path /css/Login.css File session: 1uv2jmvpu5u6s127rpc01ef8 path /js/script.nocache.js File session: 1uv2jmvpu5u6s127rpc01ef8 path /js/script2.js </code></pre> <p>And also /servlets/test have the same session:</p> <pre><code>Test session: 1uv2jmvpu5u6s127rpc01ef8 path /servlets/test </code></pre> <p>EDIT: i understand that my browser doesnt wait until the index.html is loaded completly and start to load the css and js in the meantime... But I really need to be able to determine from the first HTTP GET the "real" session for some security reasons. Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload