Page #472 (Review Questions)

 
[Page 1203 ( continued )]

Programming Exercises

Section 34.5 Creating Servlets

34.1* ( Factorial table ) Write a servlet to display a table that contains factorials for the numbers from to 10 , as shown in Figure 34.24.
Figure 34.24. The servlet displays factorials for the numbers from to 10 in a table.


34.2* ( Multiplication table ) Write a servlet to display a multiplication table, as shown in Figure 34.25.
[Page 1204]
Figure 34.25. The servlet displays the multiplication table.

34.3* ( Visit count ) Develop a servlet that displays the number of visits on the servlet. Also display the client's host name and IP address, as shown in Figure 34.26.
Figure 34.26. The servlet displays the number of visits and the client's host name, IP address, and request URL.

Implement this program in three different ways:

  1. Use an instance variable to store count . When the servlet is created for the first time, count is . count is incremented every time the servlet's doGet method is invoked. When the Web server stops, count is lost.

  2. Store the count in a file named Exercise34_3.dat, and use RandomAccessFile to read the count in the servlet's init method. The count is incremented every time the servlet's doGet method is invoked. When the Web server stops, store the count back to the file.

  3. Instead of counting total visits from all clients , count the visits by each client identified by the client's IP address. Use Map to store a pair of IP addresses and visit counts. For the first visit, an entry is created in the map. For subsequent visits, the visit count is updated.


[Page 1205]

Section 34.6 HTML Forms

34.4* ( Calculating tax ) Write an HTML form to prompt the user to enter taxable income and filing status, as shown in Figure 34.27(a). Clicking the Compute Tax button invokes a servlet to compute and display the tax, as shown in Figure 34.27(b). Use the computeTax method in the ComputeTax class introduced in Listing 6.11, ComputingTax.java, to compute personal income tax for year 2002.
Figure 34.27. The servlet computes the tax.

34.5* ( Calculating loan ) Write an HTML form that prompts the user to enter loan amount, interest rate, and number of years , as shown in Figure 34.28(a). Clicking the Compute Loan Payment button invokes a servlet to compute and display the monthly and total loan payments, as shown in Figure 34.28(b). Use the Loan class introduced in §7.15, "Case Study: The Loan Class," to compute the monthly and total payments.
Figure 34.28. The servlet computes the loan payment.

34.6** ( Finding scores from text files ) Write a servlet that displays the student name and the current score, given the SSN and class ID. For each class, a text file is used to store the student name, SSN, and current score. The file is named after the class ID with the .txt extension. For instance, if the class ID were csci1301, the file name would be csci1301.txt . Suppose each line consists of student name, SSN, and score. These three items are separated by the # sign. Create an HTML form that enables the user to enter the SSN and class ID, as shown in Figure 34.29(a). Upon clicking the Submit button, the result is displayed, as shown in Figure 34.29(b). If the SSN or the class ID does not match, report an error. Assume there are three courses available: CSCI1301, CSCI1302, and CSCI3720.
[Page 1206]
Figure 34.29. The HTML form accepts the SSN and class ID from the user and sends them to the servlet to obtain the score.

Section 34.7 Database Programming in Servlets

34.7** ( Finding scores from database tables ) Rewrite the preceding servlet. Assume that for each class, a table is used to store the student name, ssn, and score. The table name is the same as the class ID. For instance, if the class ID were csci1301, the table name would be csci1301.
34.8* ( Changing the password ) Write a servlet that enables the user to change the password from an HTML form, as shown in Figure 34.30(a). Suppose that the user information is stored in a database table named Account with three columns , username, password, and name, where name is the real name of the user. The servlet performs the following tasks :
  1. Verify that the username and old password are in the table. If not, report the error and redisplay the HTML form.

  2. Verify that the new password and the confirmed password are the same. If not, report this error and redisplay the HTML form.

  3. If the user information is entered correctly, update the password and report the status of the update to the user, as shown in Figure 34.30(b).

Figure 34.30. The user enters the username and the old password and sets a new password. The servlet reports the status of the update to the user.


[Page 1207]
34.9** (Displaying database tables) Write an HTML form that prompts the user to enter or select a JDBC driver, database URL, username, password, and table name, as shown in Figure 34.31(a). Clicking the Submit button displays the table content, as shown in Figure 34.31(b).
Figure 34.31. The user enters database information and specifies a table to display its content.

Section 34.8 Session Tracking

34.10* ( Storing cookies ) Write a servlet that stores the following cookies in a browser, and set their max age for two days:
  • Cookie 1: name is " color " and value is red.

  • Cookie 2: name is "radius" and value is 5.5.

  • Cookie 3: name is "count" and value is 2.

34.11* ( Retrieving cookies ) Write a servlet that displays all the cookies on the client. The client types the URL of the servlet from the browser to display all the cookies stored on the browser. See Figure 34.32.
Figure 34.32. All the cookies on the client are displayed in the browser.

Comprehensive

34.12*** ( Syntax highlighting ) Create an HTML form that prompts the user to enter a Java program in a text area, as shown in Figure 34.33(a). The form invokes a servlet that displays the Java source code in a syntax-highlighted HTML format, as shown in Figure 34.33(b). The keywords, comments, and literals are displayed in bold navy, green, and blue, respectively.
[Page 1208]
Figure 34.33. The Java code in plain text in (a) is displayed in HTML with syntax highlighted in (b).

34.13*** ( Opinion poll ) Create an HTML form that prompts the user to answer the question "Do you support the Iraq war?", as shown in Figure 34.34(a). Upon clicking the Submit button, the servlet increases the Yes or No count in a database and displays the current Yes and No counts, as shown in Figure 34.34(b).
Figure 34.34. The HTML form prompts the user to enter Yes or No for a question in (a), and the servlet updates the Yes or No counts in (b).

Create a table named Poll , as follows :

   create table   Poll (    question varchar(   40   )   primary key   ,    yesCount   int   ,    noCount   int   ); 

Insert one row into the table, as follows:

   insert into   Poll   values   ('Do you support the Iraq war? ',     ,     ); 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net