Java web development using hibernate, jsp and servlets, 2007

Page 118

Web Development with Java

106

address = “/ch3/dataBean/Confirm.jsp”; } else { address = “/ch3/dataBean/Edit.jsp”; }

In this example, if the location of the JSPs were changed, this would require modifying several lines of code. A more efficient solution is to encapsulate the path to the JSPs in a helper method. By adding a method to the bean that generates the location of the JSPs, it is easy to modify the application in the future if the JSPs are moved (Figure 4.5). The method has one parameter that is the file name of the next JSP. The method will append this name to the path of the JSPs. By adding the path in a separate method, it will be easier to move the JSPs in the future. protected String jspLocation(String page) { return “/ch3/dataBean/” + page; }

The path should not include the name of the web application. The controller cannot access paths outside the web application, so the name of the web application is assumed. The address of each JSP that is used in the bean must use this method to generate the address of the JSP. if (request.getParameter(“processButton”) != null) { address = jspLocation(“Process.jsp”); } else if (request.getParameter(“confirmButton”) != null) { address = jspLocation(“Confirm.jsp”); } else { address = jspLocation(“Edit.jsp)”; }

ControllerHelper RequestData data doGet() getData() copyFromSession () jspLocation ()

Figure 4.5 The jspLocation method is added to the controller helper.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.