The following is an snippet code that demonstrates the interaction between JSP and EJB session bean.
- <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,
- foo.AccountHome, foo.Account" %>
- <%!
- //declare a "global" reference to an instance of the home
interface of the session bean
- AccountHome accHome=null;
- public void jspInit() ...{
- //obtain an instance of the home interface
- InitialContext cntxt = new InitialContext( );
- Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");
- accHome = (AccountHome)PortableRemoteObject.narrow
(ref,AccountHome.class);
- }
- %>
- <%
- //instantiate the session bean
- Account acct = accHome.create();
- //invoke the remote methods
- acct.doWhatever(...);
- // etc etc...
- %>
In JSP, the less Java code, the better.
In the above example, the JSP designer has to process and understand the mechanism of accessing EJB. Instead of compressing the EJB mechanism in a Mediator and using the EJB method as the Mediator, you can use Mediator in JSP. Mediator is usually written by the EJB designer. Mediator can provide additional values, such as attribute caching.
The JSP scriptlet code must be minimized. To directly request ejbs in JSP, you may need to write a lot of code in JSP, including function blocks such as try... catch.
Using a standard JavaBean as a mediation between JSP and EJB server can reduce the number of Java code in JSP and improve reusability. This JavaBean must be a wrapper of the EJB you access ).
If you use standard JavaBean, you can use the JSP: useBean tag to initialize EJB parameters, such as server URL and Server
Security parameters.
Custom tags can also be an option. However, this requires more encoding than a simple JavaBean wrapper.
The code must be rewritten as little as possible and the JSP script content should be as light as possible ). The preceding figure shows the interaction between JSP and EJB.
- Establishment of JSP development environment
- Brief Introduction to JSP environment Configuration
- JSP, ASP, and PHP security programming
- Integrate JSP and PHP in Apache
- Security issues of JSP and Servlet applications