KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > servlets > ServletPrinter


1 package edu.rice.rubis.beans.servlets;
2
3 import edu.rice.rubis.beans.*;
4 import java.io.PrintWriter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.FileNotFoundException JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10 import javax.rmi.PortableRemoteObject JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.net.URLEncoder JavaDoc;
17
18 /** In fact, this class is not a servlet itself but it provides
19  * output services to servlets to send back HTML files.
20  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
21  * @version 1.0
22  */

23
24 public class ServletPrinter
25 {
26   private PrintWriter JavaDoc out;
27   private String JavaDoc servletName;
28   private GregorianCalendar JavaDoc startDate;
29
30
31   /**
32    * Creates a new <code>ServletPrinter</code> instance.
33    *
34    * @param toWebServer a <code>HttpServletResponse</code> value
35    * @param callingServletName a <code>String</code> value
36    */

37   public ServletPrinter(HttpServletResponse toWebServer, String JavaDoc callingServletName)
38   {
39     startDate = new GregorianCalendar JavaDoc();
40     toWebServer.setContentType("text/html");
41     try
42     {
43       out = toWebServer.getWriter();
44     }
45     catch (IOException JavaDoc ioe)
46     {
47       ioe.printStackTrace();
48     }
49     servletName = callingServletName;
50   }
51
52   void printFile (String JavaDoc filename)
53   {
54     FileReader JavaDoc fis = null;
55     try
56     {
57       fis = new FileReader JavaDoc(filename);
58       char[] data = new char[4*1024]; // 4K buffer
59
int bytesRead;
60       bytesRead = fis.read(data);
61       while (/*(bytesRead = fis.read(data))*/ bytesRead != -1)
62       {
63         out.write(data, 0, bytesRead);
64     bytesRead = fis.read(data);
65       }
66     }
67     catch (Exception JavaDoc e)
68     {
69       out.println("Unable to read file (exception: "+e+")<br>\n");
70     }
71     finally
72     {
73       if (fis != null)
74         try
75         {
76           fis.close();
77         }
78       catch (Exception JavaDoc ex)
79       {
80         out.println("Unable to close the file reader (exception: "+ex+")<br>\n");
81       }
82     }
83   }
84
85   void printHTMLheader(String JavaDoc title)
86   {
87     printFile(Config.HTMLFilesPath+"/header.html");
88     out.println("<title>"+title+"</title>\n");
89   }
90
91   void printHTMLfooter()
92   {
93     GregorianCalendar JavaDoc endDate = new GregorianCalendar JavaDoc();
94
95     out.println("<br><hr>RUBiS (C) Rice University/INRIA<br><i>Page generated by "+servletName+" in "+TimeManagement.diffTime(startDate, endDate)+"</i><br>\n");
96     out.println("</body>\n");
97     out.println("</html>\n");
98   }
99
100   void printHTML(String JavaDoc msg)
101   {
102     out.println(msg);
103   }
104
105   void printHTMLHighlighted(String JavaDoc msg)
106   {
107     out.println("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n");
108     out.println("<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>"+msg+"</B></FONT></TD></TR>\n");
109     out.println("</TABLE><p>\n");
110   }
111
112   
113
114   void printItemHeader()
115   {
116     out.println("<TABLE border=\"1\" summary=\"List of items\">\n"+
117                 "<THEAD>\n"+
118                 "<TR><TH>Designation<TH>Price<TH>Bids<TH>End Date<TH>Bid Now\n"+
119                 "<TBODY>\n");
120   }
121
122
123   void printItemFooter(String JavaDoc URLprevious, String JavaDoc URLafter)
124   {
125     out.println("</TABLE>\n");
126     out.println("<p><CENTER>\n"+URLprevious+"\n&nbsp&nbsp&nbsp"+URLafter+"\n</CENTER>\n");
127   }
128
129   /**
130    * Item footer printed function
131    *
132    * @since 1.1
133    */

134   void printItemFooter()
135   {
136     out.println("</TABLE>\n");
137   }
138
139
140
141 }
142
Popular Tags