620 likes | 693 Vues
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES. Lecture 16. George Koutsogiannakis /SUMMER 2011. Topics. Running an Applet in NetBeans Implementing the LoginAppletNORMI example. A session EJB client with annotations. Annotations. Deployment Descriptors.
E N D
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES Lecture 16 George Koutsogiannakis/SUMMER 2011
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.
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.
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.
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.
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).
Result of testing LoginApplet example with “Run File” command.
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.
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.
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.
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.
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:
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
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
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.
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.
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.
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 localinterface. • 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).
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) .
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.
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
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
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(); }
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 "; } }
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());
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.
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
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.
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
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.
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.
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:
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.
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.
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).
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).
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
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.
Create the Session Bean 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") }
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
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).
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.
Calling EJB from Client package 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){} } }
Build the applications • Build and deploy each of the projects: • EJBModule30 • ApplicationClientForTest • Run the ApplicationClientForTest