1 lecture 16 george koutsogiannakis/summer 2011 cs441 current topics in programming languages

62
1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Upload: rosamond-floyd

Post on 30-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

1

Lecture 16

George Koutsogiannakis/SUMMER 2011

CS441

CURRENT TOPICS IN PROGRAMMING LANGUAGES

Page 2: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Topics

• Running an Applet in NetBeans

• Implementing the LoginAppletNORMI example.

• A session EJB client with annotations.– Annotations.

– Deployment Descriptors.

• A session EJB client that does lookup– Lookup when the application has a single remote interface

– Lookup when multiple interfaces exist in the application.

• Rules for Multiple Interfaces.

2

Page 3: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Running an Applet in NetBeans

• Let us create a project for the applet class which is part of the LoginAppletNORMI web application example posted on the course’ s web site.– Create a project called Applet and browse to

where the LoginApplet.java file is located to be included in the project.

3

Page 4: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Running an Applet in NetBeans

• The IDE will automatically generate the LoginApplet.html file needed to launch the applet.

• To exclude the LoginApplet.html launcher file from the JAR file, right-click the project, choosing Properties, clicking Packaging, and adding an expression to exclude them.

4

Page 5: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Running an Applet in NetBeans

• An applet can be tested by using the appletviewer from NetBeans.

• To run the applet with the appletviewer:

– Select the applet class file and right click.

– Choose Run File

– The file LoginApplet.html is created in the build folder and the appletviewer is launched.

– The LoginApplet screen appears.

– Notice that the exit button works. That is because a default policy file is created for the applet that gives it AllPermissions.

5

Page 6: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Running an Applet in NetBeans

• Notice that the LoginApplet.html was created automatically when you Run the applet.

• You can view the files generated if you switch from the Projects window to Files Window.

– In the Files window you can also verify that the .jar file that contains the applet was generated under the dist folder

– The next slide shows the Files window with the Applet project expanded.

• You can edit the source code for the applet LoginApplet.java to make sure that the url contains the correct port for the server that you are going to use for the web application (either Tomcat or GlassFish).

6

Page 7: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

7

Page 8: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

8

Result of testing LoginApplet example with “Run File” command.

Page 9: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Registering A Browser with NetBeans

• By default, the IDE opens your system's default web browser. If necessary, you can specify a different browser in the Options window.

• To launch the web browser. • Choose View > Web Browser from the main

menu.

9

Page 10: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Setting Permissions for an Applet

• By default, applets do not have access to resources on a client's computer, such as threads and disk operations.

• An applet's permissions are defined in its applet.policy file.

• If you have an applet policy file, you can specify its location in the IDE. Then, when you run the application, the IDE uses the applet policy file that you specified.

• If you do not specify an applet policy file, the IDE generates one for you when you run the applet.

• The applet policy file that the IDE generates for you grants all permissions to the applet. You can use the Source Editor to modify the policy file, so that appropriate security checks are done.

10

Page 11: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Setting Permissions for an Applet

• You can view the policy file in the Files window by expanding the nbproject node.

• The default policy file has the name of the project i.e Applet.policy in our example.

grant {

permission java.security.AllPermission;

};

• You can edit the policy file to change the permissions to your desired permissions.

11

Page 12: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

• If you create an applet as part of a Java WebStart-enabled project, you can run the applet by running the project.

• If your applet is not part of a WebStart-enabled project, the applet is not run as part of the project.

• To complete the LoginApplet example we need to:– Create a web application that includes the servlet

TestLoginServlet.java and

– Bring the Applet project that we created into the web application.

– We can then deploy the web application into either Tomcat or GlassFish as a web application.

12

Page 13: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating a Web Application Project

• In the IDE, you create applications and modules in projects. The web application project is the project that you use to create web applications and web application modules (servlets and jsp source code files).

• To create a web application project: Choose File > New Project (Ctrl-Shift-N).

• From the Java Web category, select one of the following project templates:

13

Page 14: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

– Creating the Web Application in a standard project or– Web Application with Existing Sources. Imports an existing web

application into a standard project.• standard project

• A project that uses an IDE-generated Ant script to compile, run, and debug your application. The IDE comes with the following standard project templates:

– Java SE: Java Application, Java Class Library, Java Project with Existing Sources, Java Desktop Application

– Web: Web Application, Web Application with Existing Sources – Java EE: Enterprise Application, Enterprise Application with Existing

Sources – Enterprise JavaBeans Modules: EJB Module, EJB Module with Existing

Sources

14

Page 15: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

– Web Application with Existing Sources. Imports an existing web application into a standard project.

• free-form project

• A project that uses an existing Ant script to compile, run, and debug your application. The IDE comes with the following free-form project templates:

• Java Free-Form Project

• Web Free-Form Project

15

Page 16: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

• For our example we want to choose Web Application with Existing Sources because we have the source file for the servlet (from our LoginAppletNORMI example).

• Make sure that in your directory you have a folder named LoginAppletNORMI that contains the source code for the servlet.– That is because you want your project to have the name

of the web application as it appears in the url imbedded in the LoginApplet code.

16

Page 17: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

17

Page 18: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

• Next choose the server that the web application will be associated with.• For example I chose the GlassFish server because I have already

deployed the same application LoginApplet on Tomcat.

• In the Existing Sources and Libraries page, you specify the following properties:

• Web Pages Folder. Specifies the location of your web pages. You must have at least a valid web.xml deployment descriptor in the web pages folder to complete the wizard. – The IDE does not convert deployment descriptors for unsupported

application servers to the Glassfish application server deployment descriptors. There are external tools you can use for this purpose.

18

Page 19: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Creating the Web Application

• WEB-INF Content. Specifies the location of the application's WEB-INF content.

• Libraries Folder. Specifies the location of the class libraries that the web application depends on. All JAR files in this folder are added to the web application project's classpath and packaged with the application for deployment.

• Make sure that the jdk assigne dto this project is not the defualt SE version but the SUN/jdk that includes EE (because servelts are not part of the SE).

• Next import trhe Applet project into the project LoginAppletNORMI.

• Deploy and run.

19

Page 20: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Clients/EJB Architecture

20

Page 21: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Summary Of Session Bean’s Functionality (from “Help Menu” of NetBeans)

• A business method is a method inside an enterprise bean that performs some function and can be called from outside the bean.

• Business methods are written in the bean class and exposed through local or remote interfaces.

• If a business method is meant to be called by beans and clients that are running in the same JVM (on the same server), they are registered in the bean's local interface.

• If the method is meant to be called from a remote client, it is registered in the bean's remote interface. (Remember that if the bean client is a web application or an application client the bean’s interface is defined as remote, otherwise it is local).

21

Page 22: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Summary Of Session Bean’s Functionality

• A session bean's business methods typically define tasks that the bean performs for clients. They are not necessarily associated with data in a database (Entities handle database access) .

22

Page 23: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client.

• In NetBeans Create a new project called EnterpriseAppEE5– Select Enterprise Java Application from the Java EE

category and click Next.

– Set the server to GlasFish v2

– Set the Java EE version to EE5

– Select Create EJB Module

– Select Create Application Client Module

– Click Finish.

23

Page 24: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

• Create the session bean:

– Expand the project on the Projects Window and right click on the folder EnterpriseAppEE5-ejb

– Choose New->Other

– In the opened wizard:

• From the Java EE category, select Session Bean and click Next.

• Type Session as the name

• Select Remote for the type of interface . Click finish

– You now have the source editor opened in the middle of your screen showing which shows the skeleton code for the class SessionBean

24

Page 25: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

– Right click in the text editor by the name of the class and choose

InsertCode-> Add Business Method

– In the Add Business Method diaolog box type getResult for the name of the method and set the return type to be a String. Also select Remote. Interface box. Click OK.

– In the text editor modify the returned String to

return “This is a Session Bean” ;

Save it.

– Under EnterpriseAppEE5-ejb you should be able to expand the folder Source Packages-ejb and see the files:

SessionBean.java

SessionRemote.java

SessionLocal.java 25

Page 26: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client– The first file is the session bean class that was just created .

– The other two files are the two type s of interfaces automatically created. Only the Remote type has a method (because we asked so) the other is empty.

package ejb;

import javax.ejb.Remote;

/**

*

* @author George

*/

@Remote

public interface SessionRemote {String getResult();

}

26

Page 27: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

• The code for the SessionBean class looks now like:package ejb;

import javax.ejb.Stateless;

/**

*

* @author George

*/

@Stateless

public class SessionBean implements SessionRemote, SessionLocal {

public String getResult() {

return "This is a session EJB ";

}

}

27

Page 28: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

• Now we can create the code for the client.– In the Projects window expand

EnterpiseAppEE5-app-client->Source Packages->enterpriseaappee5

The file Main.java appears in the text editor pane.

– In the text editor right click on the code and choose:

Insert Code->Call Enterprise Bean

In the dialog box that appers expand EnterpriseAppEE5-ejb node and select SessionBean (as the bean that you want to call from this client)

Select Remote as the reference interface. Click OK.

– The following annotation is added to Main.java file;

@EJB

Private static SessionRemote sessionBean;

Modify the main method manually to include the invocation of the bean:

System.out.println(‘result of remote invocation=“+ sessionBean.getResult());

28

Page 29: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

• @EJB is called an annotation. The symbol for annotation is @

• There are many types of annotations used in Java EE – Annotations Replace descriptors for most purposes.

– Remove the need for marker interfaces (like java.rmi.Remote)

– Allow application settings to be visible in the component they affect.

– The Java EE 5 (and up) platform simplifies deployment by removing the need for explicit creation of deployment descriptors by the developer.

29

Page 30: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

– In summary, the Java EE 5 (and up) platform provides annotations for the following tasks, among others:

• Defining and using web services

• Developing EJB software applications

• Mapping Java technology classes to XML

• Mapping Java technology classes to databases

• Mapping methods to operations

• Specifying external dependencies

• Specifying deployment information, including security attributes

30

Page 31: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using an annotation in the Client

• Therefore in this example we used an annotation to link the EJB to the client.– We specified that the client is to connect to the specific bean.

• Notice also that this example was a single application that included the client and the bean together in the same project.

31

Page 32: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Running the Enterprise Application

• Right click on the name of the application in the Projects window and choose Run.

• The IDE should build and deploy the project.

• On the output pane you should see:

result=This is a session EJB

32

Page 33: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Annotations

• Annotations used in EE5 and up are described in package:

javax.annotation

• Annotation Types Summary :– Generated:The Generated annotation is used to mark source code that

has been generated.

– PostConstruct: The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

– PreDestroy: The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container.

– Resource:The Resource annotation marks a resource that is needed by the application.

– Resources:This class is used to allow multiple resources declarations.  33

Page 34: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Deployment Descriptors- Help Menu of NetBeans

• Deployment descriptors are XML-based text files whose elements describe how to assemble and deploy a module to a specific environment. The elements also contain behavioral information about components not included directly in code.

• Deployment descriptors specify two kinds of information: – Structural information describes the different components of the

JAR (or EAR) file, their relationship with each other, and their external dependencies. Environment entries and resource requirements are part of the structural information.

– Assembly information describes how contents of a JAR (or EAR) file can be composed into a deployable unit.

34

Page 35: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Deployment Descriptors- Help Menu of NetBeans

• There are different types of deployment descriptors: – EJB deployment descriptors described in the Enterprise JavaBeans

specification,

– web deployment descriptors described in the Servlet specification,

– and application and application client deployment descriptors described in the Java EE specification.

• For each type of module or application, there are two deployment descriptors:

35

Page 36: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Deployment Descriptors- Help Menu of NetBeans

– A general deployment descriptor that configures deployment settings on any Java EE-compliant implementation.

• The general deployment descriptor is named moduleType.xml (for example, ejb-jar.xml for EJB modules and web.xml for web application modules.)

– A server-specific deployment descriptor that configures deployment settings for a specific server implementation.

• For example, the deployment descriptors for the Sun Java System Application Server/GlassFish are named sun-moduleType.xml. The deployment descriptor for the Tomcat Web Server is named context.xml.

36

Page 37: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Deployment Descriptors- Help Menu of NetBeans

• The IDE generates a deployment descriptor from the settings for EJB modules, web application modules, and enterprise applications, when you create an empty enterprise project.

• It also reads your existing deployment descriptors when you import existing enterprise beans, enterprise applications, and web applications into the IDE.

• The deployment descriptors are located in the conf folder of your project directory, and are displayed under the Configuration Files node in the Projects window.

37

Page 38: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Using Lookup

• Normally the client needs to do a lookup in the naming service to find the bean.

• In the next example we would create an application where the client uses lookup instead of annotation.

• The next example also uses two projects, one for the bean and one for the client (in the previous example both client and bean were part of the same project).

38

Page 39: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create the EJB Module

• First create a new project and name it EJBModule30

This time the EJB module will not be part of an enterprise application, therefore select JavaEE and then EJB Module as the Java EE category when you create the project (in the previous project we selected Enterprise Application).

39

Page 40: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

40

Page 41: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

41

Page 42: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create the Session Bean

• We will create the Session Bean in the EJB Module.– In the Projects window right click the EJB Module and choose

New->Other

– Select Session Bean from the Enterprise Category. Click Next

– In the wizard that was opened type:

EJB Name: Bean30, package: ejb, Session Type:Stateless

Select Remote (the type of interface)

Click Finish

42

Page 43: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

43

Page 44: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create the Session Bean

• In the source editor:– Right click and choose insert->Add Business Method

– In the Add Business Method dialog box type getResult for the name of the method

– Set the retuned type to String

– Select the Remote Interface box and click OK.

– Modify the code manually:

public String getResult() {

return “This is EJB 3.0 Bean”;

}

– Modify the @Stateless annotation

@Stateless(mappedName=“Bean30”)

public class Bean30Bean implements Bean30Remote {

The client will use Bean30 to look up for the Bean. This is similar to registering a name for a remote object in RMI.

44

Page 45: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create the Session Bean

45

package ejb;import javax.ejb.Stateless;

/** * * @author George */@Stateless(mappedName="Bean30")public class Bean30Bean implements Bean30Remote {

public String getResult() { return "This is EJB 3.0"; } // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method" or "Web Service > Add Operation") }

Page 46: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create the Session Bean

• Notice that the interface with the business rule (aka service or method) is automatically created by the tool and annotated as remote interface:

– package ejb;

import javax.ejb.Remote

@Remote

public interface Bean30Remote {

String getResult();

}

– The code for the interface can be found under the folder

EJBModule30->Source Packages->ejb

46

Page 47: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Create Application Client

• We now create a new project for the Application Client:– This time select Enetrprise Application Client

from the Enterprise category and name the project: ApplicationClientForTest

• We now want to add the EJBModule30 project to ApplicationCleintForTest project as a library and add the code to access Bean30(alias for our bean).

47

Page 48: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Adding Application Client Project to the Bean project

• Expand ApplicationClientForTest project node (in Projects window).

– Right Click on Libraries node and select Add Project • In the dialog box select EJBModule30 project and click Add

Project Jar Files.

• You should be able to see now the EJBModule30.jar file as part of the Libraries for ApplicationClientForTest project.

• In the text editor open the Main.java. And add the code for lookup manually.

• Then click ctrl-shft-I and the proper import statements will be added.

48

Page 49: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Calling EJB from Clientpackage applicationclientfortest;

import ejb.Bean30Remote;

import javax.naming.InitialContext;

public class Main {

* @param args the command line arguments

*/

public static void main(String[] args) throws NameingException {

// TODO code application logic here

try

{

IntialContext ctx=new InitialContext();

Bean30Remote br=(Bean30Remote)ctx.lookup("Bean30");

System.err.println("EJB message is:"+br.getResult());

}

catch(NamingException ne){}

}

} 49

Page 50: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Build the applications

• Build and deploy each of the projects:– EJBModule30– ApplicationClientForTest– Run the ApplicationClientForTest

50

Page 51: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

JNDI

• JNDI (Java Naming Directory Interface) API provides a variety of services to Java Applications:– Naming Service: The means by which names are associated with

objects.

– Binding: The actual association of a name with an object.

– A pointer or a reference to the object is stored inside the Naming Service.

• A reference is information about how to access the remote object.

– Context: A set of name/object bindings.• A context provides a lookup, otherwise known as “Resolution” , operation.

• The operation returns the information about the object.

• Context may also provide other operations for binding, unbinding, listing of stored names/objects bindings.

51

Page 52: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

JNDI

– A hierarchical tree of bindings can be formed where one binding is bound to another binding and so on .

– The application component's naming environment is a mechanism that allows customization of the application component's business logic during deployment or assembly. Use of the application component's environment allows the application component to be customized without the need to access or change the application component's source code.

– A Java EE container implements the application component's environment, and provides it to the application component instance as a JNDI naming context.

52

Page 53: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

JNDI

• Each application component defines its own set of environment entries. All instances of an application component within the same container share the same environment entries. Application component instances are not allowed to modify the environment at runtime.

• A resource reference is an element in a deployment descriptor that identifies the component’s coded name for the resource.

• In the previous example the EJB was identified by the name: “Bean30”.

• Environment settings appear in the descriptor files.53

Page 54: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Multiple Interfaces

• Suppose that in the previous example we had a second interface like:

@Remote

public interface Bean30Remote2 {

String getResult2();

}

54

Page 55: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Multiple Interface

• In the Bean30Bean.java we would need to implement both interfaces:

55

Page 56: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

56

Page 57: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Multiple Interfaces-Modify Client

• The client will need to be modified also:• Double-click the main.java class of ApplicationClientForTest in the Projects

window to open the class in the Source Editor.

• Modify the code in the main method to look up the second interface.

• In this case, when there is more than one remote interface, we have to use the full name for each of the interfaces in the lookup:

57

Page 58: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

58

public static void main(String[] args) throws NamingException

{ InitialContext ctx = new InitialContext();

Bean30Remote br = (Bean30Remote) ctx.lookup("Bean30#ejb.Bean30Remote"); System.err.println("EJB message is:" + br.getResult());

Bean30Remote2 br2 = (Bean30Remote2) ctx.lookup("Bean30#ejb.Bean30Remote2"); System.err.println("EJB message 2 is:" + br2.getResult2()); }

Fix the imports to add an import statement for ejb.Bean30Remote2. Save your changes.Build, Deploy and Run.

Page 59: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Rules for Multiple Interfaces

• In case of Multiple interfaces the Interfaces must explicitly be annotated as Remote or Local (with a single interface we can omit the annotation and it will be assumed as Local).

59

Page 60: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Servlets

• NetBeans has some servlet applications examples that can be run.

• Choose

New Project->Samples->Java Web->ServletExamples.

Assign Tomcat as the server.

Build, Deploy, Run

60

Page 61: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Cart Example

• The cart example in the text EE5 Tutorial (Chapter 22) is of significance for the following concepts:– A Stateless bean can implement:

• The business methods defined in the interface.

• Additional Business Methods annotated by @Remove (not listed in the interface).

• Optional Life Cycle Callback Methods.

– Additional Helper Classes implemented.61

Page 62: 1 Lecture 16 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Study Guide

• Chapter 3 of Java EE 6 Tutorial.

• Chapters 14,15,16 of Java EE Tutorial.

• Do examples under samples in new projects of NetBeans IDE.

62