import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
/**
* This is the abstract class that all of the servlets subclass off of.
* All it does is take care of the repetitive IO stuff, wraps the
* entire page in the standard header and footer, and provides some
* convenience methods for handling errors and formatting strings.
*
* All subclasses must implement the drawPage method to draw themselves.
*/
public abstract class LitsearchBase extends HttpServlet {
public static final String ROOT_DIR = "/web/concordia/WEB-INF/";
public static final String TEMPLATE_DIR = ROOT_DIR + "templates/";
public static final String IMAGE_DIR = "/litsearch/images/";
public static final String MAIN_TEMPLATE = TEMPLATE_DIR + "template.html";
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
PrintWriter out;
response.setContentType("text/html");
try {
out = response.getWriter();
} catch (IOException e) { return; }
try {
Template template = new Template(MAIN_TEMPLATE);
String content = drawPage(request, response);
template.substitute("CONTENT FRAME", content);
out.println(template);
} catch (Exception e) {
handleException(e, out);
}
}
public abstract String drawPage(HttpServletRequest request,
HttpServletResponse response) throws Exception;
protected void handleException(Exception e, PrintWriter out) {
e.printStackTrace(out);
}
protected String drawError(String title, String message) {
return "" + title + "
\n" +
"We're sorry. The following error occurred:
\n" +
message;
}
protected String formatString(String s) {
if (s == null) return "null";
StringBuffer buf = new StringBuffer(s);
for (int i = 0; i < buf.length(); i++) {
if (buf.charAt(i) == '\'') {
buf.insert(i, '\'');
i++;
}
}
buf.insert(0, '\'');
buf.append('\'');
return buf.toString();
}
}