Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES Lecture 15 George Koutsogiannakis SUMMER 2011 1 Topics • NetBeans the JDK and other settings. – Instances of the server. – Domains. • Using NetBeans to create a Session Bean. – Opening an existing project. – Creating an empty new project. • Example of converter project. 2 NetBeans the JDK and other settings • Have uniformity in your installation. Mixing versions of NetBeans / JDK and Other tools can cause problems. • Best configuration: – JDK 1.6 with EE capability (install before NetBeans). – JDK 6 Update 12 or higher with Java EE from http://java.sun.com/javase/downloads/index.jsp • Gets installed in C:\Sun\SDK\ • It includes all the libraries needed 3 NetBeans the JDK and other settings – NetBeans 6.5 with GlassFish v2 as a minimum. – http://www.netbeans.org/downloads/index.html – Download All option or as a minimum Java SE Java Web and EE and The bundled Servers. 4 NetBeans the JDK and other settings • You can add or remove packs later using the IDE's Plugin Manager (Tools | Plugins). Java 6 or Java 5 is required for installing and running the PHP, Ruby and C/C++ NetBeans Bundles. You can download the latest Java at java.com. JDK 6 or JDK 5 is required for installing and running the Java SE, JavaFX, Java and All NetBeans Bundles. You can download standalone JDK or download the latest JDK with NetBeans IDE Java SE bundle. 5 NetBeans the JDK and other settings – After download and during installation: • Direct NetBeans to use \Sun\SDK\jdk as the default jdk (JDK 6 with update 12 and higher). • Other JDK platforms can also be added if you wish, as additions to default jdk. • Make sure that in the Services window of NetBeans you add the servers: – GlassFish V2 or and V3 – Tomcat 6.x – You can start the servers from there by right clicking on the server ‘s name. 6 NetBeans and the JDK and other settings – Install Tomcat 6.x and add it as a server under services in NetBeans. • Servers that exist can be added from Tools-> server and click on Add Server in the open Dialog. • Make sure that servers do not have conflicting ports. – Most likely you would have to change Tomcat ‘s default port from 8080 to another port like 8090. The shut down port for Tomcat would also need to be changed: » Services .. .. Highlight the server ‘s name. » Right click and click on Properties. Change the port numbers on the opened dialog pane. 7 NetBeans and the JDK and other settings – Avoid activating GlassFish V3 which an application server associated with a new OOP Language called JRuby. • Another configuration is: – http://www.netbeans.org/downloads/index.html – Download the All choice which does not include the JDK. – Use the JDK installation from previous configuration. 8 NetBeans and the JDK and other settings • Every project created in NetBeans must have an instance of the application server GlassFish registered. – When you open an existing project a message comes up warning you that an instance of the server needs to be registered (if one does not exist). • You can right click on the projects name and choose: “Resolve Missing Server Problem” Notice that in this case you will have to be aware of the path in your System where the application server is located. Note that in most cases the registration of an instance will be done automatically and you will not have to do the above. A default instance is created under Domain “domain1”. 9 NetBeans and the JDK and other settings • Another approach is to start the proper server after activating NetBeans. – Under Services right click on the server name and click start. Wait few seconds until the server is up and running. – Verify that the server is running : • If the server is not running there will be a message at the bottom pane of the screen. • If the server is running the stop and restart menu option will be active and the start option inactive. 10 NetBeans Domains. • A Domain is a process that encompasses a unique application’s modules. It is an instance of the application server. – It isolates one application from another. No direct communication is allowed between domains without marshalling a remote object. – Each domain has • the port number of the server • The port number of the administration server including User Name and password for administering the server. • Security settings. 11 Server’s log • You can get some idea of cause of failures that result in the server not starting by examining the log file of the server under <server installation>domains/domain1/logs/server file. • Log files are also accessible for the administrative console of the server. • To access the administrative console as a web application – Right click on the server name and choose View Admin Console • A password for the domain you established during installation, is required. 12 NetBeans and the JDK and other settings • The next slide shows the NetBeans window with – On the left : the 2 basic windows Projects and Services . • Other windows can be added (or closed) from the Windows menu on top. – In the center usually we have the editor window where the code of a module will appear. – On right we have the Properties window which displays properties of the focused module. 13 NetBeans and the JDK and other settings – On the bottom we have the output window which will display: • Information on the building process • Information on the deployment process • Any output when you run the deployed project 14 15 16 Session Beans • Session Bean can be called by: – Remote Application Client or – By Web Client (JSP , Servlet). – By another Bean component. 17 Session Beans • Basic steps: – Develop the code for the Bean • Interface with business methods definitions • Class that implements the interface. – Develop code for the Client Application (if there is one) – Develop code for a web client (if there is one). 18 Session Beans-Open an existing Project – Save your files in some path of your choice within your system. – From the File menu of NetBeans use Open Project to bring the files in (this is provided that your files were configured as a project). – The symbol for the overall project is a blue triangle. – You need to notify NetBeans that this project is the main project under focus right now (because multiple projects can be there). – The main project is the project that contains the application’ s main class. • Go to the menu “Run” and select “Set Main Project”-> choose your project’s name. 19 Session Beans-Open an existing Project – Compile the files by right clicking on the name of each module and choosing build. • Using NetBeans or ant tool build the application client files (if there is an application client) into a jar file. • Using NetBeans or ant tool build the web client (if there is a web client) into a war file. • Using NetBeans or ant tool build the ejb into a jar file. 20 Session Beans-Open an existing Project • Instead of the individual builds listed above , you can build the entire project by right clicking on the project’ s name and choosing the build option. • If you do that do not build each application separately again. That will cause you a problem. 21 Session Beans • Once the project has been built, it needs to be deployed. – Make sure that Tomcat is running (if it is only a web project) or if you are using GlassFish: – You can start GlassFish v2 manually or it should start automatically as part of the deployment. • Notice that the building process can fail for various reasons as well as deployment. – Failure to deploy could be because of a number of reasons including: 22 Session Beans • Port conflicts between the various servers. • Incorrect server configuration. • Invalid JDK libraries (libraries that are needed but don’t exist because of incorrect jdk libraries being used by NetBeans. – This can be corrected by changing the default jdk libraries for the project. – Right click on the name of the project and choose Properties. – In the opened dialog pane highlight “libraries” and then find the path to the proper jdk libraries. – From the combo box choose the proper java platform. • Other reasons –check NetBeans documentation or help menu. 23 24 Create a New Project • Before we provide any coding (either manually or by having NetBeans produce the code) we need to create a project: – Choose File-> New Project from Menu Bar – Select Enterprise Application from Java EE category , click Next. – Type a name for your project. – Deselect Use Dedicated Folder option if selected. Click Next 25 Create a New Project – Set the server to GlassFish and set Java EE version to EE5 or EE6. – Select Create EJB Module and Create Application Client Module (if we are creating an application module). – Click Finish 26 Creating a Session Bean • We can create a session bean by using NetBean utilities or by writing the code ourselves or by a combination of the two. • Example of a Session Bean projects are part of NetBeans IDE. • Let us look at the converter project example from the JAVA EE 6 TUTORIAL text. – This ejb application of the converter project provides the services for the conversion of dollars to to yen and yento Euro – It has a client application that communicates with the ejb. – It also has a web application that communicates with the ejb in the form of jsp code. – What is needed programmatically is: 27 Creating a Session Bean – The interface that defines the methods to be implemented by the ejb. – The class that implements the methods of the interface. – The client application that can invoke directly the methods of the ejb (dollar to yen and yen to Euro conversions). – The web application that can invoke the methods of the ejb via the web from a browser. • A jsp file resides in a web server. The file creates an html form that gets transferred to a browser instance. The user can enter amounts that can be converted. • Note : the client application and the web aplication are independent of each other. 28 Converter Bean Example Client Java Application for ConverterBean Html generated by Web Client Runs on Browser ConverterBean runs on the Application Server part of GlassFish Web Client for ConverterBean runs on Web Server part of GlassFish 29 Client Application package converter.client; import converter.ejb.Converter; import java.math.BigDecimal; import javax.ejb.EJB; public class ConverterClient { @EJB // ANNOTATION private static Converter converter; /** Creates a new instance of Client */ public ConverterClient(String[] args) { } /** * @param args the command line arguments */ public static void main(String[] args) { ConverterClient client = new ConverterClient(args); client.doConversion(); } 30 Client Application public void doConversion() { try { BigDecimal param = new BigDecimal("100.00"); BigDecimal yenAmount = converter.dollarToYen(param); // Above line is the invocation of the remote method implemented by the // //ejb component. The variable yenAmount captures the returned value of // the remote invocation System.out.println("$" + param + " is " + yenAmount + " Yen."); BigDecimal euroAmount = converter.yenToEuro(yenAmount); //another invocation involving the second remote method of the ejb System.out.println(yenAmount + " Yen is " + euroAmount + " Euro."); System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } } 31 Remote Interface for the Session Bean package converter.ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote // ANNOTATION public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); public BigDecimal yenToEuro(BigDecimal yen); } 32 Session EJB Code package converter.ejb; import java.math.BigDecimal; import javax.ejb.Stateless; @Stateless //ANNOTATION public class ConverterBean implements converter.ejb.Converter { private BigDecimal euroRate = new BigDecimal("0.0070"); private BigDecimal yenRate = new BigDecimal("112.58"); public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); } } 33 Web Client-JSP <%@ page import="converter.ejb.Converter, java.math.*, javax.naming.*"%> //directive tag <%! private Converter converter = null; public void jspInit() { try { InitialContext ic = new InitialContext(); //lookup converter = (Converter) ic.lookup(Converter.class.getName()); //lookup } catch (Exception ex) { System.out.println("Couldn't create converter bean."+ ex.getMessage()); } } public void jspDestroy() { converter = null; } %> 34 Web Client -JSP <html> <head> <title>Converter</title> </head> <body bgcolor="white"> <h1>Converter</h1> <hr> <p>Enter an amount to convert:</p> <form method="get"> <input type="text" name="amount" size="25"> <br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> 35 Web Client -JSP <% String amount = request.getParameter("amount"); if ( amount != null && amount.length() > 0 ) { BigDecimal d = new BigDecimal(amount); BigDecimal yenAmount = converter.dollarToYen(d); //invocation %> <p> <%= amount %> dollars are <%= yenAmount %> Yen. <p> <% BigDecimal euroAmount = converter.yenToEuro(yenAmount); //invocation %> <%= yenAmount %> Yen are <%= euroAmount %> Euro. <% } %> </body> </html> 36 Session EJB • You can import the files for the example projects from the CD – In that cased use Open Project in NetBeans. • Or you can create your own files and create a new project by following the previous instructions. • Start the Server (i.e. GlassFish) • Build the project. • Deploy the project. Make sure that Tomcat and GlassFish v2 are running (without conflicts). • Run the project. 37 38 Deployment • Once a project is deployed: – Do not deploy again every time you activate NetBeans. – It is deployed once and stays deployed even if you exit NetBeans and you open NetBeans later on. – Once the project is deployed, to run the client application: • Right click on the project name and choose run (as main project). The client output (if any) will appear at the bottom of the NetBeans console (under Output). 39 Convert Client Application Output 40 Converter project Web Application • To run the Web Application (Web Client for ejb): – Open an instance of the browser and in the address field type: http://localhost:8090/convert/index.jsp And press enter Notice that 8090 is an example port for the web server part of GlassFish. You must know the web server port for your installation of GlassFish. 41 Web Client for converter ejb 42 Deployment Issues • If for some reason the deployment was unsuccessful you can try redeploying • First undeploy any parts of the application that were deployed. – Follow this procedure also if you want to make changes to the code and need to rebuild and redeploy • Go to the GlassFish Administrative Console an log in. 43 Deployment Issues • In the left side of the console click on Enterprise applications. – In the screen that comes up choose the converter application and then click on undeploy button. – Repeat the same for other parts of the application • Web Application • EJB Modules • Application Client Modules. 44 NetBeans and support for Applets • NetBeans supports development of Applets. • Create the Java project from scratch or from existing sources • Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java. • Choose one of the following: – If you are creating a new applet source file, select Java Class Library under Projects. Click Next. – If you want to import an applet source file, select Java Project with Existing Sources. Click Next. Specify the file's location in the Source Packages Folder text box. • Under Project Name, type HelloApplet. Change the Project Location to any folder on your computer. • Click Finish. If you imported an applet source file, run it. 45 NetBeans and support for Applets • Create the applet source file • Right-click the HelloApplet project node in the Projects window and select New > Other (Ctrl-N). • Under Categories, select Java. Under File Types, select Applet. Alternatively, if you want to visually design your applet, select Swing GUI Forms > JApplet Form. • If you want to import an applet source file, select Java Project with Existing Sources. Click Next. Specify the file's location in the Source Packages Folder text box. 46 NetBeans and support for Applets • Click Next. • Under Class Name, type MyApplet. Under Package, type org.me.hello. Click Finish. The IDE creates the applet source file in the specified package. The applet source file opens in the Source editor. • Define your applet class by copying and pasting the following code over the existing default code: 47 NetBeans and support for Applets • The IDE creates the basic applet code: public class HelloApplet extends JApplet { public static int() { } } You can add methods and code as needed 48 NetBeans and support for Applets • The applet that you created can be run from the IDE. – Right-click the HelloApplet project node in the Projects window and choose Build from the contextual menu. – The HelloApplet.jar file is created in the dist folder, which you can see if you switch to the Files window . – Right-click the applet class node (i.e., right-click "MyApplet.java" not the "HelloApplet" project) in the Projects window and choose Run File from the contextual menu. The MyApplet.html launcher file, with the applet embedded, is created in the build folder, which you can see if you switch to the Files window (Ctrl-2): 49 NetBeans and support for Applets • Your applet is complete. Now you need to make it available to the user by deploying it as a web application. • To do so, you create a web application, put the applet JAR on its classpath, and then add an applet tag to the web application's HTML file. • Create the web project – Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application. Click Next. – Under Project Name, type HelloWebApplet. Change the Project Location to any folder on your computer. Click Next, select your server, and click Finish. 50 NetBeans and support for Applets • Add the applet JAR file to the web project – When you want to include an applet JAR file in a web project, you can do so by • adding the Java project that contains the JAR file, or • by adding the JAR file itself. Although the choice is yours, note that when you add the Java project to the web project, you enable the IDE to build the applet whenever you build the web application. Therefore, when you modify the applet in the Java project, the IDE builds a new version of the applet whenever the web project is built. • On the other hand, if the applet JAR file is not in a NetBeans IDE project, the applet source file is not rebuilt when you build the web project. 51 NetBeans and support for Applets • TO ADD THE APPLET PROJECT TO THE WEB APPLICATION: – In the Projects window, right-click the HelloWebApplet project node and select Properties from the contextual menu. Choose one of the following: • If the applet JAR file is in a Java project, click Packaging, and then click Add Project. Browse to and select the folder that contains the Java project. Note that IDE projects are marked by the NetBeans IDE project icon. • If the applet JAR file is not in a IDE project, click Packaging, and then click Add JAR/Folder. Browse to and select the folder that contains the JAR file. – The JAR file containing the applet source file is listed in a table at the bottom of the Project Properties dialog box. 52 NetBeans and support for Applets • Optionally, you can type a location for the applet in the table's Path in WAR column (in the Properties dialog box). – By default, the applet JAR file will be copied to the web application's document root, which is the build/web folder (the highest level of the web application's file structure). Click OK. – When you build the project, the applet's JAR file is packaged in the project's WAR file in the dist folder. It is also added to the build/web folder. 53 NetBeans and support for Applets • Create and run the JSP file or HTML file • Choose one of the following: – If you want to embed the applet in a JSP file, double-click the default index.jsp file in the Projects window. This file is created by the IDE when you create a web project. It opens in the Source Editor. – If you want to embed the applet in an HTML file, right-click the HelloWebApplet project node, and choose New > Other from the contextual menu. Under Categories, select Web. Under File Types, select HTML. Click Next. Give your HTML file a name and click Finish. • Embed the applet in the file by adding the following applet tag anywhere between the file's <body></body> tags: <applet code="org.me.hello.MyApplet" archive="HelloApplet.jar"/> • org.me.hello.MyApplet is the full classname to your applet • HelloApplet.jar is the JAR file that contains the applet 54 NetBeans and support for Applets 55 NetBeans and support for Applets • TO RUN THE WEB APPLICATION: – Right-click the JSP node or HTML node in the Projects window and choose Run File from the contextual menu. The server deploys the JSP file or HTML file in the IDE's default browser. 56 Applet Servlet Web Application • Create the web application with the servlet first. • Add the applet project to the web application’s project that contains the servlet by following the procedure outlined in previous slides. 57 Study Guide • Chapters 14, 15, 16 of EE 6 Tutorial. • NetBeans documentation. 58