Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why I get new session with new request from new browser window on same machine?

 
Bahadar Khan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I am not sure, what I am doing wrong here but using the following code, when ever I make a request from new browser window from the same machine it starts a new session. But if I repeatedly make requests from one window it doesn't starts new but keeps on adding info in the old session object as per logic. I am using Tomcat 5. Please see the code of servlet and jsp that calls it. And I would appreciate if somebody could tell me why its happening. As per my understanding requests made from same machine, no matter from different browser windows should be pointing to the same Session object.


----------------------------
SERVLET CODE
----------------------------


package sessionpkg;

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

public class Sessions extends HttpServlet{


String str="";

public void doPost(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException, ServletException{


String str1=req.getParameter("snail");

HttpSession session=req.getSession();

String str=(String)session.getAttribute("track");

if(str==null)str="";

str=str+str1;

session.setAttribute("track",str);


RequestDispatcher rd=req.getRequestDispatcher("/index.jsp");
rd.forward(req,res);


}


}

-----------------------------
Calling JSP CODE ( index.jsp)
-----------------------------

<%=(String)request.getSession().getAttribute("track")%>


<HTML>

<form method=POST action="sessions">


Input a character <input type="text" name="snail">
<input type="SUBMIT">


</form>

</HTML>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic