1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.sql.Connection ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.util.GregorianCalendar ; 34 35 import javax.servlet.http.HttpServletResponse ; 36 37 45 46 public class ServletPrinter 47 { 48 private PrintWriter out; 49 private String servletName; 50 private GregorianCalendar startDate; 51 52 public ServletPrinter(HttpServletResponse toWebServer, 53 String callingServletName) 54 { 55 startDate = new GregorianCalendar (); 56 toWebServer.setContentType("text/html"); 57 try 58 { 59 out = toWebServer.getWriter(); 60 } 61 catch (IOException ioe) 62 { 63 ioe.printStackTrace(); 64 } 65 servletName = callingServletName; 66 } 67 68 void printFile(String filename) 69 { 70 FileReader fis = null; 71 try 72 { 73 fis = new FileReader (filename); 74 char[] data = new char[4 * 1024]; int bytesRead; 76 bytesRead = fis.read(data); 77 while (bytesRead != -1) 78 { 79 out.write(data, 0, bytesRead); 80 bytesRead = fis.read(data); 81 } 82 } 83 catch (Exception e) 84 { 85 out.println("Unable to read file (exception: " + e + ")<br>"); 86 } 87 finally 88 { 89 if (fis != null) 90 try 91 { 92 fis.close(); 93 } 94 catch (Exception ex) 95 { 96 out.println("Unable to close the file reader (exception: " + ex 97 + ")<br>"); 98 } 99 } 100 } 101 102 void printHTMLheader(String title) 103 { 104 printFile(Config.HTMLFilesPath + "/header.html"); 105 out.println("<title>" + title + "</title>"); 106 } 107 108 void printHTMLfooter() 109 { 110 GregorianCalendar endDate = new GregorianCalendar (); 111 112 out 113 .println("<br><hr>RUBBoS (C) Rice University/INRIA<br><i>Page generated by " 114 + servletName 115 + " in " 116 + TimeManagement.diffTime(startDate, endDate) + "</i><br>"); 117 out.println("</body>"); 118 out.println("</html>"); 119 } 120 121 void printHTML(String msg) 122 { 123 out.println(msg); 124 } 125 126 void printHTMLHighlighted(String msg) 127 { 128 out.println("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">"); 129 out 130 .println("<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>" 131 + msg + "</B></FONT></TD></TR>"); 132 out.println("</TABLE><p>"); 133 } 134 135 public String authenticate(String nickname, String password, Connection conn) 136 { 137 try 138 { 139 PreparedStatement stmt = conn 140 .prepareStatement("SELECT id FROM users WHERE nickname=\"" + nickname 141 + "\" AND password=\"" + password + "\""); 142 ResultSet rs = stmt.executeQuery(); 143 rs.first(); 144 if (!rs.first()) 145 return "0"; return rs.getString("id"); 147 148 } 149 catch (Exception e) 150 { 151 return e + "Authenticate function error"; 152 153 } 154 } 155 156 public String getUserName(int UserId, Connection conn) throws Exception 157 { 158 try 159 { 160 PreparedStatement stmt = conn 161 .prepareStatement("SELECT nickname FROM users WHERE id=?"); 162 stmt.setInt(1, UserId); 163 ResultSet rs = stmt.executeQuery(); 164 rs.first(); 165 return rs.getString("nickname"); 166 } 167 catch (Exception e) 168 { 169 throw e; 170 } 171 172 } 173 174 } | Popular Tags |