<snip>
On Sat, 29 Jan 2005 16:28:42 +0000, Tim Christopher
<[EMAIL PROTECTED]> wrote:
> I've read that if you store your JSP files under the WEB-INF folder it
> blocks access to html resources (CSS etc)..  What's the work around to
> ensure the JSP has access to all the resources it still needs?
> 
> Tim
</snip>

The problem with access to HTML resources is that you are dependent on
the browser read of a path, relative or absolute, but have to locate
the resource serverside.  The way I do this is to avoid any use of the
browser at all for this purpose and use a Struts Action class to
deliver all assets/resources including CSS, JS, GIFs, etc. to the HTML
page.  Here is the essentials of  the class I use to do that, with
apologies to people that don't like anything more than a code snippet
on this list ;-) :

public final class ResourceAction
    extends Action {

  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws IOException,
             ServletException {
    String file = request.getParameter("file");
    String ext  = file.substring(file.lastIndexOf('.') + 1);
    String type = null;
    String path = null;

    if ("gif".equals(ext)) {
      type = "image/gif";
      path = path("gif");
    } else if ("jpg".equals(ext)) {
      type = "image/jpeg";
      path = path("jpeg");
    } else if ("css".equals(ext)) {
      type = "text/css";
      path = path("css");
    } else if ("flash".equals(ext)) {
      type = "application/x-shockwave-flash";
      path = path("flash");
    } else if ("text".equals(ext)) {
      type = "text/plain";
      path = path("text");
    } else if ("js".equals(ext)) {
      type = "text/javascript";
      path = path("js");
    } else if ("png".equals(ext)) {
      type = "image/png";
      path = path("png");
    } else if ("html".equals(ext)) {
      type = "text/html";
      path = path("html");
    } else if ("applet".equals(ext)) {
      type = "application/x-java-applet";
      path = "classes" + File.separator + "com" + File.separator +
"crackwillow" + File.separator + "applet";
    }

    String name = Classpath.WEB_INF + path + file;

    response.setContentType(type);
    response.setHeader("Cache-Control", "");
    response.setHeader("Pragma", "");
    response.setHeader("Expires", "");
    response.addHeader("Content-Disposition","filename=" + name);

    try {
      FileInputStream     fis   = new FileInputStream(name);
      BufferedInputStream bis   = new BufferedInputStream(fis);
      byte[]              bytes = new byte[bis.available()];
      OutputStream        os    = response.getOutputStream();
      bis.read(bytes);
      os.write(bytes);
      os.flush();
      os.close();
    } catch (IOException ioe) {
      StdOut.log(SiteConstant.ERROR_LOG,"ResourceAction: problem file
is: " + name + "\n" + StackTrace.trace(ioe) + "\n" +
ioe.getMessage());
    }

    return null;
  }

  private String path(String fileType) {
    return "resource" + File.separator + "content_type" +
File.separator + fileType + File.separator;
  }
}

Jack

-- 
"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be
crows.  We are poor . . . but we are free."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to