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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Scope of a servlet" Watch "Scope of a servlet" New topic
Author

Scope of a servlet

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

Servlets have no scope. Perhaps you need to expand on what you are asking.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

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

Joined: Oct 13, 2007
Posts: 224
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

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

Joined: Sep 11, 2007
Posts: 501

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.


Millions saw the apple fall, but Newton asked why.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

That depends where you have declared it. As a class or instance member == shared. Within a method == not shared.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

It is exactly the same as any other Java program that is multi-threaded. The same precautions need to be taken.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope of a servlet