• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which JSP scope are thread safe ?  RSS feed

 
lavnish lalchandani
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Which JSP scopes are thread safe.
  • session
  • page
  • application
  • request


  • Session : I can use two tabs in same browser to send two request , hence two different threads , may access an object in same session scope. hence i think it is not thread safe

    page : not sure

    application : I think it is not thread safe

    request : I think it is thread safe

    Please correct me where i am wrong
     
    Bauke Scholtz
    Ranch Hand
    Posts: 2458
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thread safety doesn't depend on scope. It depends on your code.
     
    lavnish lalchandani
    Ranch Hand
    Posts: 79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi
    Thanks for quick response...
    Can you please elaborate

    My understanding is that , if i put a bean in request scope , then there wont be any threading issue
    as no other request thread can access the same bean instance

    Kindly let me know an example where two threads can access the same bean in request scope
     
    Bauke Scholtz
    Ranch Hand
    Posts: 2458
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just programmatically fire another thread in the same request.
     
    lavnish lalchandani
    Ranch Hand
    Posts: 79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If i get you correct , you mean inside service method i start a new thread i.e. call my_thread.start( )
    right ?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, but without the complication of user-fired threads, in general, request context will be thread safe within a servlet and a JSP, and page context will be thread-safe in a JSP page.

    Since application and session contexts are in scope for multiple container-generated threads, care must be taken with them.
     
    lavnish lalchandani
    Ranch Hand
    Posts: 79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for your answer
    and especially for bringing up special case of starting thread inside service method
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!