// gb5, 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 adds email notification, using JavaMail. // By Rowland http://home.comcast.net/~rowland3/ /* Here's how to build the database: mysqladmin create GUESTBOOK mysql < gb5 gb5 gbname Rowland mysql_account ********* - provide your own mysql_password ********** - provide your own mysql_schema GUESTBOOK URI rowland/servlet/gb5 mailhost *********** - provide your own mailuser *********** - provide your own email_notify ********* - provide your own gb5 /servlet/gb5/* */ import javax.mail.*; // Session in mail.jar from JavaMail import javax.mail.internet.*; // MimeMessage in mail.jar from JavaMail import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.net.*; // InetAddress import java.sql.*; import java.text.DateFormat; import java.util.Properties; import java.util.Random; /** MailNotifier - a utility class to send a SMTP mail notification **/ class MailNotifier { final String localhost; final String mailhost; final String mailuser; final String email_notify; protected Session session= null; MailNotifier(String _localhost, String _mailhost, String _mailuser, String _email_notify) { localhost= _localhost; mailhost= _mailhost; mailuser= _mailuser; email_notify= _email_notify; } public void send(String subject, String text) throws Exception { if (session== null) { Properties p = new Properties(); p.put("mail.host", mailhost); p.put("mail.user", mailuser); session = Session.getDefaultInstance(p, null); // Try to fake out SMTPTransport.java and get working EHLO: Properties properties = session.getProperties(); String key= "mail.smtp.localhost"; String prop= properties.getProperty(key); if (prop== null) properties.put(key, localhost); else System.out.println(key+ ": "+ prop); //session.setDebug(true); } MimeMessage msg = new MimeMessage(session); msg.setText(text); msg.setSubject(subject); Address fromAddr = new InternetAddress(mailuser); msg.setFrom(fromAddr); Address toAddr = new InternetAddress(email_notify); msg.addRecipient(Message.RecipientType.TO, toAddr); Transport.send(msg); // Note: will use results of getLocalHost() to fill in EHLO domain } /** * Get the name of the local host, for use in the EHLO and HELO commands. * The property mail.smtp.localhost overrides what InetAddress would tell * us. Cribbed from SMTPTransport.java */ public String getLocalHost() { String localHostName= null; String name = "smtp"; // Name of this protocol try { // get our hostname and cache it for future use if (localHostName == null || localHostName.length() <= 0) localHostName = session.getProperty("mail." + name + ".localhost"); if (localHostName == null || localHostName.length() <= 0) localHostName = InetAddress.getLocalHost().getHostName(); } catch (Exception uhex) { } return localHostName; } } public class gb5 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 String mailhost; protected String mailuser; protected String email_notify; protected ServletConfig cfg; protected String gbname; protected String localName; // name of this host, from request protected MailNotifier notifier= null; 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); cfg= _cfg; println("init() called "+ databaseName+ " "+ userName); gbname= cfg.getInitParameter("gbname"); databaseName= cfg.getInitParameter("mysql_schema"); userName= cfg.getInitParameter("mysql_account"); userPassword= cfg.getInitParameter("mysql_password"); URI= cfg.getInitParameter("URI"); mailhost= cfg.getInitParameter("mailhost"); mailuser= cfg.getInitParameter("mailuser"); email_notify= cfg.getInitParameter("email_notify"); 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); } /** println - print log entry to standard output. Under Tomcat, will end up in logs/catalina.out **/ 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"); localName= req.getLocalName(); 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 10 rows going backwards while (count < 10) { 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(""+ gbname+ " Guest Book"); out.println(""); out.println(""); out.println("

"+ gbname+ " 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