package cs320.servlet; import java.io.IOException; import java.io.PrintWriter; 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 = 2) public class GuestBook extends HttpServlet { private static final long serialVersionUID = 1L; public GuestBook() { super(); } public void init( ServletConfig config ) throws ServletException { super.init( config ); // create some test data for display List entries = new ArrayList(); entries.add( new GuestBookEntry( 1, "john", "hello" ) ); entries.add( new GuestBookEntry( 2, "joe", "hi" ) ); // stored the data somewhere that can be accessed by this servlet // and other servlets. getServletContext().setAttribute( "entries", entries ); } @SuppressWarnings("unchecked") protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // get the data List entries = (List) getServletContext().getAttribute( "entries" ); // display it response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "Guest Book" ); out.println( "" ); out.println( "" ); for( GuestBookEntry entry : entries ) { out.println( "" ); } out.println( "
IdNameMessage
" + entry.getId() + "" + entry.getName() + "" + entry.getMessage() + "Edit Delete
" ); out.println( "

Add Comment

" ); out.println( "

Add Comment (with Cookie)

" ); out.println( "

Add Comment (with Session)

" ); out.println( "" ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { doGet( request, response ); } }