You are on page 1of 3

Web Application Development Learning Unit: Web Application Development THEMATIC UNIT: 4 TITLE: Introduction to JSP

ESCOM-IPN

PRACTICAL 4 Introduction to JSP using Eclipse

Part 1 Installing Tomcat 7 and Using it with Eclipse


1. Install Java. Download for Windows, MacOS, Linux, and Solaris from http://www.oracle.com/technetwork/java/javase/downloads/. As of early 2013, I used JDK 1.7.0_10 and 1.6.0_35 (the latest versions at the time), but any Java 6 or 7 version will work. Servlet 3.0 containers (of which Tomcat 7 is one) require Java 6+, and will not work with Java 5. You want the full JDK (with compiler), not just the JRE (for running existing apps). Accept all defaults when installing. 2. Unzip Tomcat. You can download any Tomcat 7 version from the Tomcat Web site and then unzip . 3. Install and start Eclipse. Download from http://www.eclipse.org/downloads/. Choose "Eclipse IDE for Java EE Developers", download, and unzip. As of early 2013, the latest version is Eclipse 3.8 (Juno), and is what I use as of the latest update of this tutorial. However, these instructions were also tested with Eclipse 3.7 and 3.6. Double click on eclipse.exe in the eclipse folder, then click on "Workbench". 4. Tell Eclipse about Tomcat. Click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v7.0, navigate to Tomcat 7 installation folder (e.g., C:\apache-tomcat-7.0.34), OK. If you don't see Servers tab, add the tab via Window, Show View, Servers. 5. Run Tomcat. Click on Servers tab at bottom. R-click on Tomcat v7.0, choose "Start". Open http://localhost/8080 in a browser. Either way, you will see a 404 error message, but at least the message comes from Tomcat. Then, copy the ROOT app as described in the next section, come back, and reload http://localhost/ (or http://localhost:8080/ if using the unmodified version from the Tomcat download site). You should now see a friendly Tomcat welcome page. 6. Copy the ROOT (default) Web app into Eclipse. Eclipse forgets to copy the default apps (ROOT, examples, docs, etc.) when it creates a Tomcat folder inside the Eclipse workspace. Go to C:\apache-tomcat-7.0.34\webapps and copy the ROOT folder. Then go to your Eclipse workspace, go to the .metadata folder, and search for "wtpwebapps". You should find something like C:\your-eclipse-workspacelocation\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps (or .../tmp1/wtpwebapps if you already had another server registered in Eclipse). Go to the wtpwebapps folder and paste ROOT (say "yes" if asked if you want to merge/replace folders/files). Then reload http://localhost/ to see the Tomcat welcome page. Tanibet Prez de los Santos Mondragn 1/3

Web Application Development

ESCOM-IPN

7. Create and test a new Web App. File, New, Project, Web, Dynamic Web Project. Make sure that the Target runtime is "Apache Tomcat v7.0". 8. Tweak Eclipse preferences. Window, Preferences, then many options re font sizes, import and indentation styles, and so forth. At a minimum, set the JDK location: Window, Preferences, Java, Installed JREs, and make sure that a JDK (not just JRE) is selected. If not, press Add, and then navigate to the base JDK folder. If you develop with servlets, you probably also want to suppress unnecessary warnings about Serializable classes for servlets. Window, Preferences, Java, Compiler, Errors/Warnings, change "Serializable class without ..." to "Ignore". 9. Bookmark the servlet & JSP Javadocs. Add the servlet 3.0, JSP 2.2, and EL 2.2 API to your bookmarks/favorites list.

Part II Comparing JSP Scripting Elements


Task 1
Output a bulleted list of five random ints from 1 to 10. Since the structure of this page is fixed and we use a separate helper class for the randomInt method, JSP expressions are all that is needed.

Task 2
Generate a list of between 1 and 10 entries (selected at random), each of which is a number between 1 and 10. Because the number of entries in the list is dynamic, a JSP scriptlet is needed.

Task 3
Generate a random number on the first request, then show the same number to all users until the server is restarted. Instance variables (fields) are the natural way to accomplish this persistence. Use JSP declarations for this.

Task 4
Make a JSP page that randomly selects a background color for each request. Just choose at random among a small set of predefined colors. Be sure you do not use the CSS style sheet, since it overrides the colors given by <body bgcolor="...">. Note: in Eclipse, allJava code goes in src/subdirectory-matching-packageName, regardless of whether the Java code is a servlet or some other kind of Java class. Then, if you want a class called ColorUtil s with a static method called randomColor and have it be in a package called utils, you might write something like this:

Tanibet Prez de los Santos Mondragn

2/3

Web Application Development


package utils; public class ColorUtils { public static String randomColor() { if (Math.random() < 0.5) { return("BLUE"); } else { return("RED"); } } }

ESCOM-IPN

In JSP, you can call the method with <%= utils.ColorUtils.randomColor() %> Also, if you want to test some Java code separately from a JSP page, note that in Eclipse, if your class has a main method, you can right-click anywhere in the file, choose Run As --> Java Application, and Eclipse will run main and put the output in the console at the bottom.

Tanibet Prez de los Santos Mondragn

3/3

You might also like