1

I have a JSP page that shows a list of new entries inserted on the system.

My myApplication.jsp is structured like this:

A list of entries in the system
A form with textboxes that submits new entries.

When my JSP submits, it calls my servlet class that does:

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String author = checkNull(req.getParameter("author"));
    String service = checkNull(req.getParameter("service"));
    Dao.INSTANCE.add(author, service);
    resp.sendRedirect("/myApplication.jsp");
}

My Dao.Add looks like this:

public void add(String author,String service) {
    synchronized (this) {
        EntityManager em = EMFService.get().createEntityManager();
        Shortly shortly = new Shortly(author, service);
        em.persist(shortly);
        em.close();
    }
}

The problem that I'm having is that when I get redirected back into myApplication.jsp, the list does not show the new entry I added. When I refresh the page, then it shows.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
andydev
  • 992
  • 9
  • 13
  • sounds like a caching problem. – botbot May 01 '12 at 02:14
  • I added: And still the same issue... Sometimes I need to refresh twice for the new line to appear. – andydev May 01 '12 at 03:08
  • still sounds like a caching issue, but perhaps it isn't. if it is a caching problem then perhaps you could manually clear your cache before loading the page. or use a browser add on to clear the cache. – botbot May 01 '12 at 03:16
  • Check this question: http://stackoverflow.com/questions/2705008/app-engine-no-cache-jsp – Moritz Petersen May 01 '12 at 11:34
  • Thanks Moritz, I followed that thread and implemented a Filter, but I still have the problem: http://i50.tinypic.com/244obv5.png Might it be related that I'm running it on my localhost? I will try to deploy it later to Google Apps and see if the problem still exists – andydev May 02 '12 at 00:47

2 Answers2

1

If you are using IE (or even some other browsers), try putting a random number (like a timestamp ) as a parameter in your redirect code piece:

 resp.sendRedirect("/myApplication.jsp?t="+timestamp);

IE is notorious in such scenarios and due to heavy caching, things don't always work the way expect. This timestamp will indicate the browser not to show a cached page and will always (hopefully) fetch the page from the server afresh.

trishulpani
  • 744
  • 8
  • 20
  • Tried your suggestion on IE and Chrome and the same problem happens. I have a feeling is related to JSP persistence on Google App Engine – andydev May 01 '12 at 05:16
0

I have found that the problem is around how Google App Engine's High Replication Datastore (HDR) is designed. HDR can only guarantee Eventual consistency results.

I have found more info here

The way I proved that's the reason why I'm seeing differences, is by counting the records on the persistence, straight after adding the record.

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String author = checkNull(req.getParameter("author"));
    String service = checkNull(req.getParameter("service"));
    Dao.INSTANCE.add(author, service);
    List shortlys = new ArrayList();
    shortlys = Dao.INSTANCE.getShortlys("default");
    System.out.println("Shortlys count is: " + shortlys.size());
    resp.sendRedirect("/myApplication.jsp");
}

Several times, the output didn't increased the count, and sometimes it increased by two (the old record, and the new record added...)

andydev
  • 992
  • 9
  • 13