Results 1 to 4 of 4
  1. #1
    damodar is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default How to invoke out side web root files inside application?

    Hi, I wanted to invoke out side webroot files inside application, is there a way to invoke out side files and include in the jsp page? if so please help me how can we achieve this?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    do you mean to have .class files located outside of the webroot, or content files ?

    .class files are tricky, because you would need to have the folder (or the jar file) added to the class path, such as by customizing or configuring the class loader of the container, which is difficult to do outside of their standard shared libraries folders.

    if you meant to have content like text files fetched from a location outside of the webroot displayed inside a JSP page, then a component that uses standard File IO to read the contents from these files, such as with FileInputStreams and then have it written to the servlet output stream should likely be possible. In this case I would suggest creating some kind of tag library so the logic to do the file reading would be encapsulated nicely in a custom JSP tag. Note as well, you would want to make precautions to sandbox the requests for files such that the JSP tag is only capable of requesting files within some sandboxed path, so as to prevent accidental or malicious JSP authors from reading the contents of privileged system files, such as /etc/passwd

  3. #3
    damodar is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default I wanted to load the header/footer jsp pages from outside webroot files?

    thanks for your immediate responce, I want to load the header.jsp and footer.jsp files from outside webroot and load them into my actual jsp page. It will be great help if you can give more details or provide the code.

    Thanks once again.

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    oh, I was thinking for static content files, like text, pdf, images outside the container. For JSP files outside of the container, I'm not sure that is readily possible to do, as JSP pages are compiled into .class files by the JSP compiler Servlet, which I think needs to work with files within a web application context. I think we would need to get into the sources for the JSP compiler, perhaps have a look at Jasper2 from the Tomcat project, and customize it to have its search for JSP files path include the folders outside of the webapplication root. Where a custom JSP servlet is enabled by defining your JSP compiler servlet in your web.xml to override the one in the container default web.xml.

    For static content reading, I don't have any specific code, but something like
    Java Code:
    String path = "path to the file"
    FileInputStream fin = new FileInputStream(path);
    OutputStream out = response.getOutputStream();
    int bytesRead = 0;
    byte[] buffer = new byte[1024];
        while (  (bytesRead = in.read(buffer, 0, bufferSize)) != -1 ) {
          out.write(buffer, 0, bytesRead);
        }
    should work.

Similar Threads

  1. Replies: 1
    Last Post: 05-17-2010, 09:03 PM
  2. Replies: 1
    Last Post: 09-17-2009, 08:19 AM
  3. Replies: 9
    Last Post: 09-01-2009, 07:17 PM
  4. Modify context root in application.xml
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 11:12 AM
  5. Replies: 6
    Last Post: 02-06-2009, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •