... // name from the session String sessionName = session.getValue("name"); // action from the forms String action = request.getParameter("action"); // name from the login.jsp form String userName = request.getParameter("name"); // password from the login.jsp form String password = request.getParameter("password"); // comment from the add.jsp form String comment = request.getParameter("comment"); // login stores the user in the session if (action != null && action.equals("login") && userName != null && password != null && password.equals("quidditch")) { session.putValue("name", userName); } // adds a new guest if (action != null && action.equals("add") && sessionName != null && comment != null) { guestBook.addGuest(sessionName, comment); } String template; // if not logged in, use login.jsp if (session.getValue("name") == null) template = "login.jsp"; // if logged in, use add.jsp else template = "add.jsp"; RequestDispatcher disp; disp = app.getRequestDispatcher(template); ...

login.jsp and add.jsp just append different forms to the display code in the previous section. <%@ page language=javascript %> <head> <title>Hogwarts Guest Book: Login</title> </head> <body bgcolor='white'> <h1>Hogwarts Guest Book</h1> <table> <tr><td width='25%'><em>Name</em><td><em>Comment</em> <% var guestBook = application.attribute.guest_book for (var guest in guestBook) { out.writeln("<tr><td>" + guest.name + "<td>" + guest.comment); } %> </table> <hr> <form action='GuestJsp' method='post'> <input type=hidden name='action' value='login'> <table> <tr><td>Name:<td><input name='Name'> <tr><td>Password:<td><input name='Password' type='password'> <tr><td><input type=submit value='Login'> </table> </form> </body>

The Resin demo shows a few ways to extend the guest book, including adding some intelligence to the form processing. However, as forms get more intelligent, even JSP templates become complicated. There is a solution: XTP templates.
Copyright (c) 1998-2009 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
Copyright (c) 1998-2009 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.