This page covers Java Server Side Application development. This includes development of Servlets, Java Server Pages (JSPs), Tag Libraries, as well as MVC (Model View Controller) frameworks such as Struts and (JSF) Java Server Faces.
Originally all of these applications were developed by hand with Visual Slickedit. I chose to develop these applications by hand to enhance my knowledge of what was going on behind the scenes. Visual Slickedit is a great general purpose editor that has many nice features (most notably its speed). However it is missing many of the convenience features offered other dedicated Java development IDE's such as: code completion, automatic buildfile generation, drag and drop UI development, debugging and profiling, Java refactoring, wizards, and other code auto-generation utilities. While these are great features, they would have had the effect of removing me the underpinnings of the build and deployment processes. But thats not what I wanted. I am no fan of FM (F-ing Magic). I didn't want auto-generation/creation/completion. I wanted to have to figure these out for myself. It was sometimes painful, but I believe valueable lessons were learned.
I have since migrated to Netbeans for the development of my Java Web Applications. While I derive many benefits from the aformentioned convenience features, I also spend a great deal of time waiting for the application to load and run.
In either case, I have done development of this page, the database page, and the xml page in both SlickEdit/Tomcat and Netbeans/Glassfish. Some minor changes were required, but generally the Java code is unchanged. One of the major differences between Tomcat and Glassfish is that Tomcat utilizes the context.xml file, and Glassfish the sun-web.xml.
ZIP files for either environment are available for the asking. Please send me an email at hhartley@pobox.com identifying which applicaiton for which environment you are interested.
For most of the applications on the server, and specifically in this section. I use ANT build files to build and deploy the applications. I follow the recommendations from the tomcat documentation for the deployment directory structures and configuration files. This documentation can be found here.
My development directory structure looks like this:
Java code in the src directory. JSP pages in the web directory (unless otherwise stated). Web.xml in the WEB-INF directory. Classes and lib are empty unless libraries jar files or property files are necessary. In the case of struts, all the the struts jar files are in the lib directory. In cases where an application requires its own context.xml, it will be located in the root application directory (in the example above CookieServlets). After an ant build, a war directory will be created, and the war file will be at the root application directory.
| Code | Description/Comment |
|---|---|
| build.xml | This is he core of my build environment. I found one in an article a few years ago, and have made some changes. |
| build.properties | Properties for the buildfile. Mostly just high level configuration items. |
| context.xml | Used primarily to avoid resource locking in tomcat applicaitons. Also used to define data source resources. |
I try to use NetBeans as generically as I can. Netbeans offers build and deployment structures based upon java blueprints. Please click here for more information. Unlike the development environment above, Netbeans manages all of the ant build scripts itself, therefor the buildfiles and build properties identified in the applicaiton below are specific to the tomcat/slickedit environment.
| Code | Description/Comment |
|---|---|
| sun-web.xml | Glassfish also makes use of the sun-web.xml for much configuration and resource definition. This file will not be shown unless it is significant. |
This application is very simple, it merely prints hello world, and dumps session and servlet information.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| HelloWorldServlet.java | Gets servlet information and prints it to the screen. |
| web.xml | Your basic do nothing web.xml. Identifies the Servlet name, class, and URL. |
This application is very simple, you enter data in an HTML form, and the data is echoed in a web page returned by the servlet. One interesting detail regarding this application is that the form is served by apache out of one directory, and the response page is rendered by a servlet and Tomcat in the web applications directory.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| BasicFormServlet.java | A basic servlet that will read the request parameters, and print html back out in response. |
| BasicForm.html | This is the html form that posts a request to the servlet. |
| web.xml | Same as HelloWorldServlet, but changing the name of the application to BasicFormServlet. This file exists for all applications with very minor variations and won't be shown in subsequent tables. |
This application consists of two Servlets. One to set a cookie, and the other to display the cookies that have already been set. Cookies can be set for the duration that a browser is open, or for longer. The cookies in this application are set for 30 days from the time they are entered in the form.
This application makes use of two Servlets, both deployed with same '.war' file. See the web.xml file listed below. Also notice the action in the form.
To set the cookie, click here.
To display all cookies set for this browser, click here.
| Code | Description/Comment |
|---|---|
| SetCookieServlet.java | Handles the form data from the CookieForm.html and sets a cookies. Echoes form data back to the page. |
| DisplayCookieServlet.java | Retrieves all cookies and writes them to a page. |
| CookieForm.html | A from that prompts for the cookie name and the cookie value. |
This application will get today's date, and increments a counter from 1 to 100 displaying the results. The web.xml of this application differs slightly in that it specifies a welcome file. See web.xml below.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| DateCountJSP.jsp | Gets the date and counts from 0 to 100. |
This application will dump, servlet, session, cookie, header, and request information. Basically this application is almost identical to the HelloWorldServlet in that it dumps the same data. But it is done in the JSP file, rather than in a servlet. An interesting thing to note in this JSP application is that all the servlet variables are already available and do not need to be declared or gotten.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| JSPDataDump.jsp | Gets basic servlet data and dumps it to the page. |
This application will prompt the user to enter some data, and then display it back to the screen. The interesting point of this JSP is that it is a single JSP with conditional logic, and when it does the form action, it is actually calling itself, but the request parameters are not empty.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| TermDefForm.jsp | All in one JSP that prompts for a term and definition, and echoes it back to the page. |
This application will prompt the user to enter some data, display the aggregate of all data entered so far, and continue prompting the user until the user is done. The interesting point of this application is that it utilizes HTML forms, JSP pages, servlets and session data to perform its tasks.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| GlossaryCtlServlet.java | This is the center of the application. It determines which pages to forward to, and updates the array (which it also created in this servlet if it does not already exist in the session scope). |
| GlosaryItemBean | A bean which is used to hold the glossary entry. |
| firstpage.jsp | A form with prompts for a glossary entry. Includes all html files below. |
| mainpage.jsp | Includes displaylist.jsp and all other html files. |
| displaylist.jsp | Displays all the glossary entries entered so far. Basically just loops through the array structure and puts the bean properties in a table. |
| header.html | Just the end of an html file. |
| footer.html | Just the beginning of an html file. |
| add_term_form.html | A form for adding the term and definition. Included in both JSP's. |
| clear_button_form.html | A form that includes only a button to clear the application and start from scratch. |
This application will prompt the user to enter some data, do validation checks, then display the result to the screen. This application is a heavily modified example from Java World magazine. Since it is someone else's code, I will not provide the code below.
To see the application in action, click here.
Sorry, no code provided.Note: These struts applications require that the struts .jar files be present in .../web/WEB-INF/lib directory, and the taglibs be present under in .../web/WEB-INF directory. Any .properties files should be present under the .../web/WEB-INF/classes directory.
This application accomplishes the same basic functionality as the JSP/Servlet Hybrid #1 (above). The MVC (Model View Controller) architecture comes at a price. While struts is very powerful in that it separates business logic from screens, it has many parts that must be setup correctly in order to operate correctly. This struts application confirms that I have the basic plumbing setup correctly. It will utilize a form bean, and perform validation in that bean. Once the form data has been entered correctly, the user will be forwarded to a screen the displays all items added so far, the last item added (if no errors exist), and a prompt for a new entry. Take a look at the struts-config.xml. It uses two actions with basically the same setup. The difference is the input line.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| struts-config.xml | The controller for this MVC application. |
| AddItemAction.java | The one and only Action in this application. Adds the term and definition to the ArrayList and forwards to ShowItem.jsp. If cancel is clicked it invalidates the session (and thus array) and forwards the application back to the AddItem.jsp. |
| GlossaryForm.java | A from bean that includes the term and definition properties. |
| GlossaryResultBean.java | A result bean that ultimately gets drawn on the ShowItem.jsp, AND stored in the array. |
| AddItem.jsp | A simple form for adding the term and definition. |
| Hello.jsp | Simple JSP with a link to the application. |
| ShowItem.jsp | The core of the application. It shows the last item added. All items added so far, and prompts for the addition of a new item. Uses the add action when new items are added in the form and submit is clicked |
| web.xml | Nothing special, but includes all of the struts stuff. |
| ApplicationResource.properties | Application resources used for prompts and for errors. More use could be made of this, but it demonstrates the basic usage. |
This application build on the prior Struts application (Glossary2). This application demonstrates multiple forms in a single JSP How to dispatch to multiple actions from a single form. Use of simple javascript onclick to set hidden fields. Use of a welcome list JSP that forwards to an action. And More. The application as it stands is incomplete. The search function is not implemented. The previous and next buttons are not implemented. All these unimplemented elements will be handled in the next application (see the My SQL button) which will utilize a database to store this data, rather than a temporary session scoped array.
There are some known issues with this application, but since it is only a demonstration of concepts, these issues are in a sense intentional. If the browser sits on a page for a long period of time, the session times out, the application basically starts over or gives an error condition.
The handling of dispatch elements can be done more efficiently without using hidden forms and execute, but rather with dispatch actions.
One other note since I am going on and on. Both struts applications so far have been done almost solely with the Struts taglib support. I have found the syntax of the taglibs fairly clean in the JSP pages, but also kind of hard to work with. It is possible that I will do another application demonstrating JSTL. Also at some point, I might like to roll my own tag library.
To see the application in action, click here.
| Code | Description/Comment |
|---|---|
| struts-config.xml | The controller for this MVC application. This is the core of the app. |
| ActionGlossarySetup.java | Builds the array to be used by the rest of the application and forwards to the mainpage. |
| ActionAddItem.java | Gets the form data and adds it to the array. Includes processing for the cancel button being pressed. |
| ActionDeleteItem.java | Removes the glossaryItem from the array. Receives the index in the form data which was forwarded to it from the ActionModDelDispatch action. |
| ActionModifyItem.java | Modifies the glossaryItem in the array at the index in the formGlossaryItem. |
| ActionDoAdd.java | Forwards to the AddPage.jsp. |
| ActionModDelDispatch.java | Determines if a modify or delete were selected, and adds the index of the item to be modified or deleted to the form data, then forwards to the modify or delete pages. |
| ActionPrevNextDispatch.java | For now, this does nothing but forward back to the mainpage.jsp. |
| ActionSearchItem.java | For now, this does nothing but forward back to the mainpage.jsp. |
| BeanGlossaryItem.java | A general purpose bean to hold a glossary item in the array. Has some helper methods to transfer to and from the FormGlossaryItem. |
| FormEmpty.java | An empty form bean because I have used a form in the mainpage.jsp. <-- There is a better way to do this. |
| FormGlossaryItem.java | The glossary item from. Holds the term and definition from the add, modify, and delete pages. Does validation on the existence of the term and definition. Has some helper methods to transfer to and from the BeanGlossaryItem. |
| FormModOrDel.java | Holds mod or del to determine if a modification is required. Also holds the index (in string form) of the item to be modified or deleted. |
| FormPrevOrNext.java | Holds the prev or next to determine if the previous or next button were pressed. |
| FormSearchItem.java | Holds the search item. |
| MainPage.jsp | This page consists of four forms and is the name suggests the main page. All actions begin here. The first form is the search item from. It will transfer control to ActionSearchItem using FormSearchItem. The second form is the mod or del from neatly displayed in a table with many buttons. This from will transfer control to ActionModDelDispatch using the FormModOrDel. The third from is the doAdd from. It passes nothing to the ActionDoAdd in the FormEmpty. The fourth and last form is the previous or next form. It passes control to ActionPrevNextDispatch using the FormPrevOrNext. |
| AddPage.jsp | This page prompts for term and definition. It includes reset and cancel. This form is processed by ActionAddItem using the FormGlossaryItem. |
| ModifyPage.jsp | This page prompts for term and definition. It includes reset and cancel. This form is processed by the ActionModifyItem using the FormGlossaryItem. |
| DeletePage.jsp | This page prompts only allows buttons to be pressed and is processed by the ActionDeleteItem using the FormGlossaryItem. |
| Startup.jsp | This page merely forwards control to the ActionGlossarySetup. |
| web.xml | Nothing special. Includes struts stuff used. |
| ApplicationResource.properties | Resource file for prompts and error messages. |
To see a completed version of this example which maintains the list of terms and definitions, and enables all the buttons and the search function, click here.
JSTL is simply another tag library. I used it in MyGlossaryOneFive due to the simple fact that it was the only way to include a JSP that runs in a different context. I also use this feature across the website for including menus and navigation JSP fragments in these JSP pages.
I have newly installed Glassfish V2 and the Netbeans 5.5. This enables super easy development of Java Server Faces. Netbeans provides some useful pre-built components to learn more about these, click here. Netbeans also offers the Visual Web Pack (VWP) which provides drag and drop UI and database integration. While I am not a fan of being restricted to Sun's idea of style and design (fixed grid, sun only stylesheets), the ugly syntax of the JSP's, or the fact that there is a lot of FM (f-ing magic) going on, there is no dispute that for simple applications VWP can generate applications very quickly.
This application was developed with the Netbeans Visual Web Pack. It utilizes the database of MyGlossaryOneFive, but does not utilize any of the previously written code. I dragged a drop down and table onto the webpage, then bound these to database tables/queries. Then built a DOM Document (the actual purpose of the example), forward the client to a new page, and dump the DOM xml. To see the source code, or the application in action, click here.
This is a much more natural way of coding JSF. Very similar to struts and easy to use. It uses managed beans with EL to access the managed bean. To see the source code and application in action, click here
Servlets, JSP's, Struts, and JSF are cool. I will have more to say comparing and contrasing Struts with JSF with doing it yourself once I have had more experience with JSF. Until then I will say that I do not care for the VWP applications style of developing web based applicaitons as it is too restrictive and implements someone elses view of what good style is.