Java Projects

It's time to start writing code. I'm working on a simple project sample to include on this page later this summer. A great book to use in setting up a first project is Murach's Java Servlets and JSP. It contains a couple of relatively easy projects (complete with code) including a mailing list program that makes a perfect beginner's project.

Tomcat has also provided several sample jsp files in the webapps directory. Navigate to C:\Program Files\apache-tomcat-5.5.17\webapps\jsp-examples to get some ideas. In the C:\Program Files\apache-tomcat-5.5.17\webapps\ROOT directory take a look at index.jsp. It's the page that came up when you originally set up the Tomcat directory above (which you reach with http://localhost/ in the address of your browser).

You could copy files to the Tomcat Root directory like the Tomcat samples, however, this would put unnecessary files in that important folder. There are several better ways to create a development environment for testing JSP's. Two are described below; one using a context XML files to link your files to Tomcat and one using a WAR file to quickly move your files to the webapps directory. Either way, the JSP page should display in your browser using an address such as this: http://localhost/‹context name›/.

Fortunately, almost any question you may have about programming has been asked before -- usually several times -- in the newsgroups. Just reading the current entries will quickly increase your understanding of Java programming. It will also help you realize you're probably not the dumbest Java programmer in the world -- although, obviously, one of us is that person! For answers and suggestions about writing Java code, check the following Google newsgroups:


Project Setup and Testing from a workspace

Tomcat PropertiesRight click on the project in the Eclipse Navigator window. Select Properties --> Tomcat from the window popup. Check the box entitled “Is a Tomcat Project”. Enter the context name under the “General” tab. The context name is the name you will use to call the project in a browser. Check the first two checkboxes (the third checkbox is optional):

  • Can update context definition
  • Mark this context as reloadable

An xml file will be created automatically in C:\apache-tomcat-5.5.17\conf\Catalina\localhost\JET.xml. The file name (“JET” in the example) will reflect the name of your context. The code will look something similar to the file below. This file allows you to make changes in your code and see it reflected immediately in your testing without restarting Tomcat.

<Context path="JET" reloadable="true" docBase="C:\Users\Greg\Documents\Workspaces\JetExample\JET" workDir="C:\Users\Greg\Documents\Workspaces\JetExample\JET\work" />

It takes little time in Eclipse to rebuild the project and restart Tomcat and, occasionally, that seems to be the only way to get your project back in sync.

Alternatively, you can export a WAR file as described in the next section to your local Tomcat server as another way to test.


Creating a WAR file

Tomcat WAR file exportRight click on the project in the Eclipse Navigator window. Select Properties --> Tomcat from the window popup. Check the box entitled “Is a Tomcat Project”. Then select the “Export to WAR settings” tab (see the image below). Browse to the Tomcat webapps directory: C:\Program Files\apache-tomcat-5.5.17\webapps and enter the name of the WAR file in the “WAR file for export” text box. This file name (“JET” in this example) will be the context name you will use in the address bar of the browser during testing.

If you also select the “Export java files” the subsequent WAR file will include the source Java code along with the class files. Including the source files is not necessary for the Tomcat servlet container; however, the WAR file is a ZIP file and can be used to transport the source code to other team members on small projects if desired.

To test your pages in a browser using a WAR file, do a build of the project (under Project in the Eclipse toolbar). Then right click again on the project in the Package Explorer window in Eclipse and select “Tomcat Project”. It will open a sub-menu. Select “Export to the WAR file sets in project properties.” You should get a “successful” popup window. The WAR file has been placed in the directory you entered in the paragraph above. Opening a browser window to http://localhost/<context name> will bring up your project’s default home page by exploding the WAR file into its own folder. The WAR file is used after coding and testing to export your program to a remote server.