aspose file tools*
The moose likes Servlets and the fly likes session management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session management" Watch "session management" New topic
Author

session management

amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
hi all, please help me
its simple but i am not getting where i am making mistake
problem:
when i am calling httpsessionservlet (myfile)
through two different browser that means two different client
they are sharing session that means they are incrementing same value of i
client-1 increments i - 1,2
then client-2 increments -3,4
code is here
//*** html file
html>
<form action="http://192.168.1.23:7001/MyWeb/httpsessionservlet.com">

Enter book name: <input type=text name="bname">
<input type=submit name="sub" value="Submit">

<input type=submit name="sub" value="Display Data">
</form>
</html>

//*****java file

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpSessionServlet extends HttpServlet
{
int i=0;
int flag=0;
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
RequestDispatcher rd;
PrintWriter out=resp.getWriter();
resp.setContentType("text/html");
String sub=req.getParameter("sub");
if (sub.equals("Submit"))
{
HttpSession session=req.getSession(true);
String bname=req.getParameter("bname");
i++;
session.setAttribute(bname, new Integer(i));
rd=req.getRequestDispatcher("HttpSessionServlet.html");
rd.include(req,resp);
//flag=1;
}
if(sub.equals("Display Data"))
{
HttpSession session=req.getSession(false);
Enumeration en=session.getAttributeNames();
while (en.hasMoreElements())
{
String t=(String)en.nextElement();
out.println((Integer)session.getAttribute(t)+"<br>");
}
}
}

}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

amod gole,
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How are you opening the second instance of your browser?
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
thaks for reply,

i am opening new browser , i am not using ctrl+n
even if i call that servlet from different machine then also session is get shared bet them
and one more thing if insted of 'i' if i am passing bname that time it works fine
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ahh, I did not read your code closley enough the first time (as the tip hinted, I usually skim past unformatted code).

Your sessions are not being shared.
You're writing to instance variables from your service methods.
This means your code is not thread-safe and the problem you're having is a result.




A simple rule of thumb to follow (until you fully understand threading in servlet apps):
Never use instance variables.
Declare all of your variables in your service (doPost, doGet) methods.
[ June 30, 2006: Message edited by: Ben Souther ]
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
thanks... for reply

one object multiple instance or multiple thread and all thread shares instance variable of that object on which thread is created

i got
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The second is closer.

The container will create one instance of your servlet.
All requests will share that same instance.
Any instance variables will be common to all requests.

Declaring your variables inside your servlet's service methods (doGet, doPost, etc) will insure that your app is thread safe.
[ June 30, 2006: Message edited by: Ben Souther ]
muralidhar Reddy
Greenhorn

Joined: Jul 04, 2006
Posts: 4
web container doesnot provide any concurrency control for the servlet. If any part of servlet or the data being modified,in order to provide the concuurency
control this is the resposneblity of the developer. u can put the data which is required to be thread safe keep it in the sychronize block.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42916
    
  68
Hello muralidhar-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it; specifically, a first name and a last name are required. Please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session management