package cs320.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cs320.model.GuestBookEntry; @WebServlet(urlPatterns = "/GuestBook", loadOnStartup = 1) public class GuestBook extends HttpServlet { private static final long serialVersionUID = 1L; public GuestBook() { super(); } public void init( ServletConfig config ) throws ServletException { super.init( config ); try { Class.forName( "com.mysql.jdbc.Driver" ); } catch( ClassNotFoundException e ) { throw new ServletException( e ); } } protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { List entries = new ArrayList(); Connection c = null; try { String url = "jdbc:mysql://localhost/cs320stu31"; String username = "cs320stu31"; String password = "abcd"; c = DriverManager.getConnection( url, username, password ); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery( "select * from guestbook" ); while( rs.next() ) { GuestBookEntry entry = new GuestBookEntry( rs.getInt( "id" ), rs.getString( "name" ), rs.getString( "message" ) ); entries.add( entry ); } } catch( SQLException e ) { throw new ServletException( e ); } finally { try { if( c != null ) c.close(); } catch( SQLException e ) { throw new ServletException( e ); } } request.setAttribute( "entries", entries ); request.getRequestDispatcher( "/WEB-INF/GuestBook.jsp" ).forward( request, response ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { doGet( request, response ); } }