01: package gnu.kawa.servlet;
02:
03: import gnu.text.*;
04: import gnu.mapping.*;
05: import gnu.expr.*;
06:
07: import java.io.IOException;
08: import java.io.PrintWriter;
09:
10: import javax.servlet.*;
11: import javax.servlet.http.*;
12:
13: /** The Servlet for a browser-base "read-eval-print-loop" session.
14: * The session state and logic is in ReplSession. */
15:
16: public class ReplServlet extends HttpServlet {
17: public void init(ServletConfig config) throws ServletException {
18: }
19:
20: public void doPost(HttpServletRequest request,
21: HttpServletResponse response) throws java.io.IOException {
22: String command = request.getParameter("command");
23: HttpSession hsession = request.getSession(true);
24: ReplSession rsession = (ReplSession) hsession.getValue("repl");
25: if (rsession == null) {
26: rsession = new ReplSession();
27: hsession.putValue("repl", rsession);
28: }
29:
30: PrintWriter out = response.getWriter();
31: String requestURI = request.getRequestURI();
32: StringBuffer resultBuffer = new StringBuffer();
33: resultBuffer.append("<?xml version=\"1.0\"?>\n");
34: resultBuffer
35: .append("<result xmlns=\"http://www.w3.org/1999/xhtml\">");
36: java.io.BufferedReader in = request.getReader();
37: StringBuffer sbuf = new StringBuffer();
38: for (;;) {
39: int ch = in.read();
40: if (ch < 0)
41: break;
42: sbuf.append((char) ch);
43: }
44: String inputData = sbuf.toString();
45: if ("line".equals(command))
46: rsession.appendInputLine(inputData);
47: else if ("action".equals(command))
48: rsession.appendInput(inputData);
49: String result = rsession.waitOutput();
50: resultBuffer.append(result);
51: resultBuffer.append("</result>");
52:
53: response.setContentType("text/xml");
54: response.setHeader("Pragma", "no-cache");
55: response.setHeader("Expires", "0");
56: response.setHeader("Cache-Control", "no-store");
57:
58: out.print(resultBuffer.toString());
59: out.close();
60: }
61: }
|