• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a servlet forward 2 web pages?

 
James Lye
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no you can't.
The forward acts as a return statement so you can't follow it with code.
 
James Lye
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use Javascript to disable the submit button so the user can only click it once.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic