Web Application EJB Access Architecture - JSP

Throughout this chapter, all the EJB access has been carried out from scriptlets within JavaServer Pages. If you have read Topic,“JSP Application Architecture,” however, you will be aware that putting excessive scriptlets into JSPs is not necessarily good practice. You have seen it done in the book examples deliberately to keep the examples simple.

In practice, however, where should the EJB access code go? There are a number of possibilities, and the best places for the EJB access code are either in tag libraries or Beans. This would fit the MVC pattern, where the JavaBeans or tag libraries act as the model, the JSP is the view, and the servlet acts as the controller. Figure demonstrates the classic model 2 architecture, but with the JavaBeans interacting with the session Beans, and then making that data available to the client applications.

Web application/EJB interaction architecture.

Web application/EJB interaction architecture

The final set of code examples will demonstrate how this can be set up. First, Listing 15.18 is a controller servlet, which sets up the collection of locally available JavaBeans.

Listing 15.18 ControllerServlet.java

Within the init() method, the CompactDiscProduct Beans returned from the session Bean are now placed into an application-scoped attribute called stock.The doGet() method acts as the controller. In our basic example, it forwards to just one page, but obviously in a more complex environment, there would be additional entries here for search pages and such like. The controller forwards to a page called welcome.jsp, and that is shown next in Listing 15.19.

Listing 15.19 welcome.jsp

The welcome.jsp page is a very simple JSP because all the complexity is now hidden away from the page developer.Any additional JSPs can now either use the same stock application-scoped attribute, or they can go back to the session Bean and get up to date data.This is the one danger of using local Beans in this way—if the data at the back changes, your Beans will not reflect those changes.You should, therefore, consider carefully when you are going to use local Beans, and when you are going to go back to the EJB container to get up to date data.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics