Professional Documents
Culture Documents
ESCOM-IPN
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.
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:
2/3
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.
3/3