• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

session management

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>");
}
}
}

}
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you opening the second instance of your browser?
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It will give me the powers of the gods. Not bad for a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic