Home | History | Annotate | Download | only in servlet
      1 /* $Id: CSPage.java,v 1.1 2002/09/20 23:14:10 jeske Exp $
      2  *
      3  */
      4 
      5 import java.io.*;
      6 import java.text.*;
      7 import java.util.*;
      8 import javax.servlet.*;
      9 import javax.servlet.http.*;
     10 
     11 import java.lang.String.*;
     12 
     13 import org.clearsilver.*;
     14 
     15 /**
     16  * The simplest possible servlet.
     17  *
     18  */
     19 
     20 public class CSPage extends HttpServlet {
     21     public HDF hdf;
     22     public CS cs;
     23     public boolean page_debug = true;
     24 
     25     public void doGet(HttpServletRequest request,
     26                       HttpServletResponse response)
     27         throws IOException, ServletException
     28     {
     29 
     30 
     31         PrintWriter out = response.getWriter();
     32 	hdf = new HDF();
     33 	cs = new CS(hdf);
     34 
     35 	// HTTP headers
     36         Enumeration e = request.getHeaderNames();
     37         while (e.hasMoreElements()) {
     38             String headerName = (String)e.nextElement();
     39             String headerValue = request.getHeader(headerName);
     40 	    hdf.setValue("HTTP." + headerName,headerValue);
     41         }
     42 
     43 
     44 	hdf.setValue("HTTP.PATH_INFO",request.getPathInfo());
     45 	hdf.setValue("CGI.QueryString",request.getQueryString());
     46 	hdf.setValue("CGI.RequestMethod",request.getMethod());
     47 
     48 	// Querystring paramaters
     49 	e = request.getParameterNames();
     50 	while (e.hasMoreElements()) {
     51 	    String paramName = (String)e.nextElement();
     52 	    String paramValue = request.getParameter(paramName);
     53 	    hdf.setValue("Query." + paramName,paramValue);
     54 	}
     55 
     56 
     57 	// Cookies
     58 
     59         Cookie[] cookies = request.getCookies();
     60         if (cookies.length > 0) {
     61             for (int i = 0; i < cookies.length; i++) {
     62                 Cookie cookie = cookies[i];
     63 		hdf.setValue("Cookie." + cookie.getName(),cookie.getValue());
     64             }
     65         }
     66 
     67 	// CGI example
     68 	// check for Actions
     69 
     70 
     71 	// then call display method
     72 	this.display();
     73 
     74 	// run required page template through CS
     75 	// cs.parseFile(a_template_file);
     76 
     77 	// Page Output
     78 
     79 
     80 	/* first do cookies
     81 
     82         String cookieName = request.getParameter("cookiename");
     83         String cookieValue = request.getParameter("cookievalue");
     84         if (cookieName != null && cookieValue != null) {
     85             Cookie cookie = new Cookie(cookieName, cookieValue);
     86             response.addCookie(cookie);
     87             out.println("<P>");
     88             out.println(rb.getString("cookies.set") + "<br>");
     89             out.print(rb.getString("cookies.name") + "  " + cookieName +
     90 		      "<br>");
     91             out.print(rb.getString("cookies.value") + "  " + cookieValue);
     92         }
     93 
     94 	*/
     95 
     96         response.setContentType("text/html");
     97 	out.print(cs.render());
     98 
     99 
    100 	// debug
    101 	if (page_debug) {
    102 	  out.print("<HR><PRE>");
    103 	  out.print(hdf.dump());
    104 	  out.print("</PRE>");
    105 	}
    106 
    107     }
    108 
    109     public void doPost(HttpServletRequest request,
    110                       HttpServletResponse response) throws IOException, ServletException {
    111         doGet(request, response);
    112     }
    113 
    114     public void display() {
    115 	hdf.setValue("Foo.Bar","1");
    116 	cs.parseStr("Hello Clearsilver<p><TABLE BORDER=1><TR><TD>Foo.Bar</TD><TD><?cs var:Foo.Bar ?></TD></TR></TABLE>");
    117     }
    118 
    119 
    120 }
    121 
    122 
    123 
    124