You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Yun Sang Jung <na...@channeli.net> on 2000/08/02 02:22:10 UTC

Could you advice me to correct my custom tag code?

Hi.

I implemented my first nocache tag. There is no error in compile and run time.
But my code seems not to work properly. If there is taglib code guru, please tell me
how to correct my code..

Thanks.

[my Code]
import javax.servlet.http.*;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import java.io.*;

public class NoCacheTag extends TagSupport {
 public int doStartTag() throws JspException {
  return SKIP_BODY;
 }
 
 public int doEndTag() throws JspException {
  HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
  HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();

  if(req.getProtocol().compareTo ("HTTP/1.0") == 0) {
   res.setHeader ("Pragma", "no-cache");
  } else if (req.getProtocol().compareTo ("HTTP/1.1") == 0) {
   res.setHeader ("Cache-Control", "no-cache");
  }
  res.setDateHeader ("Expires", 0);
  return EVAL_PAGE;
 }
}

Re: Could you advice me to correct my custom tag code?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
The code itself looks OK, but I have a couple comments and questions:

* In what way does this "not work properly"?  Is the problem that the
  headers are not getting generated, or that they are getting generated
  but the page is still cached?

* To answer the question above, you might need to use some sort of
  debugging tool that lets you look at the actual headers received
  (by the client) as part of the response.

* If this custom tag occurs in your page at a point such that the
  output response has been committed already (in other words,
  the first buffer-full of data has been sent), any later attempt to
  set HTTP headers will be ignored.  To avoid this, be sure that
  you specify a buffer size in the <%@ page %> directive big
  enough to ensure that this does not happen.

* I'm not sure you really need to go to the effort of checking which
  protocol the request came in with.  I tend to write all three of the
  following headers in my code when I want to defeat caching:
        res.addHeader("Pragma", "No-cache");
        res.addHeader("Cache-Control", "no-cache");
        res.addDateHeader("Expires", 1);

* Note that the expires header uses one instead of zero.  I have
  heard that some browsers have a problem respecting the value
  created by a zero date header.

Craig McClanahan
========================================
Yun Sang Jung wrote:
-------------------------------

Hi.

I implemented my first nocache tag. There is no error in compile and run
time.
But my code seems not to work properly. If there is taglib code guru,
please tell me
how to correct my code..

Thanks.

[my Code]
import javax.servlet.http.*;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import java.io.*;

public class NoCacheTag extends TagSupport {
 public int doStartTag() throws JspException {
  return SKIP_BODY;
 }

 public int doEndTag() throws JspException {
  HttpServletRequest req = (HttpServletRequest)
pageContext.getRequest();
  HttpServletResponse res = (HttpServletResponse)
pageContext.getResponse();

  if(req.getProtocol().compareTo ("HTTP/1.0") == 0) {
   res.setHeader ("Pragma", "no-cache");
  } else if (req.getProtocol().compareTo ("HTTP/1.1") == 0) {
   res.setHeader ("Cache-Control", "no-cache");
  }
  res.setDateHeader ("Expires", 0);
  return EVAL_PAGE;
 }
}