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. 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:
|
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. 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. |
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 :
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. |
| |
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. |
34.10* | ( Storing cookies ) Write a servlet that stores the following cookies in a browser, and set their max age for two days:
|
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. |
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. 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? ', , ); |