Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threading issues in JSP and Servlets

 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is written in books that there is only one service method which handles all the requests.here is my question.If I am creating a web site with high volume traffic then do I have to synchronize all the getter and setter methods.I mean suppose that there is a login page.A user enters his/her login name and password.On the server side we are using the setProperty action tag of the JSP.As soon as we do this the instance variables of the bean get set.But suppose another using is using getProperty action of the bean then don't you think there are chances that it might display the results of the first user.How is this handled?
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Sanjay,
I think the service method is excuted in synchronized state.You can chage the situation using the attribute isThreadSafe in jsp or implementing SingleThreadModel interface in your servlet
[This message has been edited by Anoop Krishnan (edited July 13, 2001).]
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to use the singleThreadModel interface as this will slow down the application tremendously.
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanj,
Every request to the service method of a servlet/JSP creates a new thread. Suppose, there are two simultaneous requests come, definitely there will be two concurrent threads will be running in that Servlet/Jsp Context.
You didn't mention from where you are gettin the values for getProperty.
if your are using a bean which access a database for validation,
there is no need of synchronization since most dbms has concurrent thread handling mechanisms.
If you are not using a database, you should use the synchronized method for the appropriate methods.

Thanks
Binu
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Binu and Anoop
Thanks a lot for ur reply.Both of you have helped me a lot but I have this problem still in my mind.Say there are two people logging at the same time.There names are Anoop Krishnan and Binu Kavumkal.say both of them enter the user name and password at the same time and this request is going to a jsp page.Now that jsp page has an action attribute as say
<jsp:setProperty name="classnameid" property="*">
this will set the name and password property of the bean.Now if two people are setting it at the same time don't you think that both the getter and setter methods should be synchronized.I hope you understans my problem now.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sanj,
If you use all of your variables & objects locally in the service method you need not bother about thread issues
eg
<%
String userName=request.getParameter("username");
String password=request.getParameter("password");
%>
if you do something with a class variable.then you will have problems
eg
<%!
String userName;
String password
%>
<%
userName=request.getParameter("username");
password=request.getParameter("password");
%>
because here two different threads are modifying the same references
Am i wright ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic