aspose file tools*
The moose likes Servlets and the fly likes Can a servlet forward 2 web pages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can a servlet forward 2 web pages?" Watch "Can a servlet forward 2 web pages?" New topic
Author

Can a servlet forward 2 web pages?

James Lye
Greenhorn

Joined: Nov 12, 2004
Posts: 6
Hi,
In order to prevent double submission on a form, I intend to display a temporary web page after the first submit, informing the surfer to wait while his transaction is in progress. The program will continue to process the transaction and display a 2nd web page when the transaction is successful. Does anyone knows how this can be done using servlets? I tried the following in doPost:

RequestDespatcher.include(req, res) //use include to display the temp page
//process the trxn
RequestDespatcher.forward(req, res) //display the successful trxn web page
but encountered "Cannot forward a response that is already committed"

I'm new to Java so I would really appreciate if someone could help me out.
Thanks!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
no you can't.
The forward acts as a return statement so you can't follow it with code.


42
James Lye
Greenhorn

Joined: Nov 12, 2004
Posts: 6
Ok. In that case how can I implement the following using servlets?
1. Surfer submits form.
2. Server displays a temp web page.
3. Server processes transaction.
4. Server returns a transaction successful web page.

Thanks for your help.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
not readily.
You start the transaction in a separate thread and forward to the "please wait" page. This page every few seconds does a request to the server through Javascript. If the transaction isn't yet complete the servlet it polls sends back no data and the page remains. If it does send back data that will be the URL of the next page to go to (probably a page telling what was the result) and you can through Javascript go there.
James Lye
Greenhorn

Joined: Nov 12, 2004
Posts: 6
All right. I'll try that out.
Thanks for your help.

Actually I'm looking for ways to handle double submissions on the server side. Anyone else with other ideas. Do let me know.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

To prevent double transmissions, you can use the "token" concept. On the first request, you put something in the session. If it is there on the second request, you don't start processing again.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can also use Javascript to disable the submit button so the user can only click it once.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a servlet forward 2 web pages?