import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
/**
* Servlet which displays a page of a text. It sends a query to the
* database to get a particular sequence of words, then
* formats them in HTML
*/
public class Reader extends LitsearchBase {
public String drawPage(HttpServletRequest request,
HttpServletResponse response) throws Exception {
int work, page;
Hashtable highlight = null;
ConnectionWrapper wrapper = null;
try {
work = Integer.parseInt(request.getParameter("work"));
page = Integer.parseInt(request.getParameter("page"));
String hi = request.getParameter("highlight");
int end, start = 0;
if (hi != null) {
while ((end = hi.indexOf(",", start)) != -1) {
if (highlight == null) highlight = new Hashtable();
highlight.put(hi.substring(start, end), "");
start = end + 1;
}
if (start < hi.length() - 1) highlight.put(hi.substring(start), "");
}
} catch (Exception e) {
return drawError("Invalid input",
"Your request for this particular work and " +
"page was not properly formatted.");
}
try {
wrapper = DatabaseHook.getConnection();
Connection c = wrapper.connection();
Statement stmt = c.createStatement();
StringBuffer text = new StringBuffer();
String linkAhead = "";
String title = "";
ResultSet rs = stmt.executeQuery
("SELECT Work.title, WrittenBy.name " +
"FROM Work, WrittenBy " +
"WHERE WrittenBy.work = Work.id AND Work.id = " + work);
if (rs.next()) {
title = "" + rs.getString(1) +
"
by " + rs.getString(2) + "
";
}
rs = stmt.executeQuery
("SELECT MAX(page) FROM UniquePage WHERE work=" + work);
if (rs.next()) {
int lastPage = rs.getInt(1);
linkAhead = "
"; linkAhead += "\npage " + page + " of " + lastPage + " | "; if (page > 1) linkAhead += "prev. page | "; if (page < lastPage) linkAhead += "next page"; linkAhead += " |