Hi all, This has nothing to do w/servlets or browsers but simply with Tomcat. I'm wondering if it might be a bug in the way Tomcat performs caching or handles HTTP headers. I'm using Tomcat v4.1.9 with JDK v1.4.1 on Win 2k.
This is a very simple situation. I have a Java client program (see code below) that performs the following steps: 1. Writes a file to a web context directory. 2. Uses a URLConnection to request the file in #1. 3. Sleeps for a few seconds. 4. Overwrites the file in #1 with a new contents. 5. Uses a URLConnection to request the file in #1. In step 5, I find that Tomcat is returning the contents of the file that was written in step 1 -- BUT ONLY IF THE SLEEP TIME IS LESS THAN 5 SECONDS. If I sleep for 5 or more seconds, then the contents returned in step 5 is that which was written in step 4, which is what I would expect. The problem then is that Tomcat returns stale content if the sleep time is less than 5 seconds. Why?!! The code below includes the full client that connects to Tomcat. Notice the "Cache-Control" header is set to "max-age=0" which should eliminate any caching on Tomcat. Just for grins, "Pragma:no-cache" is used too. Since I'm connecting directly to Tomcat (i.e., localhost) there is no proxy involved. I've checked the HTTP headers using a sniffer and they look fine. Here is the client code....If you run this make sure you are writing the file to the same directory from which TC will serve it (obviously). Thanks, Tony ==================================================== import java.io.*; import java.net.*; public class TestURL { public static void writeToFile (String filename, String text) throws IOException { FileWriter fw = new FileWriter(filename); PrintWriter pw = new PrintWriter(fw); pw.print(text); pw.close(); System.out.println ("writeToFile() wrote: " + filename); System.out.println ("writeToFile() text: " + "'" + text + "'"); System.out.println ("---"); } // writeToFile() public static String readURL (String url) throws IOException, MalformedURLException { StringBuffer sb = new StringBuffer(""); URL srcurl = new URL(url); URLConnection urlc = srcurl.openConnection(); urlc.setDoInput(true); urlc.setUseCaches(false); urlc.setDefaultUseCaches(false); urlc.setRequestProperty("Cache-Control", "max-age=0"); urlc.setRequestProperty("Cache-Control", "no-cache"); urlc.setRequestProperty("Cache-Control", "no-store"); urlc.setRequestProperty("Pragma", "no-cache"); InputStream is = urlc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); char[] buffer = new char[100]; int charCount = 0; while((charCount = isr.read(buffer, 0, buffer.length)) != -1) sb.append(buffer, 0, charCount); isr.close(); System.out.println ("readURL() read: " + url); System.out.println ("readURL() text: " + "'" + sb.toString() + "'"); System.out.println ("---"); return sb.toString(); } // readURL() public static void main (String[] args) throws Exception { int sleepTime = 5; if (args.length >= 1) sleepTime = Integer.parseInt (args[0]); System.out.println ("----- TOMCAT TEST -----"); String basedir = "C:\\Development\\Projects\\Tester\\Servlets\\web\\junk\\"; String baseurl = "http://localhost/t/junk/"; String testfile = "testurl.txt"; String first = "The first text"; String second = "The second text"; String filepath = basedir + testfile; String fileurl = baseurl + testfile; writeToFile(filepath, first); String s1 = readURL(fileurl); System.out.println ("Sleeping " + sleepTime + " seconds..."); Thread.sleep(sleepTime * 1000); writeToFile(filepath, second); String s2 = readURL(fileurl); if(s2.equals(second)) System.out.println("The second file was properly served."); else System.out.println("The second file was *NOT* properly served."); } // main() } // TestURL() -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>