The main technical issue in using a combination of servlet and JSP technologies to build the Controller and View components of a M - V - C design is passing control from the servlet controller to the jsp view object. In the discussion below, a DataBean is used to pass relevant data to the jsp. A RequestDispatcher, directed toward the jsp, is obtained from the ServletContext. It, in turn, is used to forward control to the jsp after the data to be displayed is passed through the HTTPServletRequest. The relevant classes and methods can be found in the javax.servlet and javax.servlet.http packages.
Servlet Controller
package jbs.interest; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class InterestController extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InterestBean bean = new InterestBean(); String name = "interestBean"; String url = "/interestView.jsp"; forward( req, resp, name, bean, url ); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userAction = req.getParameter("userAction"); if ( userAction == null ) { // Error String errorObject = "Null user action."; String name = "errorMessage"; String url = "/interestError.jsp"; forward( req, resp, name, errorObject, url ); } else if ( userAction.equalsIgnoreCase("Error") ) { // Error String errorObject = "User action = Error."; String name = "errorMessage"; String url = "/interestError.jsp"; forward( req, resp, name, errorObject, url ); } else if ( userAction.equalsIgnoreCase("Compute Results") ) { InterestBean bean = buildInterestBean( req ); String name = "interestBean"; String url = "/interestView.jsp"; forward( req, resp, name, bean, url ); } else { // Error String errorObject = "Unrecognized userAction."; String name = "errorMessage"; String url = "/interestError.jsp"; forward( req, resp, name, errorObject, url ); } } private void forward(HttpServletRequest req, HttpServletResponse resp, String name, Object object, String url ) throws ServletException, IOException { req.setAttribute( name, object ); RequestDispatcher disp = getServletContext().getRequestDispatcher( url ); disp.forward( req, resp ); } private InterestBean buildInterestBean( HttpServletRequest req ){ InterestBean bean = new InterestBean(); bean.setAmount ( req.getParameter("amount") ); bean.setRate ( req.getParameter("rate") ); bean.setYears ( req.getParameter("years") ); return bean; } }
View JSPs
View Error JSP
<HTML> <HEAD> <TITLE>error jsp</TITLE> </HEAD> <%! String message = (String) request.getAttribute("errorMessage"); %> <BODY> <h3><%= message%></h3> <A href=<"/jsp/servlet/jbs.interest.InterestController">Return to Application</A> </BODY> </HTML>View Interest JSP
<html> <head> <title>view jsp</title> </head> <jsp:useBean id="interestBean" scope="request" class="jbs.interest.InterestBean"/> <body> <h2 align=&"center"> <font color="#aa0000">JSP View:<br><br> <i>Interest Servlet</i> </font> </h2> <hr> <form method="POST" action="/jsp/servlet/jbs.interest.InterestController"> <table border="0" width="40%"> <tr> <td width="50%" align="right">Amount: </td> <td width="50%"><input type="text" name="amount" value="<jsp:getProperty name='interestBean' property='amount'/>" size="20"></td> </tr> <tr> <td width="50%" align="right">Rate: </td> <td width="50%"><input type="text" name="rate" value="<jsp:getProperty name='interestBean' property='rate'/>" size="20"></td> </tr> <tr> <td width="50%" align="right">Years: </td> <td width="50%"><input type="text" name="years" value="<jsp:getProperty name='interestBean' property='years'/>" size="20"></td> </tr> <tr> <td width="50%" align="right">Result: </td> <td width="50%"><input type="text" name="result" value="<jsp:getProperty name='interestBean' property='result'/>" size="20"></td> </tr> <tr> <td width="50%" align="right"></td> <td width="50%"><input type="submit" name="userAction" value="Compute Results"></td> </tr> <tr> <td width="50%" align="right"></td> <td width="50%"><input type="submit" name="userAction" value="Error"></td> </tr> </table> </form> </body> </html>
Run the Servlet