Linki |
» Dzieci to nie książeczki do kolorowania. Nie da siÄ™ wypeÅ‚nić ich naszymi ulubionymi kolorami. » 44 Przygotowanie do segmentacjilinie lotnicze dostarczaj¹ przyk³adu segmentacji zorientowanej wewnêtrznie (wed³ug obszarów sprzeda¿y), a nie zewnêtrznie (na... » 153z zadowoleniem, jakiego dostarcza odpowiednie oddzia³ywanie wychowawców, je¿eli natomiast chce siê dziecko oduczyæ niepo¿¹danych postêpków, to trzeba tak... » To get started, declare the two widgets you need, a label to hold the Hello World text and a button to exit the application:
private System... » cy, które dostarczaj¹ ok... » Rodzina typów caÅ‚kowitoliczbowych skÅ‚ada siÄ™ z czterech typów: q byte,q short,q int,q long... » int len = min(lv... » descent, and harm and death, be deriv'd solely from custom and
experience... » - Tak... » ( a, i + 2) is a losing move for Algois... » - Załóżmy jednak, że mógÅ‚ on pozostawić przesyÅ‚ki u wspólnika, nim jeszcze udaÅ‚ siÄ™ do szpitala?- Jest to maÅ‚o prawdopodobne, gdyż do pierwszego...
|
|
Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
|
toString(customerId);
}
public void setCustomerID(String id) {
customerId = Integer.parseInt(id);
}
376
Chapter 16: Introducing Enterprise JavaBeans
public void release() {
}
}
From this outline, you can see most of the standard features relating to custom tags. There's the constructor, the doStartTag() method (both of which require some code) and the methods for processing the customerID
attribute. Of interest here is how you process the attribute information. Notice that the code has elected to take the value from the JSP as a String and then process it internally. You may provide alternative classes, such as Integer in the TLD rather than the default String.
You are still missing the important part: interacting with the EJB. You should remember from the other examples the basic process that is needed — fetch the home interface and then access the bean information.
As usual, Java gives you plenty of options, but this is the one we recommend. Use the constructor to load the home interface of the Customer EJB. You would use the constructor rather than fetching the home interface in the doStartTag() method. The reason for this is for efficiency. The home interface is a common item, and you don't need to be fetching it every single time. So place that code in the constructor: public CustomerTag() {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("ejb/Customer");
home = (CustomerHome)PortableRemoteObject.narrow(
objref, CustomerHome.class);
} catch(Exception e) {
// Just print the error out
System.err.println("Error finding home interface" + e);
}
}
If you allocate items in the constructor, you should naturally follow it up with an implementation of the release() method so that you can free them.
public void release() {
home = null;
}
That leaves the doStartTag() method for you to fill in. You should probably be able to guess exactly what needs to be done now. In this method, you fetch the EJB corresponding to the provided customer ID and then write out some formatted text to the output like this:
public int doStartTag() {
JspWriter out = pageContext.getOut();
try {
CustomerID id = new CustomerID(customerId);
Customer cust = home.findByPrimaryKey(id);
out.write("Customer ID: ");
out.write(customerId);
out.write("<br>");
out.write("Name: ");
out.write(cust.getName());
out.write("<br>");
out.write("Address: ");
377
Chapter 16: Introducing Enterprise JavaBeans
out.write(cust.getAddress());
out.write("<br>");
out.write("Country: ");
out.write(cust.getCountry());
out.write("<br>");
out.write("Email: ");
out.write(cust.getEmail());
out.write("<br>");
out.write("Phone: ");
out.write(cust.getPhoneNumber());
out.write("<br>");
} catch(Exception e) {
out.write("There was an error processing the customer"); out.write(e.getMessage());
}
return SKIP_BODY;
}
That's all you need to do to integrate EJBs with custom tags in JSPs. As you can see from the two different examples, there is really not that much difference in difficulty between the approaches. Which you choose is more a matter of personal and/or company preferences.
Summary
During this chapter, you received an extensive introduction to the core of the J2EE specification — Enterprise JavaBeans. You could reasonably argue that the rest of the specifications in J2EE are there to support the requirements for EJBs. As you can see, EJBs are a huge topic, and so far we have barely scratched the surface of what you can do with them (stay tuned for more in the next chapter!). However, what we have covered so far is enough to get you up and running on most simple and medium−complexity projects. We gave you: An introduction to the concepts and terminology of EJBs.
•
Instructions for writing basic session and entity beans.
•
Instructions for integrating EJBs within other J2EE systems, such as servlets and JSPs.
•
378
|
|
|