Web and User Interfaces


When working with a JSP or servlet, you may be accustomed to retrieving data from a javax.servlet.ServletRequest using the getParameter() and getParameterNames() methods. This works great for simple HTML-based forms, such as the one shown in Listing 2-1. The standard servlet method, request.getParameter("textfield"), is used to retrieve data submitted when the user clicks the Submit button.

Listing 2-1. Simple Request JSP
 <head>   <title>Request Example</title>   <meta http-equiv="Content-Type"      content="text/html; charset=iso-8859-1" /> </head> <body> <%= request.getParameter("textfield") %><br /> <form name="form1"  method="post" action="request_example.jsp">   <input type="text" name="textfield" />   <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> 

The JSP page as rendered in a browser is shown in Figure 2-1.

Figure 2-1. Simple request starting page.


As you can see in Figure 2-1, the initial page display shows "null" for the request.getParameter("textfield") call. Entering the words "Hello World!" into the text field and clicking the Submit button sends a page request back to the server (as specified by the action attribute). The server parses the request, and the form data is made available to the JSP. This can be seen in Figure 2-2.

Figure 2-2. Simple request parsed response.


This standard JSP/servlet mechanism works fine for normal HTML controls (check boxes, radio buttons, text fields, text areas, buttons, etc.). Unfortunately, it fails to handle file uploads using the <input type="file" name="file_upload"> HTML tag.

Listing 2-2. Sample File Upload Form
 <form action="request_example.jsp" method="post" enctype="multipart/form-data" name="form1" >     <input type="file" name="file" />     <input type="submit" name="Submit" value="Submit" /> </form> 

The code shown in Listing 2-2 is an example of an HTML file upload form. Notice the addition of the enctype="multipart/form-data" attribute to the form, as well as the <input type="file" name="file" /> tag. The enctype attribute governs how the browser should package and send the data back to the server (for more information on this, read RFC 1867 at http://www.ietf.org/rfc/rfc1867.txt).

Figure 2-3 shows the <input type="file" name="file_upload"> HTML tag as rendered by Internet Explorer.

Figure 2-3. FileUpload user interface.


The Jakarta Commons FileUpload component provides support for the file upload capability of a typical web browser, letting you go beyond the limitations of the parameter parsing of the standard servlet system. FileUpload will parse the data returned by the multipart/form-data form and file input type as sent by your browser.



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net