• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread safe Please help!!!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic