XMLServlet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}