Java Tutorial - Java Script :
JSP
Creating a Web Application:
By combining Java classes, servlets, and JSP, you can create interactive web applications—
sites that dynamically generate content in response to user input in a sophisticated, cohesive way. Every time you shop on an e-commerce site such as Amazon.com or use an online reference such as the Internet Movie Database (IMDB), you are running a web application.
To see how several aspects of Java technology can work together on the Web, you create Guestbook, a web application that enables visitors to leave a message for the creator of a
site. The Guestbook project is made up of three things:
· guestbook.jsp—A JSP page that displays guestbook entries from a text file on a web server and provides a form where a visitor can add an entry
· guestbook-post.jsp—A page that saves a new guestbook entry to the text file
· Guestbook.java—A class used to filter out some characters before they are saved in the guestbook
The JSP page in this project makes heavy use of scriptlets and expressions. Listing 21.9 contains the source code for guestbook.jsp.
After you save this page, store it in any folder on your server where JSP pages can be stored. You can test this even before anything else in the project is done, as long as you have an empty guestbook file. To create this file, save an empty text file on your system and give it the name cinema.gbf. Store it on the Web in the webapps folder relative to where the guestbook page has been stored. For instance, if the page is in webapps\jspexamples\guestbook. jsp, the text file should be saved in \webapps\jspexamples. When you load this JSP page, you must include a parameter that specifies the ID of the guestbook to load, as in this URL:
The server name and folder depend on where you have published guestbook.jsp. Figure 21.8 shows what your guestbook looks like when your page compiles successfully and tries to display the contents of the cinema.gbf fil
The guestbook file stores each guestbook entry on its own line, with a caret (‘^’) separating each field in the entry. Visitors can provide their name, email address, home page address, and a comment. Two other things are saved for each entry: the date and time it was written and the IP address of the visitor. The following text is an example of a guestbook file that contains one entry: Puddin N. Tane^puddin@example.com^None^Sat Feb 24 18:29:17 EST 2007^127.0.0.1^Ask me again and I’ll tell you the same. The next JSP page to create is guestbook-post.jsp, the page that updates the guestbook with new entries submitted by visitors. Listing 21.10 contains the source code for this
The guestbook-post.jsp page collects data from a web form, removes characters from the data that can’t be put in the guestbook, and stores the result in a text file. Each guestbook has its own file with a name that begins with the ID parameter of the book and ends with the .gbf file extension. If the guestbook has the ID of cinema, the filename is cinema.gbf. Like the other JSP pages included in this web application, guestbook-post.jsp can be stored in any folder on your server where JSP pages are kept. For this project, store the page in the same folder as guestbook.jsp and cinema.gbf. Before you can try the Guestbook application, you must create a Java class that is used to filter some unwanted text from guestbook entries before they are posted. Three characters cannot be included in the guestbook because of the way entries are stored in a file:
· Caret characters (“^”)
· Return characters, which have the integer value of 13 in Java
· Linefeed characters, which have the integer value of 10
To remove these characters before they are saved in a guestbook, a helper class called Guestbook is created. This class has a static method called filterString(String) that removes those three characters from a string.
Listing 21.11 contains the source code for this classThe replaceText() method in lines 11–21 of Listing 21.11 does most of the work in the class. It takes three arguments:
· A string that might contain unwanted characters
· A character that should be removed
· A character that should be added in its place
·
When you compile the Guestbook class, it should be stored in a WEB-INF\classes\ example subfolder in the same part of the webapps hierarchy as the project’s JSP page. For example, if the pages are in webapps\jspexamples\jsp, the class file should be saved in webapps\jspexamples\WEB-INF\classes\example. To test the guestbook-post.jsp page, open the page that displays guestbook entries using an ID parameter of cinema again, as in this example:
http://localhost:8080/java/guestbook.jsp?id=cinema Add a few guestbook entries to see how they are displayed in the guestbook.
