Although I personally favor using XML and XSLT to provide the view component in a model - view - controller architecture, using JSPs to provide that function is a reasonable alternative. When doing so, the Java code should be kept to a minimum.
The main technical issue in doing this is passing control from the servlet controller to the jsp view object. In the discussion below, a HashTable 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 ServletContext. The relevant classes and methods can be found in the javax.servlet package.
Background
Recall the use of XML to provide the view component in an m-v-c- arhitecture.
JSPs can be used in almost an identical way as to that shown in the example design and code.
JSP with Servlet Controller
Servlet
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletController extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { viewIB( req, resp, new InterestBean() ); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userAction = req.getParameter("userAction"); if ( userAction == null ) { // Error viewError( req, resp, "Null user action." ); } else if ( userAction.equalsIgnoreCase("Compute Results") ) { InterestBean bean = buildInterestBean( req ); viewIB( req, resp, bean ); } else if ( userAction.equalsIgnoreCase("Error") ) { // Error viewError( req, resp, "User action = Error." ); } else { // Error viewError( req, resp, "Unrecognized user action." ); } } private void viewIB(HttpServletRequest req, HttpServletResponse resp, InterestBean ib) throws ServletException, IOException { Hashtable interestValues = new Hashtable(); interestValues.put( "amount", ib.getAmount() ); interestValues.put( "rate", ib.getRate() ); interestValues.put( "years", ib.getYears() ); interestValues.put( "result", ib.getResult() ); RequestDispatcher disp; disp = getServletContext().getRequestDispatcher("/WEB-INF/classes/jsp/view.jsp"); req.setAttribute("interestValues", interestValues); disp.forward(req, resp); } private void viewError( HttpServletRequest req, HttpServletResponse resp, String e ) throws ServletException, IOException { String error = e; RequestDispatcher disp; disp = getServletContext().getRequestDispatcher("/WEB-INF/classes/jsp/error.jsp"); req.setAttribute("errorMessage", error); 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> <BODY> <% String message = (String) request.getAttribute("errorMessage"); %> <h3><%= message%></h3> </BODY> </HTML>View Data JSP
<html> <head> <title>Comp 117 view jsp</title> <% java.util.Hashtable interestValues = (java.util.Hashtable) request.getAttribute("interestValues"); String jspAmount = (String)interestValues.get("amount"); String jspRate = (String)interestValues.get("rate"); String jspYears = (String)interestValues.get("years"); String jspResult = (String)interestValues.get("result"); %> </head> <body> <h2 align="center"><font color="#aa0000">JSP View:<br> <br> <i>Interest Servlet</i></font></h2> <hr> <!--<form method="POST" action="http://wwwj.cs.unc.edu:8888/assignments/servlet/ServletInterest">--> <form method="POST" action="http://localhost:8888/jsp/servlet/ServletController"> <table border="0" width="40%"> <tr> <td width="50%" align="right">Amount: </td> <td width="50%"><input type="text" name="amount" value="<%= jspAmount%>" size="20"></td> </tr> <tr> <td width="50%" align="right">Rate: </td> <td width="50%"><input type="text" name="rate" value="<%= jspRate%>" size="20"></td> </tr> <tr> <td width="50%" align="right">Years: </td> <td width="50%"><input type="text" name="years" value="<%= jspYears%>" size="20"></td> </tr> <tr> <td width="50%" align="right">Result: </td> <td width="50%"><input type="text" name="result" value="<%= jspResult%>" size="20"></td> </tr> <tr> <td width="50%" align="right"></td> <td width="50%"><input type="submit" value="Compute Results" name="userAction"></td> </tr> <tr> <td width="50%" align="right"></td> <td width="50%"><input type="submit" value="Error" name="userAction"></td> </tr> </table> </form> </body> </html>
Run the Servlet