Servlet cache issue

Discussion in 'Java' started by Joel S, Apr 24, 2006.

  1. Joel  S

    Joel S Guest

    I wrote a servlet that appears to have a cache issue, everytime i hit
    reload it appends to the data in the browser already. I've tried
    everything i can think of to fix this issue but would appreacite any
    advice you may have.

    the code is below:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    /**
    * Servlet implementation class for Servlet: index
    *
    */
    public class index extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet {
    /* (non-Java-doc)
    * @see javax.servlet.http.HttpServlet#HttpServlet()
    */
    public index() {
    super();
    }

    /* (non-Java-doc)
    * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest
    request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "Sun, 15 Jan 1998 17:00:00 GMT");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Max-Age", 0);

    PrintWriter out = response.getWriter();
    StringBuffer buffer = new StringBuffer( 1024 );
    buffer.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
    Strict//EN\">\n");

    try
    {
    buffer.append(this.generatePage());
    out.print(buffer.toString());
    out.close();
    response.flushBuffer();
    }
    catch (Exception e)
    {
    e.printStackTrace(out);
    }
    }

    /* (non-Java-doc)
    * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest
    request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    private String generatePage() throws Exception {

    ServletContext sc = getServletContext();
    Jtpl tpl = new Jtpl(sc.getRealPath("/") + "Template.jtpl");
    tpl.assign("TITLE", "Rippr");
    tpl.parse("head");
    tpl.assign("COMMENT", "<!-- piece of junk -->");
    tpl.parse("main");
    return tpl.out();
    }
    }
     
    Joel S, Apr 24, 2006
    #1
    1. Advertising

  2. Joel  S

    Venkatesh Guest

    The problem might be due to the "Template.jtpl", may be u should post
    the content of that.

    This is most probably not a servlet issue. Caching problem, as far as
    I've observed is something like this: The changes made to your jsp's
    and html pages don't get refreshed and this is because tomcat maintains
    a cache of all of them in the "work" directory. If that is the problem,
    then u can just delete the contents of "work" directory (the directory
    is present in tomcat's installation path).

    -Venkatesh

    ps: I'm assuming that the appserver being used is tomcat :)
     
    Venkatesh, Apr 24, 2006
    #2
    1. Advertising

  3. Joel  S

    Joel S Guest

    yes it's tomcat, there are no jsp's

    the template is

    <!-- BEGIN: main -->

    <html>
    <head>
    <meta http-equiv="Pragma" content="no-cache">


    {COMMENT}
    <!-- BEGIN: head -->
    <title>{TITLE}</title>
    <!-- END: head -->

    </HEAD>
    <BODY>
    <div class="content">
    <div class="header_top"></div>
    <div class="header">
    <div class="sf_right">
    <div id="nav">
    <ul>

    <li>Example Link</li>

    </ul>
    </div>
    <div class="sf_search">
    <form method="post" action="?">
    <p><b>Search:</b> <input type="text" name="search" class="search"
    /> <input type="submit" value="Go" class="submit" /> &nbsp; Explore the
    pictures!</p>
    </form>
    </div>
    </div>
    <div class="sf_left">
    <h1><a href="#">Ripper</a></h1>
    </div>
    </div>
    <div class="header_bottom"></div>
    <div class="subheader">
    <p></p>
    </div>
    <div class="header_top"></div>
    <div class="left">
    <div class="left_side">
    <div class="box_top">
    <-- BEGIN: LeftColumnTitle -->
    Replace
    <-- END: LeftColumnTitle -->
    </div>
    <div class="box">
    <-- BEGIN: LeftColumn -->
    Replace
    <-- END: LeftColumn -->
    </div>
    <div class="box_bottom"></div>
    </div>
    <div class="right_side">
    <div class="article">

    Replace

    </div>
    </div>
    </div>
    <div class="right">
    <div class="box_top">

    Replace

    </div>
    <div class="box">

    Replace

    </div>
    <div class="box_bottom"></div>
    </div>
    <div class="header_bottom"></div>
    <div class="footer">
    Ripper - 2006
    </div>
    </div>
    </BODY>
    </HTML>
    <!-- END: main -->
     
    Joel S, Apr 24, 2006
    #3
  4. Joel  S

    Venkatesh Guest

    Hi Joel,

    So the problem u r facing is due to usage of jtpl ....

    I replaced your template.jtpl with this small snippet of template code:

    <!-- BEGIN: main -->
    <html>
    <head>
    <meta http-equiv="Pragma" content="no-cache">

    {COMMENT}
    <!-- BEGIN: head -->
    <title>{TITLE}</title>
    <!-- END: head -->

    </HEAD>
    <BODY>
    Welcome
    </BODY>
    </HTML>

    <!-- END: main -->

    and with every server hit, the word Welcome was getting replicated.

    So I had to look through the source code of JTPL jar (The version i'm
    having is Jtpl_1.1.jar and the source is present in
    net\sourceforge\jtpl\Jtpl.java) . This is the main culprit.

    The JTPL class has a static HashMap called "blocks"

    public class Jtpl
    {
    private static HashMap blocks = new HashMap();
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    private HashMap parsedBlocks = new HashMap();
    private HashMap subBlocks = new HashMap();
    private HashMap vars = new HashMap();

    and this particular HashMap is neither recreated nor refreshed for
    every server hit, hence contents get appended.

    Hence, u r getting the problem. Don't know why that member variable is
    made static. So, if things are fine, may be u should remove that static
    declaration and make blocks a non-static member, or otherwise u should
    contact the authors of this library for more details.

    Hope this helps

    -Venkatesh
     
    Venkatesh, Apr 24, 2006
    #4
  5. Joel  S

    Joel S Guest

    You are a life savor, i owe you a million zillion hot women.
     
    Joel S, Apr 24, 2006
    #5
  6. Joel  S

    Venkatesh Guest

    Thanks Boss :)

    -Venkatesh
     
    Venkatesh, Apr 25, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Nokes

    Cache::Cache Stale Segments

    Jeff Nokes, Sep 30, 2003, in forum: Perl
    Replies:
    0
    Views:
    582
    Jeff Nokes
    Sep 30, 2003
  2. DesignerX

    Page.Cache vs HttpContext.Current.Cache

    DesignerX, Jan 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    8,268
    vMike
    Jan 20, 2004
  3. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,300
    Karl Seguin [MVP]
    Mar 8, 2006
  4. circuit_breaker
    Replies:
    2
    Views:
    2,019
    Jack Jia
    Apr 4, 2004
  5. Sergey via DotNetMonster.com

    ASP.NET Cache vs Window System Cache

    Sergey via DotNetMonster.com, Nov 15, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    Sergey via DotNetMonster.com
    Nov 15, 2006
Loading...

Share This Page