// gb4, based on example from http://www.oreilly.com/catalog/msql/servlet.txt // Very loosely based, as it turns out. I couldn't find adequate documentation to make that work. // this is the generic (non-Jetty standalone) servlet version. // This version uses the getRemoteHost() to provide the remote host name as a default user name. // It also uses a cookie to store the user name. /* Here's how to build the database: mysqladmin create GUESTBOOK mysql < gb4 gb4 mysql_account ********* - provide your own mysql_password ********** - provide your own mysql_schema GUESTBOOK URI rowland/servlet/gb4 gb4 /servlet/gb4/* */ import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.sql.*; import java.text.DateFormat; import java.util.Properties; import java.util.Random; public class gb4 extends HttpServlet { static final String driverName = "com.mysql.jdbc.Driver"; static final String jdbcURL1 = "jdbc:mysql://127.0.0.1/"; private Driver driver = null; private Random random = new Random(); protected String databaseName; protected String userName; protected String URI; protected String userPassword; protected void openDriver() throws Exception { if (driver!= null) return; // already done println("Instantiating "+ driverName); driver = (Driver)Class.forName(driverName).newInstance(); if (driver== null) throw new SQLException("newInstance of "+ driverName+ "returned NULL!"); } protected Connection openConnection() throws Exception { // return null if failed to connect openDriver(); println(" Connecting "+ jdbcURL1+ databaseName); Connection conn; conn= DriverManager.getConnection(jdbcURL1+ databaseName, userName, userPassword); //[Not for Msql or MySql]conn.setAutoCommit(false); println(" ...connected"); return conn; } public void init(ServletConfig cfg) throws ServletException { // Called the first time a servlet is invoked super.init(cfg); databaseName= cfg.getInitParameter("mysql_schema"); userName= cfg.getInitParameter("mysql_account"); userPassword= cfg.getInitParameter("mysql_password"); URI= cfg.getInitParameter("URI"); println("init() called "+ databaseName+ " "+ userName); try { openDriver(); } catch( Exception e ) { throw new ServletException("EXC init(): " + e.getMessage()); } } protected String curDateString() { java.util.Date date= new java.util.Date(); return DateFormat.getDateTimeInstance().format(date); } protected static void println(String s) { java.util.Date d= new java.util.Date(); System.out.println(d.getTime()%10000+ ": "+ s); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { println("doGet called"); ServletOutputStream out = res.getOutputStream(); String name= null; Cookie[] cookies= req.getCookies(); if (cookies!= null) { int c= cookies.length; for (int i= 0; i"); out.println("
"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
Name:
Comments:
"); out.println("
"); out.println("
"); out.println(""); } private void printComments(ServletOutputStream out) throws IOException { Connection conn = null; println(" printComments()"); try { ResultSet results; Statement stmt; int rows, count; conn = openConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); results = stmt.executeQuery("SELECT name, date, " + "comment, comment_id " + "FROM comments " + "ORDER BY comment_id"); println(" ...SELECT FROM comments"); out.println("
"); results.last(); count = 0; // print up to 5 rows going backwards while (count < 5) { String name, cmt, date; name = results.getString(1); if( results.wasNull() ) name = "Unknown User"; date = results.getString(2); if( results.wasNull() ) date = curDateString(); cmt = results.getString(3); if( results.wasNull() ) cmt= "No comment"; out.println("
" + name + " on " + date); cmt = noHTML(cmt); out.println("
" + cmt + "
"); if (!results.previous()) break; // no more. count++; } out.println("
"); } catch( Exception e ) { out.println("Error: " + e.getMessage()); println("EXC printComments(): " + e.getMessage()); e.printStackTrace(); } finally { if( conn != null ) { try { conn.close(); } catch( SQLException e ) { } } } } private void printPageHeader(ServletOutputStream out) throws IOException { out.println(""); out.println(""); out.println("Guest Book"); out.println(""); out.println(""); out.println("

Guest Book

"); } private void printPageFooter(ServletOutputStream out) throws IOException { out.println(""); out.println(""); out.flush(); } private String noHTML(String cmt) { int ilt= cmt.indexOf('<'); int igt= cmt.indexOf('>'); if (ilt< 0 && igt< 0) return cmt; String tmp = ""; for (int i=0; i' ) tmp = tmp + ">"; else tmp = tmp + c; } return tmp; } private String fixComment(String comment) { if( comment.indexOf('\'') != -1 ) { String tmp = ""; for(int i=0; i