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.
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.
JSP Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||Java Servlets Interview Questions|
|Hibernate Interview Questions||JavaServer Faces (JSF) Interview Questions|
|JSTL(JSP Standard Tag Library) Interview Questions||JBOSS Interview Questions|
|Log4j Interview Questions||NHibernate Interview Questions|
|Apache Struts 2 Interview Questions|
Jsp, J2ee, And The Role Of The Servlet
The Basic Syntax Of Jsp
Further Jsp Syntax
Deploying Jsp In A Web Container
The Jsp Standard Tag Library
Jsp And Javabeans
Jsp Application Architecture
Session Tracking Using Jsp
Developing Custom Tag Libraries
Utilizing Xml From Jsp
Transforming Xml Using Xslt And Xslfo
Invoking Web Services From Jsp
Locating Resources Using Jndi
Databases And Jsp
Jsp And Ejb Interaction
Security And Jsp
Personalization And Web Applications
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.