Developing JSP's and Servlets
Looking for a desktop environment to develop and test commercial-quality Java code, JSP's and Servlets? Prefer not to spend a small fortune for the software?
Here's a great alternative to high-priced, commercial software and you just can't beat the price! It's all either free and open source (or just free) and available for downloading on the internet (for commercial work or for provider support, some of the components may have a price tag). After it's set up you'll be able to make changes to your JSP's and servlets and see the effects immediately in a browser. You'll also have many of the debugging features of the Eclipse environment available and the console will be live along with the Tomcat logs for bug tracing.
You can develop WAR files (Java Web Archive files) which can be dropped into the webapps folder of a Tomcat server. Tomcat will magically “explode” the WAR file into the appropriate folders. This makes transferring your website to a server a snap. If you select the option to include the Java files you can also pass your WAR file to other developers who can unzip the WAR into a project inside Eclipse for further development.
This tutorial covers the basics of a JSP and Servlet development environment. It is recommended you learn these basics before moving on to Struts, Tiles, Validation and other advanced frameworks.
The following pages include the steps to load Java, a Tomcat JSP container which works as a server, the Eclipse IDE, and a MySQL database server. The versions are generally up-to-date as of May, 2012. Other versions may be available but remember to read through the various websites to confirm all the components will work together. After the downloads are unzipped, installed and set up you will have eaten 500+ mb's on your hard drive. Even more as you begin to set up the workspaces and databases for development -- plan on a gigabyte of hard-drive space.
The programs were installed on a workstation running Windows 7 in 64-bit mode. The setup is essentially the same for XP, Vista and Windows 7 in 32-bit. If you download the 32-bit versions of the following programs remember to install the programs in C:\Program Files(x86)\ rather than C:\Program Files\. Some of the screen captures were made in Windows Classic mode -- they look old-fashion but are a little easier to read.
As of this update (12/01/2011) the Tomcat link above will display the 7.0.23 version release. You can find previous versions of Tomcat at the same location. In the Binary Distributions section chose the Tomcat version that works on your computer. The final Tomcat folder location will depend on which version you downloaded. The 64-bit version would go into the “C:\Program Files”. The 32-bit version will go into the “C:\Program Files (x86)” folder. Please adjust the locations in the following paragraph accordingly.
After downloading, right click on the zip file and choose Extract All. Browse to “C:\Program Files”; the extraction process will create the “apache-tomcat-7.0.23” folder. If you extract the files to the default folder (which will be the folder where the zip file is located) create the following folder C:\Program Files\apache-tomcat-7.0.23 and move or copy the extracted files to it. The version numbers ("7.0.23") should reflect the version you downloaded. Due to the wonders of Windows 7 security it may be easier to extract to the default folder and copy the files.
There are a couple of steps to associate Tomcat with Eclipse and your project. These will be discussed in the upcoming Eclipse section.
Unlike earlier environments, you no longer have to change the system environment variables to run Tomcat inside Eclipse. If this is your first try at setting up a development environment, you should probably take advantage of this change and forego setting up Tomcat so it will also run outside of Eclipse. However, if you previously created a Java/Tomcat/Eclipse/MySQL environment on your current computer you should update or delete the following entries in the system environment variables.
Changes to the environment variables are made at the following location: Control Panel / System / Advanced System Settings / Environment Variables. You can create, update, and delete system variables in the lower part of the window that pops up.
To run Tomcat outside of Eclipse you will need the CATALINA_HOME variable set in the System Variables section in the bottom half of the Environmental Variables window. The JAVA_HOME variable will also need to be set -- that's in the Java section of this tutorial. You don't need the Classpath and Path variables set.
Classpath: C:\Program Files\apache-tomcat-7.0.23\lib\servlet-api.jar
Path: C:\Program Files\apache-tomcat-7.0.23\bin
CATALINA_HOME: C:\Program Files\apache-tomcat-7.0.23
Restart the workstation for the environment variable changes to take effect.
Online Reference Manual: dev.mysql.com/doc/mysql/en/index.html
MySQL will be installed as a service which will start up with Windows. It is recommended you accept most of the defaults although I did check in the "Include Bin Directory in Windows Path" checkbox (which allows Windows to find executables from the command line if you choose to run it that way occasionally) and you should add a password when given the opportunity -- which you will soon forget so write it down! You may need to configure your firewall to accept the port(usually port 3306).
Grab your favorite MySQL manual and create a database and some tables from the command line. If you're not in love with command line programming give the MySQL Administrator below a try.
The Java-MySQL Connector allows standard Java JDBC commands to access the database. As of December, 2011, the current connector is 5.1.18.
Download and unzip the file linked above. Copy the mysql-connector-java-5.1.14-bin.jar to the following folder in the Java home directory: C:\Program Files\Java\jdk1.7.0_01\jre\lib\ext.
To properly administer a MySQL database you can download or buy the 800+ page manual or you download and use the MySQL Workbench. With the Workbench's GUI interface you can create databases, tables and columns much faster than entering arcane SQL commands in a DOS command box. The WorkBench replaces 3 previous tools: query browser, administrator, and migration toolkit. You can use the Workbench to export databases to other team members on a project.
Find the Workbench tutorial by clicking the Help button or selecting F1.
It's time to start writing code. 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-7.0.5\webapps\examples\jsp\ to get some ideas. In the C:\Program Files\apache-tomcat-7.0.5\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:8080/ in the address of your browser).
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:
Please send any comments or suggestions to firstname.lastname@example.org. It would be a huge help if you report errors in the instructions or just point out hard-to-understand sections. Several hundred Java/Tomcat beginners are using these instructions every day -- so any corrections would help us all.
The Murach book, “Java Servlets & JSP (2nd Edition),” is highly recommended for developers learning the basics of JSP's and servlets. It will help you develop a strong foundation before venturing into Struts, Tiles, Validation, etc. The Hall, Brown, and Chaikin book, “Core Servlets and JavaServer Pages, Vol. 2 (2nd Edition),” is excellent and includes the Struts, Tiles and Validation frameworks. Both are readable by beginners. There are Amazon links to both books on the left column of this page. Using these Amazon links help pay for this site -- thank you very much.
This page was last updated December, 2010. The screen captures are from a Windows 7 computer and may differ slightly from other operating systems. Some captures may also indicate earlier application versions, i.e., C:\Program Files\Java\jdk1.6.0_01 whereas the current version might be C:\Program Files\Java\jdk1.6.0_23.