• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scope of a servlet

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is servlet independent of each request sent to it?

Is there anyway I can set scope of a Servlet (Session, Application, Request, etc)?
[ July 22, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets have no scope. Perhaps you need to expand on what you are asking.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm not fully understand yet how servlet works.

Let say there are 1000 users accessing the same servlet, will there be any concurrency issue with the variables in the servlet?

In JSP, I can specify whether this JSP is application scope, request scope, or session scope. Why not in servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Susan Smith:
Let say there are 1000 users accessing the same servlet, will there be any concurrency issue with the variables in the servlet?
That depends upon whether the servlet is written to be thread-safe or not. Since a single instance is shared amongst all threads, read-write class members, and instance members are a no-no.

In JSP, I can specify whether this JSP is application scope, request scope, or session scope.
Umm, no. JSPs don't have any scope either. What makes you think that they have a scope? What could that scope possibly mean?
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I remember incorrectly . It's the bean that has scope. Something like this below:


Servlet can use Bean too I assume?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet is just a class like any other Java class, so there's nothing to prevent it from instantiating anything it likes -- including classes that conform to the JavaBean standard.
 
Mandar Khire
Ranch Hand
Posts: 569
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confuse between servlet, jsp & usebean.Me too but i try to give references of links which helps me to clear my basic queries about servlet & jsp.

for usebean.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thank you guys.

>>Since a single instance is shared amongst all threads, read-write class members,
>>and instance members are a no-no.
So if a servlet uses Bean, for example in my servlet I will have:

Then this bean will be shared by every threads?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends where you have declared it. As a class or instance member == shared. Within a method == not shared.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. It's different from regular java program, isn't it?
Each object has its own instance members.

Do you have a good online resources/ books that you can recommend to learn about Servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is exactly the same as any other Java program that is multi-threaded. The same precautions need to be taken.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic