• 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

Thread safe Please help!!!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which model is thread safe:
import javax.servlet.http.*;
Part 1:
public class SetUpAccount extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
Account account = new Account();
req.getSession().setAttribute("Account", account);
......
req.getSession().getAttribute("Account");
}
}
PART 2
public class SetUpAccount extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
Account account = new Account();
req.setAttribute("Account", account);
......
req.getAttribute("Account");
}
}
I think its part two.
Pleas help
Thanks
Deee
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right, while request objects are threadsafe, session objects are not .
you can read this:
https://coderanch.com/t/164301/java-Web-Component-SCWCD/certification/Objective-Thread-Safety-Sesion-Object
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are terribly wrong!
In fact, request and response objects are not thread-safe.
You can see Servlet 2.4 specification SRV.2.3.3.3 Thread Safety.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
part II is safe, because the object (attribute) is bound to the request. Each request will create a separate request object.
Actually part I is pretty safe too that the object (attribute) is bound to a session, and in practice there's only one user per session so a user can only make one request at a time, right? Forget about hacking/bot/etc
Anyway, part II is safer
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A user can make more than one request at a time if:
- an application page uses <frame>s, or
- if it includes non-static images, and if the application requires access to the session object to serve those images.
 
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
Ron is correct. Additionally, at least with IE on Windows, it is possible for multiple browser windows to share cookies and therefore share sessions.
hth,
bear
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right -- in any browser, a user can "Open in New Window" multiple links, potentially causing multiple requests to run simultaneously in the same session.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic