Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Servlet cache issue

Thread view: 
Joel  S - 24 Apr 2006 03:19 GMT
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();
    }
}
Venkatesh - 24 Apr 2006 06:14 GMT
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 :-)
Joel  S - 24 Apr 2006 07:41 GMT
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 -->
Venkatesh - 24 Apr 2006 11:04 GMT
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
Joel  S - 24 Apr 2006 16:17 GMT
You are a life savor, i owe you a million zillion hot women.
Venkatesh - 25 Apr 2006 10:04 GMT
Thanks Boss :-)

-Venkatesh


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.