Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing variable so it can be used by 2 servlets

 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I have a jsp file which posts to a servlet. The servlet is meant to go to a database, run a query and display one column from last row only i.e the row with most recent updated date. All this is pretty simple so far but now there is a need to display all the rows coming back from the database if the user click on a "Display details" button.

so i have created a "form" within the servlet which holds the "Display Details" button and am making that post to yet another servlet which will display all the rows. i pass the details to the "display details" servlet by storing details in session. My question is since the session and its stored variables live till the session is ended, how do i expire the detailed stored in the session right after its been displayed ?? at the moment i set the value to empty string, is there a better way of doing this ??

JSP code :




Servlet1 code :



Servlet 2 Code :





thanks in advance
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about letting the first servlet issue a query to the database that returns the single row and have the second servlet query the database that returns all of the rows? And don't use the session object at all.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for interfering but you are generating HTML in your servlets?
Use servlets for the dirty work and JSP for presentation.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Todd wrote:Sorry for interfering but you are generating HTML in your servlets?
Use servlets for the dirty work and JSP for presentation.


thanks for the reply. How do i send results from servlet back to jsp ?? by setting it in the request object and doing a sendRedirect("/name.jsp") ??

Also how do i read the results from jsp ??

thanks again.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote:How about letting the first servlet issue a query to the database that returns the single row and have the second servlet query the database that returns all of the rows? And don't use the session object at all.


thanks for the reply. I was trying to avoid going to the db twice.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikil shar wrote:
John Todd wrote:Sorry for interfering but you are generating HTML in your servlets?
Use servlets for the dirty work and JSP for presentation.


thanks for the reply. How do i send results from servlet back to jsp ?? by setting it in the request object and doing a sendRedirect("/name.jsp") ??

Also how do i read the results from jsp ??

thanks again.

Yes set your data in the most suitable scope and then forward to JSP.
JSP EL is a preferred way to access scoped data.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to avoid going to the db twice.

Well, consider these (in no particular order of importance)
1. A user waiting for one web page to display and then a second page to display takes many seconds. Two calls to a database is a fraction of this.
2. A user may not make the a second request so you are now delaying responses for every user to accommodate the subset of users that request the second page.
3. It really simplifies the design so it's much easier to support
4. Your users will never see the difference that you make two database requests vs. one.
5. Absolute efficiency is not the only criteria in an application design. See the big picture.
6. Is reading all the data from the database in one call and storing it in the application server really more efficient then making two calls to the database?
7. By doing your own caching, you are bypassing any caching that the database already does.
8. Don't complicate design by prematurely designing for efficiency.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look up "premature optimization"
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote:
I was trying to avoid going to the db twice.

Well, consider these (in no particular order of importance)
1. A user waiting for one web page to display and then a second page to display takes many seconds. Two calls to a database is a fraction of this.
2. A user may not make the a second request so you are now delaying responses for every user to accommodate the subset of users that request the second page.
3. It really simplifies the design so it's much easier to support
4. Your users will never see the difference that you make two database requests vs. one.
5. Absolute efficiency is not the only criteria in an application design. See the big picture.
6. Is reading all the data from the database in one call and storing it in the application server really more efficient then making two calls to the database?
7. By doing your own caching, you are bypassing any caching that the database already does.
8. Don't complicate design by prematurely designing for efficiency.


ok have another issue, since i am using a form within my first servlet, the request to the second servlet is a new request. how do i pass variables from the first request onto the second request to the second servlet ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic