aspose file tools*
The moose likes Servlets and the fly likes Servlet Only Fires Once 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 "Servlet Only Fires Once" Watch "Servlet Only Fires Once" New topic
Author

Servlet Only Fires Once

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
If I use jQuery.getJSON to call a servlet say with a button click to get some remote value, clicking the button will not fire the servlet again unless I change the parameter values sent to it. Why is this and can it be fixed?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

How have you determined that the servlet is only getting called once?

Does the servlet exhibit the same behavior when you invoke it directly from the browser?

It sounds as if this might be a browser caching issue.
[ November 27, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
To verify the only once calling I put a System.out.println("test") This is the only code in the servlet(for testing) in the doGet method. It only writes once if the parameters do not change.

If I start the app navigate to the calling page then manually call the servlet directly(type in address) the System.out.println("test") prints every time I press enter key.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You might want to add nocache headers to your response (see Servlets FAQ) and see if that makes a difference. The fact that the change in param value triggers a hit makes this sounds suspiciously like a caching issue.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
one way to do it is, in Jquery Ajax call, you can specify no cache

a hack is add a variable to the url with a constantly changed value like:
String url = requestUrl + "?now=" + new Date();
when you call jquery to make an ajax call, if the url is not changed, you will get the same result.
[ November 30, 2007: Message edited by: Hongli Li ]

Do you know why this cup is useful? Because it is empty.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet Only Fires Once