wood burning stoves 2.0*
The moose likes Servlets and the fly likes URL getting loaded twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL getting loaded twice" Watch "URL getting loaded twice" New topic
Author

URL getting loaded twice

Pedro Nuno Santos
Greenhorn

Joined: Aug 06, 2009
Posts: 3
Hello, I'm new to the whole servlet/jsp thing so bear with me (i.e. this maybe a noob question).

I'm currently writing a website that uses a servlet to catch all GETs and POSTs and forward the user to the appropriate jsp. These use EL & HTML to display the intended information. All my site links have the following format ?action=[actionName]¶m1=[param1]¶m2=[...]&...
After catching the request I identify the action, execute it and forward the user to another .jsp (it can be the same one though) with: request.getRequestDispatcher(address).forward(request, response) where the address is the path to the jsp file (currently all of them are stored in \WEB-INF\jsp\ folder

Now my problem is that when I click an action link, the servlet catches 2 requests, one after the other, with the exact same URL... my guess is the browser, after making the first request, when loading the forwarded jsp, loads it with the same URL it made the request with in the first place. thus making the servlet execute again the same action... Am I correct in thinking so?

How can I prevent this from happening? Am I doing something wrong here?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
There is another problem ... firing a request with your browser won't cause the servlet to be invoked twice. On top of that, your browser does not call the JSP separately, that happens on the server.


JDBCSupport - An easy to use, light-weight JDBC framework -
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Pedro Nuno Santos wrote:Am I correct in thinking so?

No.

Either you are double clicking the button, or you have some code on the page that is causing the double submit.

Do you have script on the page doing form validation or other activities at form submit?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pedro Nuno Santos
Greenhorn

Joined: Aug 06, 2009
Posts: 3
I've been trying to debug this and it just got weirder... sometimes URLs are getting called twice other times not, but the process is always the same:

1) doGet catches link and calls executeAction()
2) executeAction searches the class annotations and corresponding methods and when it finds an annotation equal to the action it calls the method
3) method does it's stuff and returns the address to display (jsp file)
4) excecuteAction returns that address to doGet
5) doGet forwards with RequestDispatcher

I'm printing what the query string at doGet start and what the jsp path is just before the fowarding with the RequestDispatcher. Every single time both the query string and address are correct, only sometimes the doGet gets called twice, here is an example of a double call:

[doGet] Start #####################
[doGet] Request Query: action=action1&id=27
[doGet] Forward Address: /WEB-INF/jsp/view1.jsp
[doGet] End #####################
[doGet] Start #####################
[doGet] Request Query: action=action1&id=27
[doGet] Forward Address: /WEB-INF/jsp/view1.jsp
[doGet] End #####################


Above "Start" is printed just after doGet and "End" just after the RequestDispatcher is called. This happens when I click the link or when I copy-paste the address to the browser URL bar. However it is not persistant, i.e. does not happen everytime...

Edit: @Bear Bibeault: No no javascript whatsoever, all content is pure HTML with some EL to access session varibles and do "foreach" cycles and "if" tests.
Pedro Nuno Santos
Greenhorn

Joined: Aug 06, 2009
Posts: 3
Well found something interesting today, I now have a way to replicate the double request (happens every time with firefox), however if I use IE8 and do the exact same sequence of actions the double request is never made. Now this may be some kink with my code the somehow doesn't expose it's ugly head with IE8.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URL getting loaded twice