Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using IFRAME in JSF to display a PDF file

843844Sep 10 2007 — edited Sep 10 2007
2 all,

How do i display a PDF file inside a JSF page in a IFRAME tag?
I store the PDF file location (like d:\images\pdf1.pdf) in my database. I cant give this location directly into the src attribute cos then the file will be rendered to only users who have access to that folder (the application is a internet application). IFRAME would be ideal as the display is very very neat (esp for pdf files).

To get this working i tried the examples given in balusc blogs (on image servlet) but the problem is that my servlet is not getting called. Find below the web.xml and imageDisplay.jsp pages that i tried
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>WorkFlowTool</display-name>
 <context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>server</param-value>
 </context-param>
 <filter>
  <filter-name>MyFacesExtensionsFilter</filter-name>
  <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
  <init-param>
   <param-name>maxFileSize</param-name>
   <param-value>20m</param-value>
  </init-param>
 </filter>
 <filter>
  <display-name>SecurityCheckFilter</display-name>
  <filter-name>SecurityCheckFilter</filter-name>
  <filter-class>filters.SecurityCheckFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>MyFacesExtensionsFilter</filter-name>
  <url-pattern>/faces/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>MyFacesExtensionsFilter</filter-name>
  <url-pattern>*.faces</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>MyFacesExtensionsFilter</filter-name>
  <url-pattern>*.jsf</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>SecurityCheckFilter</filter-name>
  <url-pattern>/faces/*</url-pattern>
 </filter-mapping>
 <listener>
  <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet>
  <servlet-name>Image Servlet</servlet-name>
  <servlet-class>servlets.ImageServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Image Servlet</servlet-name>
  <url-pattern>/imageServlet/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>60</session-timeout>
 </session-config>
 <welcome-file-list>
  <welcome-file>/jsp/index.jsp</welcome-file>
 </welcome-file-list>
 <error-page>
  <error-code>500</error-code>
  <location>/jsp/error.jsp</location>
 </error-page>
 <resource-ref>
  <res-ref-name>jdbc/JDDS</res-ref-name>
  <res-type>java.lang.Object</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
</web-app>
imageDisplay.jsp
<HTML>
<HEAD>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<f:loadBundle basename="messages" var="msg" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK rel="stylesheet" type="text/css" href="../theme/Styles.css"
	title="Style">

</HEAD>
<body topmargin="0" leftmargin="0">
<f:view>
	<h:form id="CaseLookUp">
		<h:dataTable value="#{pc_ImageDisplay.dataModel}" var="var">
			<h:column id="one">
				<f:facet name="header">
					<h:outputText value="H1" id="HOne"/>
				</f:facet>
				<h:outputText value="#{var.caseID}" id="IDONE"/>
			</h:column>
			<h:column id="two">
				<f:facet name="header">
					<h:outputText value="H2" id="HTwo"/>
				</f:facet>
				<h:graphicImage value="imageServlet?file=#{var.PODocPath}" id="image"/>
			</h:column>
		</h:dataTable>
		
	</h:form>
</f:view>
</body>
</HTML>
The image servlet is what i got from balusc's site (http://balusc.blogspot.com/2007/04/imageservlet.html).

I dont know why my servlet is not getting called. Can someone help me with this pls?

okay let me post the modified code for my image servlet here
public class ImageServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {

        // Define base path somehow. You can define it as init-param of the servlet.
//        String imageFilePath = "/images";
        // In a Windows environment with the Applicationserver running on the
        // c: volume, the above path is exactly the same as "c:\images".
        // In UNIX, it is just straightforward "/images".
        // If you have stored images in the WebContent of a WAR, for example in the
        // "/WEB-INF/images" folder, then you can retrieve the absolute path by:
        // String imageFilePath = getServletContext().getRealPath("/WEB-INF/images");
        
        // Get file name from request.
        String imageFileName = request.getParameter("file");
        System.out.println("Inside the image servlet ---->>>> " + imageFileName);

        // Check if file name is supplied to the request.
//        if (imageFileName != null) {
//            // Strip "../" and "..\" (avoid directory sniffing by hackers!).
//            imageFileName = imageFileName.replaceAll("\\.+(\\\\|/)", "");
//        } else {
//            // Do your thing if the file name is not supplied to the request.
//            // Throw an exception, or show default/warning image, or just ignore it.
//            return;
//        }

        // Prepare file object.
        File imageFile = new File(imageFileName);

        // Check if file actually exists in filesystem.
        if (!imageFile.exists()) {
            // Do your thing if the file appears to be non-existing.
            // Throw an exception, or show default/warning image, or just ignore it.
            return;
        }

        // Get content type by filename.
        String contentType = URLConnection.guessContentTypeFromName(imageFileName);

        // Check if file is actually an image (avoid download of other files by hackers!).
        // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
        if (contentType == null || !contentType.startsWith("image")) {
            // Do your thing if the file appears not being a real image.
            // Throw an exception, or show default/warning image, or just ignore it.
            return;
        }

        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open image file.
            input = new BufferedInputStream(new FileInputStream(imageFile));
            int contentLength = input.available();

            // Init servlet response.
            response.reset();
            response.setContentLength(contentLength);
            response.setContentType(contentType);
            response.setHeader(
                "Content-disposition", "inline; filename=\"" + imageFileName + "\"");
            output = new BufferedOutputStream(response.getOutputStream());

            // Write file contents to response.
            while (contentLength-- > 0) {
                output.write(input.read());
            }

            // Finalize task.
            output.flush();
        } catch (IOException e) {
            // Something went wrong?
            e.printStackTrace();
        } finally {
            // Gently close streams.
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    // This is a serious error. Do more than just printing a trace.
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    // This is a serious error. Do more than just printing a trace.
                }
            }
        }
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 8 2007
Added on Sep 10 2007
6 comments
1,954 views