Servlet cache issue

J

Joel S

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();
}
}
 
V

Venkatesh

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 :)
 
J

Joel S

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 -->
 
V

Venkatesh

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top