package c3.servlet;
import c3.xml.XMLWriter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
/**
* An abstract class from which each example servlet extends. This class wraps the XML creation
* (delegated to the child servlet class) and submission back through the HTTP response.
*
*/
public abstract class XMLServlet extends HttpServlet {
private String _encoding_output;
public XMLServlet() {
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
if((_encoding_output = getInitParameter("encoding.output")) == null)
_encoding_output = "utf-8";
}
protected long getLastModified(HttpServletRequest req) {
return -1L;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set content to xml
response.setContentType("text/xml; charset="+ _encoding_output);
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
try {
XMLWriter handler = new XMLWriter(out, _encoding_output);
handler.setPrettyPrinting(true);
getContent(handler);
out.flush();
out.close();
} catch (SAXException e) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* Each child class should override this method to generate the specific XML content necessary for each AJAX action.
*/
public abstract void getContent(ContentHandler handler) throws SAXException;
}