Lösungsvorschläge zum Übungsblatt 13: Fortgeschrittene Aspekte objektorientierter Programmierung (WS 2005/06)



Similar documents
Anno Accademico 2004/2005. Sperimentazione di Ingegneria del Software II. Giovanna Petrone J2EE. Laboratorio

Software Development using MacroMedia s JRun

Java E-Commerce Martin Cooke,

Web Container Components Servlet JSP Tag Libraries

Introduction to Entity Beans

Aufgabenstellung. Aufgabenstellung

EJB 3.0 and IIOP.NET. Table of contents. Stefan Jäger / stefanjaeger@bluewin.ch

Automatic generation of distributed dynamic applications in a thin client environment

Creating Java EE Applications and Servlets with IntelliJ IDEA

Java Servlet and JSP Programming. Structure and Deployment China Jiliang University

ACM Crossroads Student Magazine The ACM's First Electronic Publication

2. Follow the installation directions and install the server on ccc

Objectives. Software Development using MacroMedia s JRun. Enterprise Application Model. Topics for Discussion

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

How To Test A Load Test On A Java Testnet 2.5 (For A Testnet) On A Test Server On A Microsoft Web Server On An Ipad Or Ipad (For An Ipa) On Your Computer Or Ipa

Web Application Programmer's Guide

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

An introduction to web programming with Java

Java Server Pages combined with servlets in action. Generals. Java Servlets

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

2.8. Session management

EJB & J2EE. Component Technology with thanks to Jim Dowling. Components. Problems with Previous Paradigms. What EJB Accomplishes

Web Application Architecture (based J2EE 1.4 Tutorial)

How To Write A Bean In Java (Java) (Java 2.4.2) (Java.Net) (Javax) 2 (Java Bean) ( Java Bean) 2-4.5

Controlling Web Application Behavior

CHAPTER 9: SERVLET AND JSP FILTERS

Java 2 Web Developer Certification Study Guide Natalie Levi

Introduction to J2EE Web Technologies

MDA Overview OMG. Enterprise Architect UML 2 Case Tool by Sparx Systems by Sparx Systems

The Collaborative Information Portal and NASA s Mars Rover Mission

Java 2 Platform, Enterprise Edition (J2EE) Bruno Souza Java Technologist, Sun Microsystems, Inc.

J2EE-Application Server

Work with XI 3.0 Java Proxies

Java OGSI Hosting Environment Design A Portable Grid Service Container Framework

Enterprise JavaBeans Fundamentals

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

SSC - Web applications and development Introduction and Java Servlet (II)

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

How to program Java Card3.0 platforms?

Web Programming: Announcements. Sara Sprenkle August 3, August 3, Assignment 6 due today Project 2 due next Wednesday Review XML

WebOTX Application Server

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

Service Data and Notifications

Announcements. Comments on project proposals will go out by in next couple of days...

CONTROLLING WEB APPLICATION BEHAVIOR WITH

Supplement IV.E: Tutorial for Tomcat. For Introduction to Java Programming By Y. Daniel Liang

NetBeans IDE Field Guide

Course Name: Course in JSP Course Code: P5

How to use JavaMail to send

Java Servlet 3.0. Rajiv Mordani Spec Lead

JSP Java Server Pages

INTRODUCTION TO WEB TECHNOLOGY

J2EE Web Development. Agenda. Application servers. What is J2EE? Main component types Application Scenarios J2EE APIs and Services.

Piotr Nowicki's Homepage. Java EE 6 SCWCD Mock Exam. "Simplicity is the ultimate sophistication." Important!

Software Development. COMP220/COMP285 Seb Coope Introducing Ant

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

Enterprise Applications

The Duke s Bank Application

Java 2 Platform, Enterprise Edition (J2EE): Enabling Technologies for EAI

Ch-03 Web Applications

Load Balancing in Cluster

Module 13 Implementing Java EE Web Services with JAX-WS

Oracle WebLogic Server

Web Applications. For live Java training, please see training courses at

Java EE 6 New features in practice Part 3

Pure server-side Web Applications with Java, JSP. Application Servers: the Essential Tool of Server-Side Programming. Install and Check Tomcat

Java Web Programming. Student Workbook

DTS Web Developers Guide

CGI Vs. Java - Which is Better For Marketing

Tutorial c-treeace Web Service Using Java

Enabling Grids for E-sciencE. Web services tools. David Fergusson. INFSO-RI

Implementing the Shop with EJB

Principles and Techniques of DBMS 5 Servlet

Component Middleware. Sophie Chabridon. INT - INF Department - Distributed Systems team 2006

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

JSP. Common patterns

Password-based authentication

CS506 Web Design and Development Solved Online Quiz No. 01

Java Servlet Tutorial. Java Servlet Tutorial

JBoss SOAP Web Services User Guide. Version: M5

Java EE 6 Ce qui vous attends

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, Java Servlets

IBM Rational Rapid Developer Components & Web Services

Essentials of the Java(TM) Programming Language, Part 1

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Manual. Programmer's Guide for Java API

Brekeke PBX Web Service

BAPI. Business Application Programming Interface. Compiled by Y R Nagesh 1

PowerTier Web Development Tools 4

Geronimo Quartz Plugins

Glassfish, JAVA EE, Servlets, JSP, EJB

INSIDE SERVLETS. Server-Side Programming for the Java Platform. An Imprint of Addison Wesley Longman, Inc.

Servlet and JSP Filters

Java Server Pages and Java Beans

Session Tracking Customized Java EE Training:

WebSphere and Message Driven Beans

Penetration from application down to OS

Web Service Development Using CXF. - Praveen Kumar Jayaram

Transcription:

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. N. Rauch Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik http://softech.informatik.uni-kl.de/fasoop Lösungsvorschläge zum Übungsblatt 13: Fortgeschrittene Aspekte objektorientierter Programmierung (WS 2005/06) Aufgabe 1 EJB Remote Interface: Stellt die Methoden der Bean zur Verwendung durch Clients bereit. / Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. package samples.ejb.stateless.simple.ejb; Remote interface for the GreeterEJB. The remote interface defines all possible business methods for the bean. These are the methods going to be invoked remotely by clients, once they have a reference to the remote interface. Clients (GreeterServlet, in this case), generally take the help of JNDI to lookup the bean s home interface (GreeterHome, in this case) and then use the home interface to obtain references to the bean s remote interface (Greeter, in this case). public interface Greeter extends javax.ejb.ejbobject { Returns a greeting. @return returns a greeting as a string. @exception throws a RemoteException. public String getgreeting() throws java.rmi.remoteexception; Home Interface: Erlaubt den Clients, Zugriff auf eine Bean zu bekommen (durch das Remote Interface). / Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. package samples.ejb.stateless.simple.ejb; Home interface for the GreeterEJB. Clients generally use home interface to obtain references to the bean s remote interface. public interface GreeterHome extends javax.ejb.ejbhome { Gets a reference to the remote interface to the Greeter bean. @exception throws CreateException and RemoteException. public Greeter create() throws java.rmi.remoteexception, javax.ejb.createexception;

Implementierung: Der Code der Enterprise Java Bean selbst. / Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. package samples.ejb.stateless.simple.ejb; import java.util.; import java.io.; A simple stateless bean for the HelloWorld application. This bean implements one business method as declared by the remote interface. public class GreeterEJB implements javax.ejb.sessionbean { private javax.ejb.sessioncontext m_ctx = null; Sets the session context. Required by EJB spec. @param ctx A SessionContext object. public void setsessioncontext(javax.ejb.sessioncontext ctx) { m_ctx = ctx; Creates a bean. Required by EJB spec. @exception throws CreateException. public void ejbcreate() throws javax.ejb.ejbexception, javax.ejb.createexception { System.out.println("ejbCreate() on obj " + this); Removes the bean. Required by EJB spec. public void ejbremove() { System.out.println("ejbRemove() on obj " + this); Loads the state of the bean from secondary storage. Required by EJB spec. public void ejbactivate() { System.out.println("ejbActivate() on obj " + this); Serializes the state of the bean to secondary storage. Required by EJB spec. public void ejbpassivate() { System.out.println("ejbPassivate() on obj " + this); Required by EJB spec. public void Greeter() { 2

Returns a greeting, based on the time of the day. @return returns a greeting as a string. public String getgreeting() { String message = null; Calendar calendar = new GregorianCalendar(); int currenthour = calendar.get(calendar.hour_of_day); if(currenthour < 12) message = "morning"; else { if( (currenthour >= 12) && (calendar.get(calendar.hour_of_day) < 18)) message = "afternoon"; else message = "evening"; return message; Client: Ein Beispielclient (hier ein HTTP-Servlet). Zuerst beschafft sich der Client eine Referenz auf das home interface der Bean, dann erzeugt er eine Referenz auf das remote interface der Bean. Anschließend kann er auf die Methoden (= Geschäftslogik) der Bean zugreifen. / Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. package samples.ejb.stateless.simple.servlet; import java.io.; import java.util.; import javax.servlet.; import javax.naming.; import javax.servlet.http.; import javax.rmi.portableremoteobject; import javax.ejb.; import samples.ejb.stateless.simple.ejb.; This servlet is responsible for throwing the html pages for the HelloWorld application. public class GreeterServlet extends HttpServlet { The doget method of the servlet. Handles all http GET request. Required by the servlet specification. @exception throws ServletException and IOException. public void doget (HttpServletRequest request,httpservletresponse response) throws ServletException, IOException { javax.ejb.handle beanhandle; Greeter mygreeterbean; GreeterHome mygreeterhome; Greeter mygreeterremote; InitialContext initcontext = null; Hashtable env = new java.util.hashtable(1); ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", Locale.getDefault()); System.out.println("\n"+rb.getString("greeting_servlet")+ "..."); System.out.println(rb.getString("retrieving_jndi")); try { initcontext = new javax.naming.initialcontext(); 3

catch (Exception e) { System.out.println(rb.getString("exception")+": " + e.tostring()); try { System.out.println(rb.getString("looking_up")); String JNDIName = "java:comp/env/ejb/greeter"; System.out.println(rb.getString("looking")+": " + JNDIName); Object objref = initcontext.lookup(jndiname); mygreeterhome = (GreeterHome)PortableRemoteObject.narrow(objref, GreeterHome.class); catch(exception e) { System.out.println(rb.getString("greeter_bean")+" - " + rb.getstring("is_registered")+"?: " + e.tostring()); try { System.out.println(rb.getString("creating")); mygreeterremote = mygreeterhome.create(); catch(createexception e) { System.out.println(rb.getString("could_not")+": "+ e.tostring()); System.out.println(rb.getString("getting")); String themessage = mygreeterremote.getgreeting(); System.out.println(rb.getString("got")+": " + themessage); System.out.println(rb.getString("storing")); request.setattribute("message", themessage); System.out.println(rb.getString("dispatching")); response.setcontenttype("text/html"); RequestDispatcher dispatcher; dispatcher = getservletcontext().getrequestdispatcher ("/GreeterView.jsp"); dispatcher.include(request, response); The dopost method of the servlet. Handles all http POST request. Required by the servlet specification. @exception throws ServletException and IOException. public void dopost (HttpServletRequest request,httpservletresponse response) throws ServletException, IOException { doget(request,response); Returns the servlet info as a String. @return returns the servlet info as a String. public String getservletinfo() { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", Locale.getDefault()); return rb.getstring("call")+"."; Die wichtigsten Auszüge aus dem Buildfile: 4

<!-- Tagets to build and deploy sample application --> <target name="init" depends="init_common" /> <target name="compile" depends="compile_common" /> <target name="jar" depends="init,create_ejbjar_common"/> <target name="core" depends="compile,jar" /> <!-- Taken from common makefile --> <target name="init_common" depends="build_cpath"> <!-- define these properties if samples dont define --> <property name="classpath.prefix" value="."/>... <!-- Classpath and environment --> <target name="build_cpath" unless="appserver.cpath"> <path id="sample_classpath_jars"> <fileset dir="${com.sun.aas.installroot/lib" includes=".jar" /> <fileset dir="${com.sun.aas.webserviceslib" includes=".jar"/> <fileset dir="${com.sun.aas.imqlib" includes=".jar"/> <fileset dir="${com.sun.aas.javaroot/lib" includes="tools.jar"/> </path> <pathconvert pathsep=":" property="appserver.cpath" refid="sample_classpath_jars"/> <!-- Compile all classes. --> <target name="compile_common" depends="init_common"> <mkdir dir="${build.classesdir"/> <echo message="compiling source files into ${build.classesdir" /> <javac srcdir="${src" destdir="${build.classesdir" classpath="${build.classpath"/> <!-- Assemble EJB JAR module. --> <target name="check_ejbjarfile"> <available file="${build.classesdir/${jar.pkg/" type="dir" property="ejbjarfiles.present"/> <target name="add_sharejar_to_ejb_jar" if="sharejar.done"> <jar destfile="${assemble.ejbjar/${ejbjar" update="true"> <zipfileset dir="../${sample.name${sharejar-dir-suffix/${assemble.sharejar" includes="${sharejar"/> <target name="add_shareclasses_to_ejb_jar" if="sharejar.done"> <jar destfile="${assemble.ejbjar/${ejbjar" update="true"> <zipfileset dir="../${sample.name${sharejar-dir-suffix/${build/classes/" includes=".class" excludes="servlet/"/> <target name="add_wsdlfiles_to_ejb_jar" if="iswebserviceclient"> 5

<jar destfile="${assemble.ejbjar/${ejbjar" update="true"> <zipfileset dir="../${sample.name${ejbjar-dir-suffix/${src.conf" includes="${wsdlfile" prefix="meta-inf/wsdl/"/> <zipfileset dir="../${sample.name${ejbjar-dir-suffix/${build/shared" includes="mapping.xml" prefix="shared/"/> <target name="add_classes_to_ejb_jar"> <jar destfile="${assemble.ejbjar/${ejbjar" update="true"> <zipfileset dir="${build.classesdir/" includes=".class" excludes="servlet/, client/"/> <metainf dir="${jardd.dir" includes="${jardd"/> <target name="mkdir_ejbjar_common"> <mkdir dir="${assemble.ejbjar"/> <target name="create_ejbjar_common" depends="clean_ejbjar_common,mkdir_ejbjar_common,compile_common,add_classes_to_ejb_jar, add_shareclasses_to_ejb_jar,add_wsdlfiles_to_ejb_jar"> <property name="ejbjar.done" value="true"/> <target name="clean_ejbjar_common"> <delete dir="${assemble.ejbjar"/> </project> Deployment Descriptors: <?xml version = "1.0" encoding = "UTF-8"?> <!-- Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. --> <ejb-jar xmlns = "http://java.sun.com/xml/ns/j2ee" version = "2.1" xmlns:xsi = "http://www.w3.org/2 <display-name>greeterjar</display-name> <enterprise-beans> <session> <display-name>thegreeter</display-name> <ejb-name>thegreeter</ejb-name> <home>samples.ejb.stateless.simple.ejb.greeterhome</home> <remote>samples.ejb.stateless.simple.ejb.greeter</remote> <ejb-class>samples.ejb.stateless.simple.ejb.greeterejb</ejb-class> <session-type>stateless</session-type> <transaction-type>bean</transaction-type> <security-identity> <use-caller-identity/> </security-identity> </session> </enterprise-beans> </ejb-jar> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 EJB 2.1//EN" "h <!-- Copyright 2004-2005 Sun Microsystems, Inc. Use is subject to license terms. --> All rights reserved. 6

<sun-ejb-jar> <enterprise-beans> <unique-id>1</unique-id> <ejb> <ejb-name>thegreeter</ejb-name> <jndi-name>greeter</jndi-name> </ejb> </enterprise-beans> </sun-ejb-jar> 7