• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to create a new instance for each new request to a servlet.  RSS feed

 
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a new instance of a Java model class for each new request coming to a servlet.
How to do that without doing that in doGet() or doPost().
 
Rancher
Posts: 42974
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you wish to avoid those methods? Those are the lifecycle methods for handling requests, after all.
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually after getting the first request from the user , I want to create a new instance .
Now why I don`t want to to do that in doGet() or doPost() is because the same servlet is going to process the subsequent request from the same user.

If I put it into doGet() or doPost() it will be creating the instance for each request.



 
Ulf Dittmer
Rancher
Posts: 42974
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this object would be user-specific, but not request-specific? Sounds like you should put it in the user session after initially creating it, and then reusing it in subsequent requests.
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this
 
Ulf Dittmer
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, although I'd write it a bit differently (which IMO makes it a bit clearer to understand):
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!