Interaction between JSP and EJB

Source: Internet
Author: User

The following is an snippet code that demonstrates the interaction between JSP and EJB session bean.

 
 
  1. <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,   
  2. foo.AccountHome, foo.Account" %>   
  3. <%!  
  4. //declare a "global" reference to an instance of the home 
    interface of the session bean   
  5. AccountHome accHome=null;  
  6. public void jspInit() ...{  
  7. //obtain an instance of the home interface   
  8. InitialContext cntxt = new InitialContext( );  
  9. Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");  
  10. accHome = (AccountHome)PortableRemoteObject.narrow
    (ref,AccountHome.class);  
  11. }  
  12. %> 
  13. <%  
  14. //instantiate the session bean   
  15. Account acct = accHome.create();  
  16. //invoke the remote methods   
  17. acct.doWhatever(...);  
  18. // etc etc...   
  19. %> 

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.

  1. Establishment of JSP development environment
  2. Brief Introduction to JSP environment Configuration
  3. JSP, ASP, and PHP security programming
  4. Integrate JSP and PHP in Apache
  5. Security issues of JSP and Servlet applications

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.