Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Developing Web applications using RAD tools, IBM extended JSF components, and WebSphere Studio V.5.1.2

Eugene Konstantinov (ekonstan@ie.ibm.com), Software Engineer, IBM Ireland
Photo of NAME
Eugene Konstantinov works on the Websphere Studio RAD Tools team, developing Fileupload/Download components and working on the Visual Editor for Java open source project at the IBM Dublin Software Lab. He is a Sun Certified Web Developer and Java Developer. You can reach Eugene at ekonstan@ie.ibm.com

Summary:  This article describes the Fileupload/Download features of the IBM Extended JavaServer Faces components that are integrated into WebSphere Studio V5.1.2. You can use these components and complementary rapid application development (RAD) tools.to build a Web application that uploads content and then displays it. The article shows how the Faces components work with data using Websphere Service Data Object (SDO) technology.

Date:  01 Sep 2004
Level:  Advanced

Activity:  10977 views
Comments:  

Introduction

IBM® WebSphere® Studio V5.1.2 includes a set of components based on the recently released JavaServer Faces technology (JSR 127). These components were previewed in Websphere Studio V5.1.1 and then reworked to add new features and extend functionality. This article explains in detail how you can use the Fileupload/Download components to create richer user interfaces.

In this article, we will import a JSF Web project with simple JSP pages that demonstrate Fileupload/Download components in action. Then we will look at those pages and discuss usage scenarios for the components. Finally, we will discuss usage scenarios of the download components within a datatable.

Create new web project and import resources used in that article

Before beginning, make sure that you have your access to a DB2 server, because we are going to create a new table.

  1. Download photoalbum.war at the bottom of this article.
  2. Select File => Import => WAR file to start importing photoalbum.war into the new dynamic WEB project.
  3. Browse to the the downloaded WAR file.
  4. Click New to create new dynamic project
  5. Click Next and enter PhotoAlbum as the name of the project.
  6. Check Configure advanced options.
  7. Click Next twice until you get to the Feature page, and then check WDO Relational database runtime.
  8. Click Finish to create the Web project.
  9. Click Finish again to import source pages and configuration files.

Create a new data table

  1. Go to dbsetup folder of your PhotoAlbum project.
  2. Right-click on dbsetup.sql and select Run on Database Server.
  3. Go through the pages of the wizard to provide the options to run the script.

Make sure the table PHOTO has been created

  1. Go to the Data Perspective and find the DB Servers view.
  2. Select a connection to the PHOTO table you created before (Con1)
  3. Right-Click and select Refresh. You should be able to see that the table had been created.

Restore the database connection in the project

  1. In he Web perspective, open upload.jsp from the PhotoAlbum project.
  2. Switch to the Page Data view and double-click on the new_rec SDO record.
  3. Go to the Connections tab and click Edit for the Development connection details.
  4. Select Existing live connections and click Next.
  5. Select the connection that you created before (Con1) and click Finish.
  6. Rebuild project

Fileupload component

Feature description

You use the Fileupload component to upload data from client to server. It emits a <input type="file"/> HTML tag at run time. The Content-Type of the submitted form will be automatically set to multipart/form-data.

Fileupload component characteristics

  • Uploads file using corresponding hx:fileupload tag.
  • Renders standard HTML input tag of the type file.
  • Accepts Websphere SDO as data model.
  • Accepts JavaBeans as data model, supports byte[], java.lang.String, java.io.OutputStream.
  • Optionally, stores filename and mime type of the uploaded item (hx:fileProp tags).
  • Restricts uploaded items by mime type or file extension (attributes accept and exclude).
  • Provides a validation error message if data of the incorrect type is uploaded.

Figure 1. How Fileupload component gets rendered at run time
Figure 1. How Fileupload component gets rendered at run time

Step 1. Run on server experience

The upload.jsp page of our project demonstrates the use of the component. Let's see it in action by selecting that page in the navigator and running it on the WebSphere V5.1 Test Server. Once the server is up and running you should see the following screen:


Figure 2. upload.jsp output
Figure 2. upload.jsp output

Let's take a closer look how this component works:


Code snippet 1. upload.jsp with hx:fileupload tag
...
<hx:fileupload styleClass="fileupload" id="fileupload1" 
           value="#{pc_Upload.new_rec.PHOTO}" accept="image/*">
	<hx:fileProp name="fileName"	value="#{pc_Upload.new_rec.FILENAME}" />
	<hx:fileProp name="contentType" value="#{pc_Upload.new_rec.FILETYPE}" />
</hx:fileupload>
...

As we can see, the Fileupload component has a corresponding tag hx:fileupload that can be bound to the model via its value attribute. The value binding exClickion "#{pc_Upload.new_rec.PHOTO}" sets up a connection to the WebSphere SDO (relational record) that was mapped to the PHOTO column of the type BLOB of our table. To be more precise, the exClickion pc_Upload refers to the managed bean that represents a page code for upload.jsp. new_rec is a property of page code bean that represents an SDO record, while PHOTO is a property of the SDO object that mapped to the PHOTO column(BLOB) of our table.

New feature: hx:fileProp tag

In Websphere Studio V.5.1.1, nested hx:fileProp tags were missing. The proprietary information was kept in the BLOB itself, making data readable only by the IBM components.

The attribute accept=image/* specifies that only images can be uploaded; otherwise an error is displayed. Finally, two hx:fileProp tags are needed to specify extra information about the uploaded item in the same table. The uploaded data may be useless without this meta information. Hence, keeping information about filename and mime type is important if it is possible to upload data of an arbitrary nature.

The attribute excluded="comma-delimited-list-of-extensions" is rarely used and makes sense only in Windows. This attribute throws an error if there is an attempt to submit a file with an extension that is listed as a value of that attribute. For example,excluded="exe,bat" means that upload exe or bat files is not allowed.


Download components

List of download components

  • hx:graphicImageEx
  • hx:outputLinkEx
  • hx:playerGenericPlayer
  • hx:playerFlash
  • hx:playerMediaPlayer
  • hx:playerShockwave
  • hx:playerRealPlayer

General characteristics

  • Download data from back-end server to client
  • Use Websphere SDO as data model
  • Use JavaBeans as data model, support byte[],java.lang.String,java.io.InputStream
  • Don't rely on HTTP session or server file system to read value resource

Step 2. Displaying uploaded image using Image component

Type in a number such as 1 in the ID text box and browse to the image file you want to upload (maximum size is 150 KB). Make sure this is a valid image type (.jpg, .gif, .bmp, and so on) -- otherwise you will get an error stating that an invalid content type was provided.

Try to click Submit, which inserts a new record in the table and navigates to the display.jsp that shows the uploaded image:


Figure 3. display.jsp shows an uploaded image
Figure 3. display.jsp shows an uploaded image

The hx:graphicImageEx tag deals with uploaded image downloading uploaded bytes from the table. By its nature, this component always deals with download. In order to identify the type of the image correctly, it needs to pass that information with help of the "mimeType" attribute, which also works with value binding exClickions. Here's a code snippet that helps clarify:


Code snippet 2. display.jsp with hx:graphicImageEx tag
...
<hx:graphicImageEx id="imageEx1" value="#{pc_Display.disp_rec.PHOTO}" 
			styleClass="graphicImageEx" 
			mimeType="#{pc_Display.disp_rec.FILETYPE}" >
</hx:graphicImageEx>
...

Step 3. Submit more images to the database

Before going on to read the usage scenarios for download within a datatable, try to upload a few more images to the table. Just click the Back button on your browser to return to the upload.jsp and submit more files. Don't forget to give unique IDs, or else you will be updating the same record and overwriting the image that was previously uploaded.


Usage Scenarios for download within Datatable

The Datatable component (formerly known as Datagrid) consists of columns that may contain other components. At run time, it displays records out of the data model, such as a Websphere SDO list. Thus Datatable iterates through the elements of the list and provides values to the child components via a variable defined by the var attribute.

The next section discusses issues related to the download in the Datable.

Default usage scenario

The fact that this scenario is supported by tooling makes it the default. All you need to do is drag and drop download components from the palette to the Datable column.

Step 4. Run on server download.jsp

Try to run the download.jsp page to see the example results.


Figure 4. Datatable with hx:graphicImageEx and hx:outputLinkEx
Figure 4. Datatable with hx:graphicImageEx and hx:outputLinkEx

In this scenario, the component that deals with download uses the variable (defined by the var attribute) of the parent Datatable component. The photos is the SDO List and mapped to the PHOTO table. Notice how the value attribute of the hx:graphicImageEx and hx:outputLinkEx are set (all marked bold):


Code snippet 3. "download.jsp" uses Datatable variable
...
<h:dataTable id="table1" value="#{pc_Download.photos}" var="varphotos" >
  <h:column id="column1" >
   <f:facet name="header">
     <h:outputText value="Id" id="text1"></h:outputText>
   </f:facet>
   <h:outputText id="text2" value="#{varphotos.ID}" >
   </h:outputText>
  </h:column>
  <h:column id="column2" >
    <f:facet name="header">
	  <h:outputText value="Photo" id="text3"></h:outputText>
    </f:facet>
	<hx:graphicImageEx id="imageEx1" value="#{varphotos.PHOTO}" 
	      mimeType="#{varphotos.FILETYPE}"
    </hx:graphicImageEx>
  </h:column>
  <h:column id="column3">
    <f:facet name="header">
	  <h:outputText value="Download" id="text4"></h:outputText>
    </f:facet>
    <hx:outputLinkEx id="linkEx1" value="#{varphotos.PHOTO}" 
          mimeType="#{varphotos.FILETYPE}">
    <h:outputText id="text5" value="#{varphotos.FILENAME}"></h:outputText>
    </hx:outputLinkEx>
  </h:column>
</h:dataTable>
...

In this scenario, Websphere Record List of the parent Datatable is being used by the nested components. The side effect is that the download request will be generated the same number of times as the number of records displayed by the Datatable. This happens because the ValueResourcePhaseListener that deals with download requests has to instantiate the Websphere Record List (SDO List) each time it processes the request. The inefficiency is that, while we only need to get one row, in fact all rows of the Datatable will be populated. This may considerably delay displaying records from a table with a large number of records.

Benefits

  • Easy to generate code (use drag and drop of the Datalist column from the Page Data View)
  • No need to create extra Websphere SDO records

Disadvantage:

  • Excessive access to the underlying database

Step 5. Improved usage scenario: Run on server download_2.jsp

This time try to run download_2.jsp. This page shows exactly the same results as download.jsp. However, this time we created an extra SDO record that eliminates the above problem with the default scenario. Then, any download component can use that SDO record to perform a download. In order to set up a filter condition on that SDO Record, you must use nested f:parameter tags on the download component with a value that identifies a key for the record.


Code snippet 4. "download_2.jsp" is efficient on data access
...
<h:dataTable id="table1" value="#{pc_Download_2.photos}" var="varphotos" >
  <h:column id="column1" >
    <f:facet name="header">
      <h:outputText value="Id" id="text1"></h:outputText>
    </f:facet>
    <h:outputText id="text2" value="#{varphotos.ID}"  >
    </h:outputText>
  </h:column>
  <h:column id="column2" >
    <f:facet name="header">
	  <h:outputText  value="Photo" id="text3"></h:outputText>
    </f:facet>
      <hx:graphicImageEx id="imageEx1" value="#{pc_Download_2.disp_rec2.PHOTO}" 
                            mimeType="#{pc_Download_2.disp_rec2.FILETYPE}" 
         <f:param id="param1" name="ID" value="#{varphotos.ID}"></f:param>
      </hx:graphicImageEx>
  </h:column>
  <h:column id="column3">
    <f:facet name="header">
      <h:outputText  value="Download" id="text4"></h:outputText>
    </f:facet>
    <hx:outputLinkEx id="linkEx1" value="#{pc_Download_2.disp_rec2.PHOTO}"
                        mimeType="#{pc_Download_2.disp_rec2.FILETYPE}">
      <h:outputText id="text5" value="#{varphotos.FILENAME}"></h:outputText>
      <f:param id="param2" name="ID" value="#{varphotos.ID}"></f:param>
    </hx:outputLinkEx>
  </h:column>
</h:dataTable>
...

Benefits:

  • Efficient access of the underlying data

Disadvantage:

  • No default tool support for this scenario
  • Need to create an extra SDO record

Summary

This article described the Fileupload and Download extended JSF components. Together with the sample Web project, they illustrate how they can be used to build dynamic Web pages. This article also demonstrated the use of the download in the Datatable component.



Download

NameSizeDownload method
photoalbum.zip2.6 MBFTP|HTTP

Information about download methods


Resources

About the author

Photo of NAME

Eugene Konstantinov works on the Websphere Studio RAD Tools team, developing Fileupload/Download components and working on the Visual Editor for Java open source project at the IBM Dublin Software Lab. He is a Sun Certified Web Developer and Java Developer. You can reach Eugene at ekonstan@ie.ibm.com

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=23588
ArticleTitle=Developing Web applications using RAD tools, IBM extended JSF components, and WebSphere Studio V.5.1.2
publish-date=09012004
author1-email=ekonstan@ie.ibm.com
author1-email-cc=ekonstan@ie.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers