• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference betwen parameters and attributes

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear about the need to have attributes that we use sometimes with HttpServletRequest, ServletContext etc. Parameters are the string values that we recieve from the client browser. Hope I am right. Now when one servlet wants to forward some data to any other web resource , we set the information in the form of object by setting an atribute in the HttpServletRequest or ServletContext object etc ( whichever seems apt). Why cant we simply have the local varaibles or the instance varaibles to hold the information in the servlet class and send it to other web resource?
I have read that servlets run in multithreaded environment and so all the data is shared by the multiple threads running on the same instance. If we use instance variables and local variables, then they are shared by all the threads. But I dont think this happens. If I have a class:

Then the variables x and y are not shared by the threads. All the threads will have their own value for these varaibles.
The concept of attributes is not clear to me. I would like to know the need of attributes which we set in Request /Context/ any other object.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads aren't servlets.

There's only one instance of a servlet created for each mapping.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As David said, container creates only one object of servlet and for every request, this servlet object is accessed with different thread created by the container and service method is called . So if we make use of instance variable for communication, it will be shared with other request from different users. To avoid that we have to use of request or session object for communication between different resources.
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know container creates only one instance of servlet and for every request, separate thread is made running on the same servlet instance. Thats why I am little confused with what might have happened with the the local and instance varaibles of the servlet class. I referred HeadFirst book...In chapter 5 they have discussed about the attributes and parameters..... Book says that local variables inside the service method (or more exactly any of doXXX methods) are thread safe but the instance variables in the servlet class are not thread safe i.e they are accessible by the multiple threads. I have not understood this....

In the above code the local vaiables are not shared by the class but the instance varaible are shared. changes made on instance variable x by t1 is seen by another thread t2. but this does not happen with local varaible k. Both the threads have their own value of k. changes made by one are not seen by the other thread. have I made this program accurate. This happens with servlets also? If make the program:

In this case the instance variables are thread safe i.e they are not shared by multiple threads. I hope this might not happen in case of servlets as the threads are not executed on any instance. both are different instances. My concept of threading may be little bit weak. I am little confused about these two appraoches.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really not indent your code? It's too hard to see the structure of the code without indentation. If you want people to look at your code, take the time to format it to be readable.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

carox kaur wrote:Book says that local variables inside the service method (or more exactly any of doXXX methods) are thread safe but the instance variables in the servlet class are not thread safe i.e they are accessible by the multiple threads. I have not understood this....



Container creates only one instance of a servlet and multiple threads based on the number of service() method calls. The instance varible resides in a memory which is accessible to all the threads. i.e instance varibles are shared among different threads. For the local variables (any variable declared inside doXXX method), each call to doXX method is handled by a new thread. Which means each thread will have its own copy of local varible, seperate from the other thread handeling another call to doXXX method. Hence local varibles do not get shared among multiple threads and are thread safe.

In your first block of code think of object 't' as it is created by container. No more objects will be created ther after for the same servlet/class. Assume t1,t2,t3 are three different threads crated by container upon each new call to doXXX(). consider run() as doXX() where t1, t2, t3 will go after being initialized. In this case 'x' is an instance variable of a servlet and shared among t1, t2 and t3. While 'k' is local varible to run() (i.e doXX() as per assumed). 'k' will not be shared among different threads and is therefore threadsafe.
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did indent the code...but now I thinks its satisfactory....
Thanks jaydeep for the explanation...


Book says that local variables inside the service method (or more exactly any of doXXX methods) are thread safe but the instance variables in the servlet class are not thread safe i.e they are accessible by the multiple threads.



In your first block of code think of object 't' as it is created by container. No more objects will be created ther after for the same servlet/class


Thanks to confirm this.
So approach1 is followed in case of servlet and not approach2. As in approach2 two different objects are created and both the threads are not instantiated on single object. Hope I am right.
Hence in servlets (approach1), instance variables are shared by multiple threads (ie. multiple requests), keeping any temporary data in the instance variable will not be thread safe. And we need thread safety. When Thread1 saves the data in the instance variable so that other web resource (other servlet or jsp can use it), it could be that Thread1 goes from running to runnable state and Thread2 resumes execution, meantime thread2 can change the value of the varaible. This will lead to inconsistent state. When thread1 will resume execution, the value of the variable would have changed. And so this problem was solved by setting any data in the form of attribute in any object (Session, Request or context) scope, whichever seems apt. Am I right.
 
Jaydeep Vaishnav
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are going in a right direction

In your first block of code think of object 't' as it is created by container. No more objects will be created ther after for the same servlet/class



I wanted to make a small point that there is a case when more than one object of a same class will be created by container. For multiple mappings of a same sevlet in a deployment descriptor, container creates multiple objects. But this is different issue and doesn't happen most of the time.

And so this problem was solved by setting any data in the form of attribute in any object (Session, Request or context) scope, whichever seems apt.



Please make sure that attributes should not be used unless we have a data (user's information for instance) to be shared among different web components in a web application. If the data is specific to one servlet you may want to consider putting it under init parameters in deployment descriptor.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember:

A new instance of servlet MAY or MAY NOT be created by the Servlet Container (tomcat/weblogic) for every request. Therefore you must never have Servlet Class level attributes.

Whether a New instance of Servlet is created by the Servlet Container for every request - is a configurable property in Servlet Container.

Hope this helps
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic