473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Threading - ASP. Net vs JSP/Servlets

Hi -

I have a question for someone who has experience with both ASP .Net and
JSP/Servlets, relating specifically to thread safetly.

In JSP, we say that instance vaiables of a servlet are not thread safe.
Several threads could be running the same servlet at the same time, because
a new thread of a servlet - as opposed to a new servlet itself - is created
by requests for the servlet. Since there is only one instance of the
servlet object, there is only one set of instance variables, and each thread
could set them to values that are in conflict with those in another thread.
However, you can write thread-safe servlets by avoiding instance variables,
or by judicious use of the synchronize(){ } construction.

It follows from this that instance variables in JSP pages are also not
thread safe, since the JSP is simply turned into a servlet. In theory, if
two people are running the same JSP page at the very close to the same time
the second person to access the page could re-set the value of an instance
variable to something that causes improper results for the first person, who
may be only part way through the execution of the page. I believe there is
a directive you can use to "sychronize" the whole page, although many
authorities seem to discourage this.

Can anyone tell me if there is a similar issue with ASP .Net pages,
specifically variables in the "code-behind" page? Is there something
analagous to a "servlet" that is created by ASP .Net, of which only one copy
exists that many users are executing in different threads? Is there the
possibility that users will "step on" each other's instance variables (as
there is only one instance of the object)?

Regards to all -

Peter Beck
Nov 18 '05 #1
4 1403
Peter,

I'm just beginning to learn JSP (the company I work for bought me a
Powerbook G4 to play with about a month ago and I'm just getting started),
so I may not be the best person to answer this, but I think I can.

Each ASP.Net page is threadsafe. All variables in the code-behind page are
threadsafe. The best equivalent to a servlet, of which only one copy exists
that many users are executing in different threads, that I can think of
would be an application level object. For example: If I create an object in
the global.asax page and then assign that object to an application level
variable, then that object is not threadsafe.

I hope this helps.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Peter Beck" <pb***@inscapesolutions.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
Hi -

I have a question for someone who has experience with both ASP .Net and
JSP/Servlets, relating specifically to thread safetly.

In JSP, we say that instance vaiables of a servlet are not thread safe.
Several threads could be running the same servlet at the same time, because a new thread of a servlet - as opposed to a new servlet itself - is created by requests for the servlet. Since there is only one instance of the
servlet object, there is only one set of instance variables, and each thread could set them to values that are in conflict with those in another thread. However, you can write thread-safe servlets by avoiding instance variables, or by judicious use of the synchronize(){ } construction.

It follows from this that instance variables in JSP pages are also not
thread safe, since the JSP is simply turned into a servlet. In theory, if
two people are running the same JSP page at the very close to the same time the second person to access the page could re-set the value of an instance
variable to something that causes improper results for the first person, who may be only part way through the execution of the page. I believe there is a directive you can use to "sychronize" the whole page, although many
authorities seem to discourage this.

Can anyone tell me if there is a similar issue with ASP .Net pages,
specifically variables in the "code-behind" page? Is there something
analagous to a "servlet" that is created by ASP .Net, of which only one copy exists that many users are executing in different threads? Is there the
possibility that users will "step on" each other's instance variables (as
there is only one instance of the object)?

Regards to all -

Peter Beck

Nov 18 '05 #2
Overall, the Page variables are a bad example, as the actual variable values
are stored on each client, in ViewState. If ViewState is off, then nothing
is saved on the client, so there is no issue. In reality, there is no issue
either way. Some do not like to work with ViewState.

From the way you have written this, I am not sure whether we are talking the
same thing when we say "thread safety," but I will run with the

If you write the following Class

public class SessionVars
{
public static string SessionID;
}

If you set this with user #1, and then user #2 comes in and you reset it,
both users are now effectively user #2. This is not a good thing. As such,
you need to set up instance variables (Shared = VB.NET; static = C#) on
application level properties, not on session level. I have personally seen
this happen in an application and it is not pretty. If you have instance
properties, they should be "global" in scope. I am sure someone will come up
with an exception, but learn the rule before you break it.

Another good reasoning for instances methods is helper functions. This is
more a performance issue, as loading the helper function once, in memory, is
more efficient that instancing a bunch of classes.

On the Page class (meaning your own page, which is derived from the FCL Page
class), I am fairly certain you could bump two sessions into each other with
instance methods. I cannot think of a good reason, overall, to use an
instance method, unless you have a setting for the page that should remain
static. As the weight of setting this value when creating the instance is
very low, I am not sure I can rubber stamp and instance method in a class.
If someone has a good example, I would love to see it.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Peter Beck" <pb***@inscapesolutions.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
Hi -

I have a question for someone who has experience with both ASP .Net and
JSP/Servlets, relating specifically to thread safetly.

In JSP, we say that instance vaiables of a servlet are not thread safe.
Several threads could be running the same servlet at the same time, because a new thread of a servlet - as opposed to a new servlet itself - is created by requests for the servlet. Since there is only one instance of the
servlet object, there is only one set of instance variables, and each thread could set them to values that are in conflict with those in another thread. However, you can write thread-safe servlets by avoiding instance variables, or by judicious use of the synchronize(){ } construction.

It follows from this that instance variables in JSP pages are also not
thread safe, since the JSP is simply turned into a servlet. In theory, if
two people are running the same JSP page at the very close to the same time the second person to access the page could re-set the value of an instance
variable to something that causes improper results for the first person, who may be only part way through the execution of the page. I believe there is a directive you can use to "sychronize" the whole page, although many
authorities seem to discourage this.

Can anyone tell me if there is a similar issue with ASP .Net pages,
specifically variables in the "code-behind" page? Is there something
analagous to a "servlet" that is created by ASP .Net, of which only one copy exists that many users are executing in different threads? Is there the
possibility that users will "step on" each other's instance variables (as
there is only one instance of the object)?

Regards to all -

Peter Beck

Nov 18 '05 #3
He is not using the wording thread safe the way you are thinking. The
question deals with whether or not you can muck up instance variables from
different sessions, which is, in fact, the case.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"S. Justin Gengo" <sj*****@aboutfortunate.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Peter,

I'm just beginning to learn JSP (the company I work for bought me a
Powerbook G4 to play with about a month ago and I'm just getting started),
so I may not be the best person to answer this, but I think I can.

Each ASP.Net page is threadsafe. All variables in the code-behind page are
threadsafe. The best equivalent to a servlet, of which only one copy exists that many users are executing in different threads, that I can think of
would be an application level object. For example: If I create an object in the global.asax page and then assign that object to an application level
variable, then that object is not threadsafe.

I hope this helps.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Peter Beck" <pb***@inscapesolutions.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
Hi -

I have a question for someone who has experience with both ASP .Net and
JSP/Servlets, relating specifically to thread safetly.

In JSP, we say that instance vaiables of a servlet are not thread safe.
Several threads could be running the same servlet at the same time,

because
a new thread of a servlet - as opposed to a new servlet itself - is

created
by requests for the servlet. Since there is only one instance of the
servlet object, there is only one set of instance variables, and each

thread
could set them to values that are in conflict with those in another

thread.
However, you can write thread-safe servlets by avoiding instance

variables,
or by judicious use of the synchronize(){ } construction.

It follows from this that instance variables in JSP pages are also not
thread safe, since the JSP is simply turned into a servlet. In theory, if two people are running the same JSP page at the very close to the same

time
the second person to access the page could re-set the value of an instance variable to something that causes improper results for the first person,

who
may be only part way through the execution of the page. I believe there

is
a directive you can use to "sychronize" the whole page, although many
authorities seem to discourage this.

Can anyone tell me if there is a similar issue with ASP .Net pages,
specifically variables in the "code-behind" page? Is there something
analagous to a "servlet" that is created by ASP .Net, of which only one

copy
exists that many users are executing in different threads? Is there the
possibility that users will "step on" each other's instance variables (as there is only one instance of the object)?

Regards to all -

Peter Beck


Nov 18 '05 #4
Ahhh, then I was right about not knowing enough JSP yet.

Thanks. (I'll keep studying.)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O$*************@TK2MSFTNGP11.phx.gbl...
He is not using the wording thread safe the way you are thinking. The
question deals with whether or not you can muck up instance variables from
different sessions, which is, in fact, the case.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"S. Justin Gengo" <sj*****@aboutfortunate.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Peter,

I'm just beginning to learn JSP (the company I work for bought me a
Powerbook G4 to play with about a month ago and I'm just getting started),
so I may not be the best person to answer this, but I think I can.

Each ASP.Net page is threadsafe. All variables in the code-behind page are threadsafe. The best equivalent to a servlet, of which only one copy exists
that many users are executing in different threads, that I can think of
would be an application level object. For example: If I create an object

in
the global.asax page and then assign that object to an application level
variable, then that object is not threadsafe.

I hope this helps.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Peter Beck" <pb***@inscapesolutions.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
Hi -

I have a question for someone who has experience with both ASP .Net and JSP/Servlets, relating specifically to thread safetly.

In JSP, we say that instance vaiables of a servlet are not thread safe. Several threads could be running the same servlet at the same time,

because
a new thread of a servlet - as opposed to a new servlet itself - is

created
by requests for the servlet. Since there is only one instance of the
servlet object, there is only one set of instance variables, and each

thread
could set them to values that are in conflict with those in another

thread.
However, you can write thread-safe servlets by avoiding instance

variables,
or by judicious use of the synchronize(){ } construction.

It follows from this that instance variables in JSP pages are also not
thread safe, since the JSP is simply turned into a servlet. In theory, if two people are running the same JSP page at the very close to the same

time
the second person to access the page could re-set the value of an instance variable to something that causes improper results for the first
person,
who
may be only part way through the execution of the page. I believe
there is
a directive you can use to "sychronize" the whole page, although many
authorities seem to discourage this.

Can anyone tell me if there is a similar issue with ASP .Net pages,
specifically variables in the "code-behind" page? Is there something
analagous to a "servlet" that is created by ASP .Net, of which only
one copy
exists that many users are executing in different threads? Is there

the possibility that users will "step on" each other's instance variables

(as there is only one instance of the object)?

Regards to all -

Peter Beck



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: N.K. | last post by:
Hi, I'm using Tomcat 7.2 and I'm trying to deploy an aplication that uses JSP and servlets. I'm actually trying to duplicate a production server but can't get it right. In the directory...
1
by: Sascha Pohlmann | last post by:
hello, I have the following problem: I would like to evacuate the logic from my jsp files in servlets. servlets provide database contents prepared in HTML tables, the jsp files are supposed...
3
by: Jose Munoz | last post by:
Hi all, I want to share some data for all my applications (servlets and jsps). For this i am using a JSP to set the variables with scope=application. When i get this data from some JSP all is o.k,...
0
by: Ruaan Kruger | last post by:
Hi there, I'm a junior java programmer that has just started working on j2ee. The company I worked for is currently using Struts for all new implementations etc. The problem is however that...
3
by: Bit Byte | last post by:
I have written a custom servlet engine (and "wrapper" servlets) for some legacy code (C/C++) that I have. The servlets contain the bulk of my 1st 2 layers in a 3 tier architecture - i.e. data...
2
by: prakashsurya | last post by:
gud evening i need a help about servlets its a bit urgent actually the problem i am facing is when i use doGet method i am getting the result in servlets but when i am going for doPost i am...
0
by: ank99 | last post by:
hello...i m trying to run servlets(using GET) from wml page..... using apache tomcat 5.5 server and WinWap for Windows(version 3.2.1.28.)....its working fine as far as just to display wml form...
1
by: ank99 | last post by:
hello...i m trying to run servlets(using GET) from wml page..... web server : apache tomcat 5.5 server WAP Browser: WinWap for Windows(version 3.2.1.28.).... its working fine as far as just to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.