aspose file tools*
The moose likes Servlets and the fly likes storing variable so it can be used by 2 servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "storing variable so it can be used by 2 servlets" Watch "storing variable so it can be used by 2 servlets" New topic
Author

storing variable so it can be used by 2 servlets

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Nov 08, 2003
Posts: 3479

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

Joined: May 25, 2008
Posts: 116
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

Joined: May 25, 2008
Posts: 116
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

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jan 10, 2002
Posts: 61662
    
  67

Please look up "premature optimization"


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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 ?
 
Don't get me started about those stupid light bulbs.
 
subject: storing variable so it can be used by 2 servlets