Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Only Fires Once

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic