import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
/**
* Add a table of contents to an XHTML document at a specified URL.
*/
public class TOCGen extends HttpServlet
{
/**
* Respond to any HTTP GET request with a
* form that requests the entry of a URL.
*/
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set the HTTP content type in response header
response.setContentType("text/html; charset=\"UTF-8\"");
// Obtain a PrintWriter object for creating the body
// of the response
PrintWriter servletOut = response.getWriter();
// Create the body of the response
servletOut.println(
" \n" +
" \n" +
"
\n" +
" \n" +
" TOC Generator \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
servletOut.close();
}
/** Add a TOC to the document at the specified URL and
* display it.
*/
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set the HTTP content type in response header
response.setContentType("text/html; charset=\"UTF-8\"");
// Obtain a PrintWriter object for creating the body
// of the response
PrintWriter servletOut = response.getWriter();
// Get URL from request
String aURL = request.getParameter("aURL");
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(new StreamSource("http://localhost:8080/toc.xsl"));
transformer.transform(
new StreamSource(aURL),
new StreamResult(servletOut));
}
catch (Exception e) {
e.printStackTrace();
}
servletOut.close();
}
}