数据库CH
数据库CH
数据库CH
9
Application Design and
Development
Exercises
9.12 Write a servlet and associated HTML code for the following very simple
application: A user is allowed to submit a form containing a value, say n,
and should get a response containing n “*” symbols.
Answer:
HTML form
<html>
<head>
<title>DB Book Exercise 8.8 </title>
</head>
<form action=”servlet/StarServlet” method=get>
Enter the value for “n”
<br>
<input type=text size=5 name=”n”>
<input type=submit value=”submit”>
</form>
</html>
Servlet Code
77
78 Chapter 9 Application Design and Development
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
9.13 Write a servlet and associated HTML code for the following simple appli-
cation: A user is allowed to submit a form containing a number, say n,
and should get a response saying how many times the value n has been
submitted previously. The number of times each value has been submitted
previously should be stored in a database.
Answer: HTML form
<html>
<head>
<title>DB Book Exercise 9.13 </title>
</head>
<form action="servlet/KeepCountServlet" method=get>
Enter the value for "n"
<br>
<input type=text size=5 name="n">
<input type=submit value="submit">
</form>
</html>
Schema
Servlet Code
9.14 Write a servlet that authenticates a user (based on user names and pass-
words stored in a database relation), and sets a session variable called userid
after authentication.
Answer: HTML form
<html>
<head>
<title>DB Book Exercise 9.14 </title>
</head>
<form action=“servlet/SimpleAuthServlet” method=get>
User Name:
<input type=text size=20 name=“user”>
<BR>
<BR>
Password :
<input type=password size=20 name=“passwd”>
<BR>
<input type=submit value=“submit”>
</form>
</html>
Schema
Servlet Code
Exercises 81
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<HEAD><TITLE>Exercise 9.14</TITLE></HEAD>”);
out.println(“<BODY>”);
out.println(message);
out.println(“</BODY>”);
out.close();
}
}
82 Chapter 9 Application Design and Development
9.15 What is an SQL injection attack? Explain how it works, and what precau-
tions must be taken to prevent SQL injection attacks.
Answer:
SQL injection attack occurs when a malicious user (attacker) manages to
get an application to execute an SQL query created by the attacker. If
an application constructs an SQL query string by concatenating the user
supplied parameters, the application is prone to SQL injection attacks.
For example, suppose an application constructs and executes a query to
retrieve a user’s password in the following way:
Connection getConnection() {
if(freeConnections.size() != 0){
conn = freeConnections.remove();
activeConnections.add(conn);
return conn;
}
activeConns = activeConnections.size();
if (activeConns == MAX POOL SIZE)
ERROR("Max pool size reached");
if (MAX POOL SIZE − activeConns > POOL SIZE INCREMENT)
connsToCreate = POOL SIZE INCREMENT;
else
connsToCreate = MAX POOL SIZE − activeConns;
releaseConnection(conn) {
activeConnections.remove(conn);
freeConnections.add(conn);
}
84 Chapter 9 Application Design and Development
closePool() {
if(activeConnections.size() != 0)
WARNING("Connections active. Will force close.");
for (i=0; i < freeConnections.size(); i++) {
conn = freeConnections.elementAt(i);
freeConnections.removeElementAt(i);
conn.close();
}
b. A Web site that supports autocompletion for text that you are typing
almost surely uses Ajax. For example, a search Web site that suggests
possible completions of your query as you type the query in, or a
Web-based email site that suggests possible completions of an email
address as you type in the address almost surely use Ajax to com-
municate with a server after you type in each character (sometimes
starting after the 3rd or 4th character), and respond with possible
completions.
c. A Web form that, on filling in one piece of data, such as your email
address or employee code, fills in other fields such as your name and
contact information, without refreshing the page, almost surely uses
Ajax to retrieve required information using the information (such as
the email address or employee code) provided by the user.
Popular Web sites that use Ajax include almost all current generation Web
email interfaces (such as GMail, Yahoo! mail, or Windows Live mail), and
almost all search engines, which provide autocompletion. Online docu-
ment management systems such as Google Docs or Office Live use Ajax
extensively to push your updates to the server, and to fetch concurrent
updates (to different parts of the document or spreadsheet) transparently.
Check your organizations Web applications to find more local examples.
9.19 XSS attacks:
Answer:
b. No. Someone who has the administrator privileges can disable the
trigger and thus bypass the trigger based audit trail.
9.24 Hackers may be able to fool you into believing that their Web site is actually
a Web site (such as a bank or credit card Web site) that you trust. This
may be done by misleading email, or even by breaking into the network
infrastructure and rerouting network traffic destined for, say mybank.com, to
the hacker’s site. If you enter your user name and password on the hacker’s
site, the site can record it, and use it later to break into your account at the
real site. When you use a URL such as https://mybank.com, the HTTPS protocol
is used to prevent such attacks. Explain how the protocol might use digital
certificates to verify authenticity of the site.
Answer: In the HTTPS protocol, a Web site first sends a digital certificate
to the user’s browser. The browser decrypts the digital certificate using
the stored public key of the trusted certification authority and displays
the site’s name from the decrypted message. The user can then verify if
the site name matches the one he/she intended to visit (in this example
mybank.com) and accept the certificate. The browser then uses the site’s
public key (that is part of the digital certificate) to encrypt user’s data. Note
that it is possible for a malicious user to gain access to the digital certificate
of mybank.com, but since the user’s data (such as password) is encrypted
using the public key of mybank.com, the malicious user will not be able to
decrypt the data.
Exercises 89