Writing Enterprise Applications with J2EE (Fifth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Slides:



Advertisements
Similar presentations
Price List Utilities.
Advertisements

Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT211/3 Internet Application Development JSP: Processing User input.
Writing Enterprise Applications with J2EE (Sixth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Internet Technologies 1 Master of Information System Management Java Server Faces Model/View/Controller Design Pattern for Web Development Slides.
Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
DT228/3 Web Development Java Beans. Intro A major problem with JSP is tendency to mix java code with HTML  -  web designer write the HTML and programmer.
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Writing Enterprise Applications with J2EE (Seventh lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
Apache Struts Technology A MVC Framework for Java Web Applications.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10 th March 2005 Bogdan L. Vrusias
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3. Using Beans: Basic Tasks 4. Sharing Beans 5. Practice.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Jordan Anastasiade. All rights reserved.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Using Client-Side Scripts to Enhance Web Applications 1.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CSCI 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Struts Framework Day-2 Ashok Chakravarti. DataSource Usage Sample Struts-config.xml …
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 14 Using JavaBeans Components in JSP Documents.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
SDJ INFOSOFT PVT. LTD. 2 BROWSERBROWSER JSP JavaBean DB Req Res Application Layer Enterprise server/Data Sources.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
Apache Struts Technology A MVC Framework for Java Web Applications.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
JSP java server pages.
Structure of a web application
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
JavaBeans and JSP CS-422.
Introduction to Java Bean
Introduction to Java Bean
Java Server Pages (JSP)
Java Server Pages B.Ramamurthy.
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

Writing Enterprise Applications with J2EE (Fifth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Using JavaBeans with EJBs Rationale for using JavaBeans We can put a JavaBean between the JSP page and CalcBean session bean to obtain the Model, View, Controller (MVC) separation. MVC is a design pattern: the Model provides the application business logic, the View is its screen presentation, and the Controller is an object that manages user interactions with the View. In the previous example, the HTML and JSP pages both provide the View and manage user interactions with the data (Controller). The entity and session bean ( BonusBean and CalcBean ) are the application objects (or Model).

Separation of View and Controller In the current example, we are going to use a JSP page for the screen presentation (View), a JavaBean to manage what happens when the user interacts with the View (Controller), and the entity and session beans for the application objects (Model). Separating the Controller from the View lets the JavaBean serve as a wrapper for the session bean and gives the example a much cleaner MVC separation. An application that uses clear design patterns is easier to update, maintain, and manage.

Steps towards MVC Separation Create bonus.jsp Create the JavaBeans Class –Bean Properties Remove the WAR File Create New WAR FIle Verify and Deploy the J2EE Application Run the J2EE Application

Merging bonus.html and Bonus.jsp In the previous example, the HTML form calls the JSP page when the user clicks the Submit button on the HTML page. Another way to create the user interface is with one single JSP page that includes the HTML form, JSP scriptlets, and JSP-specific tags for interacting with the JavaBean. When the JSP page loads, the HTML form is displayed and the scriptlet and JSP-specific tags are executed. After the user enters some data and clicks the Submit button, the HTML form is redisplayed and the scriptlet and JSP-specific tags execute again with the supplied data. This is because the ACTION parameter for the HTML form cursively calls the JSP page itself:......

Create Bonus.jsp The code for bonus.jsp is now simplified, because the code to look up the session bean and calculate the bonus is now in the JavaBean. The first part of the file contains the HTML code to create the form. The code to pass the HTML form data to the JavaBean is in the second part of the file. Bonus Calculation Bonus Calculation Enter social security Number: Enter Multiplier:...

Bonus.jsp (cont.)... <% sMult = request.getParameter("MULTIPLIER"); ssec = request.getParameter("SOCSEC"); %> "/> Social security number retrieved: Bonus Amount retrieved: Error messages:

Specify the JavaBean The following HTML tag specifies the JavaBean being used in this example. The id parameter defines an alias to use to reference the JavaBean, and the class parameter specifies the JavaBeans class. In this example the id is jbonus and the class is JBonusBean. <jsp:useBean id = "jbonus" class = "JBonusBean"/>

Get the Data The following JSP scriptlets retrieve the user-supplied data from the HTML form input fields. The multiplier is stored in the sMult String variable, and the social security number is stored in the ssec String variable. <% sMult = request.getParameter("MULTIPLIER"); ssec = request.getParameter("SOCSEC"); %>

Pass Data to the JavaBean The following tags set two properties in the JavaBean. A property is a private field in the JavaBean class. The first line uses the jsp:setProperty name tag to set the strMult field in the JBonusBean class (aliased by the jbonus id ) to the value stored in the sMult variable. The second line performs a similar operation for the socsec field in the JBonusBean class. The value=" " expression sends the data contained in the ssec variable to the socsec field in the JavaBean. "/>

Retrieve Data from the JavaBean Retrieving data from a JavaBean is similar to sending data to it. You use the jsp:getProperty name tag and indicate the property (private field) whose data you want to get. The following getProperty name tag retrieves the data stored in the socsec private field of the JBonusBean class (aliased by the jbonus id ). Social security number retrieved:

Create the JavaBeans Class A Java bean looks just like any ordinary Javaclass. But to be a bean, a Java class must follow a set of simple naming and design conventions. Because beans follow the JavaBeans specification, they can be accessed and managed by other programs and tools that follow the same conventions. In the previous example with bonus.jsp, HTML tags and JSP scriptlets are used to get and set data in the private fields of the JBonusBean class. This is possible because JBonusBean follows the JavaBeans naming and design conventions.

The JBonusBean class (I) import javax.naming.*; import javax.rmi.PortableRemoteObject; import Beans.*; public class JBonusBean { private String strMult, socsec, message; private double bonusAmt; CalcHome homecalc; public JBonusBean() { try{ InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("calcs"); homecalc = (CalcHome)PortableRemoteObject.narrow(objref, CalcHome.class); } catch (javax.naming.NamingException e) {e.printStackTrace(); } }

The JBonusBean class (II) public double getBonusAmt() { if(strMult != null){ Integer integerMult = new Integer(strMult); int multiplier = integerMult.intValue(); try { double bonus = ; Calc theCalculation = homecalc.create(); Bonus theBonus = theCalculation.calcBonus(multiplier, bonus, socsec); Bonus record = theCalculation.getRecord(socsec); bonusAmt = record.getBonus(); socsec = record.getSocSec(); } catch (javax.ejb.DuplicateKeyException e) { message = e.getMessage(); } catch (javax.ejb.CreateException e) { e.printStackTrace(); } catch (java.rmi.RemoteException e) { e.printStackTrace(); } return this.bonusAmt; } else { this.bonusAmt = 0; this.message = "None."; return this.bonusAmt;} } public String getMessage(){ return this.message; } public String getSocsec(){ return this.socsec; } public String getStrMult(){ return this.strMult; } public void setSocsec(String socsec) { this.socsec = socsec; } public void setStrMult(String strMult) { this.strMult = strMult; } }

Bean Properties Bean properties define the data accessible to other programs and tools through get and set methods.. Properties are actually private class fields that should always be private and only accessible through get and set methods. The following code segment shows the private properties for the JBonusBean class. This class has a corresponding get method for each field and corresponding set methods for the strMult and socsec fields. public class JBonusBean { private String strMult, socsec, message; private double bonusAmt;...

JBonusBean constructor The JBonusBean constructor looks up the session Bean. public JBonusBean() { try{ InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("calcs"); homecalc =(CalcHome)PortableRemoteObject.narrow( objref, CalcHome.class); } catch (javax.naming.NamingException e) { e.printStackTrace(); }

Set Methods Setter methods set properties (private fields) to specified values. setSocsec and setStrMult are in charge ofr setting the socsec and strMult private fields (JavaBean properties). The values used to set the socsec and strMult properties come from the setProperty name tags in the JSP page. The J2EE server uses the information in the following setProperty name tags to locate the corresponding set methods in the JBonusBean (aliased by the jbonus id ): Similar observations apply to “get” methods. <jsp:setProperty name = "jbonus" property="strMult“ value=" "/> <jsp:setProperty name = "jbonus" property="socsec" value=" " />