The following is a snippet code that demonstrates how JSP pages interact with the EJB session Bean.
<%@ page import= "javax.naming.*, Javax.rmi.PortableRemoteObject,
Foo. Accounthome, Foo. Account "%>
<%!
Declare a "global" reference to a 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 ...
%>
The less Java code should be in the JSP, the better.
In the above example, the JSP designer has to deal with and understand the mechanism of accessing the EJB. Instead of compressing the EJB mechanism in a mediator and taking the EJB method as a mediator method,
You can use mediator in your JSP. Mediator is usually written by the EJB designer. Mediator can provide additional values such as attribute caching.
*****
JSP Scriptlet code must be minimized. If you want to request an EJB directly in the JSP, you might want to write a lot of code in the JSP, including functions such as Try...catch.
Using a standard JavaBean as a mediator of a JSP and EJB server can reduce the number of Java code in the JSP and increase reusability. This javabean must be an overlay of the EJB you are accessing (wrapper).
If you use the standard JavaBean, you can use the Jsp:usebean tag to initialize the EJB parameters, such as server URL and server
Security parameters, and so on.
Custom tags can also be an option. However, this requires more coding than a simple JavaBean wrapper.
The place must be light as small as possible and ensure that the JSP script content is as light as possible.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.