package cs320.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/AddComment") public class AddComment extends HttpServlet { private static final long serialVersionUID = 1L; public AddComment() { super(); } protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { request.getRequestDispatcher( "/WEB-INF/AddComment.jsp" ).forward( request, response ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // get the user input String name = request.getParameter( "name" ); String message = request.getParameter( "message" ); Connection c = null; try { String url = "jdbc:mysql://localhost/cs320stu31"; String username = "cs320stu31"; String password = "abcd"; String sql = "insert into guestbook (name, message) values (?, ?)"; c = DriverManager.getConnection( url, username, password ); PreparedStatement pstmt = c.prepareStatement( sql ); pstmt.setString( 1, name ); pstmt.setString( 2, message ); pstmt.executeUpdate(); } catch( SQLException e ) { throw new ServletException( e ); } finally { try { if( c != null ) c.close(); } catch( SQLException e ) { throw new ServletException( e ); } } // send the user back to the guest book page response.sendRedirect( "GuestBook" ); } }