Seeing as I am giving a short talk at JavaOne 2012 concerning GlassFish and our usage of it and how potentially it could help others, I thought I’d create a small example application in NetBeans for those interested but don’t know where to start. You can download it here, in order to run it you will also need the Apache Commons libraries, specifically commons-fileupload and commons-io.
The simple example application allows a user to navigate to a web page and upload a document to a directory of their choosing. Unlike our converter it doesn’t display a progress bar since that’s a bit more advanced.
Below is a screenshot of the Applications front end, index.jsp.
It’s appearance is due to style rules inside a style sheet. It’s actual code is very simple:
<%@page contentType="text/html" pageEncoding="UTF-8"%><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link href="style.css" rel="stylesheet" type="text/css" />
As you can see the jsp file is not very complicated at all, it’s just a simple form with some styling. The real magic comes from the java file provided and the wonderful Apache Commons libraries.
Within the java file (I’m not going to place the code within this article since it’s quite long) you will find the function called processRequest() it handles both GET and POST methods and serves as the entry point for our example. Within it you will find a call to a function I have created called doRequest() which has a few similar parameters to processRequest(). I could have forgone a seperate function entirely and placed all the code within doRequest() inside processRequest() but found separating them more aesthetically pleasing.
A quick run through of what we are doing in the code:
- doRequest() begins with setting some session attributes to default values, ensuring that the attributes exist.
- It then makes sure the request we are getting is correct and part of the right kind of form.
- Now we set up the required objects to parse the request and begin parsing it.
- In the while loop we then check each field within the form data we have received.
- If it is a form field we record it’s information in a session variable, in this case we only have one form field called dir.
- If it is not a form field it must be a file that’s being uploaded so we record this in a variable set up before the loop so we can refer to the file later.
- Next we do a few checks to make sure the file was uploaded and it was the right type of file (in the example I am using PDFs). We also make sure the user supplied a directory to place the file in.
- Now we set up the path we want to save the file in, make sure the directory structure exists and save the file to disk, remove it from memory and set an appropriate response for the user to read.
- Finally after going through all these steps the program will output a very simple web page titled Response, with a message either saying the file was successfully uploaded or showing the error that occurred.
If you then navigate to your GlassFish domains folder (NetBeans normally places it somewhere similar to C:\Users\yourUsername\AppData\Roaming\NetBeans\7.2\config) you will see a directory called fileUploads with a subdirectory containing your file.
Our online PDF to HTML5 converter is basically a much more advanced version of this example, in fact the general structure is almost identical; except we have some extra code relating to the progress bar and converting the uploaded file.
If you want to find out a whole load more about GlassFish and you are attending JavaOne make sure you come to the GlassFish Unconference on Sunday 30th September 2012. See you there!
This post is part of our “GlassFish Articles Index” series. In these articles, we aim to explore GlassFish in different ways, from useful tutorials, to JaveOne and general.