import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; /** * This servlet modifies a specified XHTML document so that its ol * elements are collapsible. */ public class Hw7 extends HttpServlet { /** * Handle get request. It should contain 'targetDoc' parameter * which is the URL of the XHTML document to be modified. */ 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 and output initial portion of XHTML response PrintWriter servletOut = response.getWriter(); // Get the URL of the target document String targetDocURL = request.getParameter("targetDoc"); if (targetDocURL == null) { servletOut.println( " \n" + " \n" + "
\n" + "Error: targetDoc parameter missing.
\n" + " \n" + ""); } else { // Input the document as a DOM tree try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); Document document = parser.parse(targetDocURL); // Add script element Node head = document.getElementsByTagName("head").item(0); Element script = document.createElement("script"); head.appendChild(script); script.setAttribute("type", "text/javascript"); script.setAttribute("src", "../BlockCollapse.js"); // Add "Click to collapse" buttons before all ol // elements NodeList ols = document.getElementsByTagName("ol"); for (int i=0; i