0% found this document useful (0 votes)
9 views4 pages

Book Catalog Search and Display

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Book Catalog Search and Display

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Book Catalog</title>
</head>
<body>
<h2>Book Catalog</h2>
<form action="[Link] method="get">
<button type="submit">View Book Catalog</button>
</form>
<form action="[Link] method="get">
<label for="bname">Enter the Book title to search:</label>
<input type="text" id="bname" name="bname" placeholder="Book title">
<button type="submit">Search</button>
</form>
</body>
</html>

BookCatalogServlet:

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

@WebServlet("/BookCatalogServlet")
public class BookCatalogServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Book Catalog</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h2>Book Catalog</h2>");

Connection connection = null;


Statement statement = null;
ResultSet rs = null;

try {
[Link]("[Link]").newInstance();
connection = [Link]("jdbc:derby://localhost:1527/six", "root", "root");
statement = [Link]();
String queryString = "SELECT * FROM BOOKDB";
rs = [Link](queryString);

[Link]("<table cellpadding=\"15\" border=\"1\" style=\"background-color: #ffffcc;\">");


while ([Link]()) {
[Link]("<tr>");
[Link]("<td>" + [Link](1) + "</td>");
[Link]("<td>" + [Link](2) + "</td>");
[Link]("<td>" + [Link](3) + "</td>");
[Link]("<td>" + [Link](4) + "</td>");
[Link]("<td>" + [Link](5) + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
} catch (Exception ex) {
[Link]("<font size=\"+3\" color=\"red\"><b>Unable to connect to
database.</b></font>");
[Link]();
} finally {
try {
if (rs != null) [Link]();
if (statement != null) [Link]();
if (connection != null) [Link]();
} catch (SQLException e) {
[Link]();
}
}

[Link]("<table>");
[Link]("<tr>");
[Link]("<td><form action=\"[Link]\" method=\"get\">");
[Link]("<button type=\"submit\"><-- back</button></form></td>");
[Link]("</tr>");
[Link]("</table>");
[Link]("</body>");
[Link]("</html>");
}
}

BookCatalogServlet1:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/BookCatalogServlet1")
public class BookCatalogServlet1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Book Catalog</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h2>Book Catalog</h2>");

Connection connection = null;


Statement statement = null;
ResultSet rs = null;

try {
String connectionURL = "jdbc:derby://localhost:1527/six";
[Link]("[Link]").newInstance();
connection = [Link](connectionURL, "root", "root");
statement = [Link]();
String ss = [Link]("bname");
String queryString = "SELECT * FROM bookdb WHERE bname LIKE '" + ss + "%'";
rs = [Link](queryString);

[Link]("<table cellpadding=\"15\" border=\"1\" style=\"background-color: #ffffcc;\">");


while ([Link]()) {
[Link]("<tr>");
[Link]("<td>" + [Link](1) + "</td>");
[Link]("<td>" + [Link](2) + "</td>");
[Link]("<td>" + [Link](3) + "</td>");
[Link]("<td>" + [Link](4) + "</td>");
[Link]("<td>" + [Link](5) + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
} catch (Exception ex) {
[Link]("<font size=\"+3\" color=\"red\"><b>Unable to connect to
database.</b></font>");
[Link]();
} finally {
try {
if (rs != null) [Link]();
if (statement != null) [Link]();
if (connection != null) [Link]();
} catch (SQLException e) {
[Link]();
}
}

[Link]("<table>");
[Link]("<tr>");
[Link]("<td><form action=\"[Link]\" method=\"get\">");
[Link]("<button type=\"submit\"><-- back</button></form></td>");
[Link]("</tr>");
[Link]("</table>");
[Link]("</body>");
[Link]("</html>");
}
}

Common questions

Powered by AI

The search functionality allows users to input a book title, which is sent as a parameter in a GET request and used directly in an SQL query. To enhance usability, autocomplete or suggestions could be implemented to improve the user experience. For security, input validation and encoding techniques should be applied to prevent SQL injection attacks. Moreover, using POST method instead of GET could also be more secure for transmitting input in some cases .

The servlet annotations, such as `@WebServlet("/BookCatalogServlet")` and `@WebServlet("/BookCatalogServlet1")`, define the URL patterns that map HTTP requests to these specific servlet classes. This simplifies configuration by eliminating the need for explicit servlet mappings in the web.xml deployment descriptor. It streamlines deployment but requires careful naming and pattern management to avoid conflicts and ensure that URL routing matches the application's navigation logic .

Both servlets handle database connections in a similar way using JDBC but differ in their querying processes. BookCatalogServlet retrieves all records from the BOOKDB table using a query string "SELECT * FROM BOOKDB". In contrast, BookCatalogServlet1 allows users to search for books by title through a parameter from the request and uses it in the SQL query string; it employs a query like "SELECT * FROM bookdb WHERE bname LIKE '" + ss + "%'" which includes a user-input based wildcard search .

A refactor could involve implementing a connection pool to manage database connections more efficiently. This would involve creating a connection manager class responsible for providing and releasing connections, reducing the overhead of repeatedly creating and closing connections. Additionally, principle of Single Responsibility can be applied by separating the database access logic from the servlets, potentially through a DAO (Data Access Object) pattern, improving maintainability and testability .

The HTML table used in both servlets follows a simple design using border attributes, padding, and a background color for visual distinction. The table is effective for displaying tabular data in an organized manner. However, its limitations include a lack of sophisticated styling or responsiveness and no sorting or pagination features, which could affect usability, especially when displaying large datasets .

The SQL query in BookCatalogServlet1 is vulnerable to SQL injection because it directly concatenates user input into the SQL statement without validation or parameterization. This can be exploited by injecting malicious SQL code into the input field. To mitigate these risks, prepared statements with parameterized queries should be used, which separate SQL code from data, effectively neutralizing the input .

Using `org.apache.derby.jdbc.ClientDriver` with a specified connection URL like `jdbc:derby://localhost:1527/six` ties the application to a Derby database running on a local server, which could be limiting or problematic in production environments that require scalability or distributed systems. For deployment, implications include needing to set up and possibly migrate to another relational database management system that supports broader concurrency or distribution, and ensuring the JDBC driver used is available on the production server .

Both servlets employ similar error handling strategies, encapsulating database operations within a try-catch block to catch exceptions such as IOExceptions or SQLExceptions. If an exception occurs, an error message is displayed in the HTML response indicating that the connection to the database failed. Additionally, both servlets close the ResultSet, Statement, and Connection objects in a finally block to ensure resource cleanup, regardless of whether an error occurred or not .

The HTML form interfacing with BookCatalogServlet simply contains a button labeled "View Book Catalog" which, when clicked, sends a GET request to retrieve and display the entire catalog. Meanwhile, the form for BookCatalogServlet1 requires user input to search for a specific book title, enhancing interactive capability by allowing users to query specific data. This impacts user interaction by allowing for more targeted queries in BookCatalogServlet1, as opposed to a full catalog display in BookCatalogServlet .

The current design of the servlets, which instantiate a new database connection per request, may lead to concurrency issues under heavy load, such as exhaustively opening connections if many requests are processed simultaneously. This can degrade performance or lead to connection pool exhaustion. To resolve these issues, a connection pooling library like Apache DBCP or HikariCP could be used to manage and reuse connections, reducing the overhead and improving resource utilization and concurrency .

You might also like