1 / 29

Week 9

Week 9. JSP and JavaBeans. Java Beans. A Java Bean is Java servlet .class file that contains data (properties) and methods that follow certain coding conventions

Télécharger la présentation

Week 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 9 JSP and JavaBeans

  2. Java Beans • A Java Bean is Java servlet .class file that contains data (properties) and methods that follow certain coding conventions • The SUN JavaBean API provides a powerful mechanism for software reuse and automated support (e.g. introspection = beans can tell you about themselves) • Under JSP – the use of Beans is the preferred method of separating static HTML from dynamic Java code and business logic

  3. Example • A JavaBean called GuestBean • The source file would be saved as GuestBean.java • Below shows the top part of the JavaBean code import java.sql.*; public class GuestBean{ String email, firstName, lastName; etc etc

  4. JavaBean structure • ANY Java class can be a JavaBean provided it follows certain conventions • A bean must have fields with corresponding “get” and/or “set” methods • So you need at least one get and/or set method • It follows that any existing Java class can be “converted” to a bean (via of course modifying the source code and removing the main method)

  5. JavaBean structure • A JavaBean can have what are called properties which are essentially either a get or set prefix with a variable name suffix – see next slide • Properties are manipulated using get and set access methods public <datatype> getXXX( ) public void setXXX(<datatype>) • XXX is the property name • Examples follow after the next slide

  6. Bean get and set syntax • The general syntax is get + variableName set + variableName • The variable name will follow the naming conventions being employed by the development team e.g. the first letter of the variable name may be capitalised and the rest lower case

  7. get method • A get method is ALWAYS used to access a property public int getfieldId( ) { return fieldId; } • The property accessed may be a calculated value public double getInterest( ) { return fieldBalance * fieldInterestRate; } A method variables

  8. JSP and JavaBeans • JSPs can use JavaBeans using a set of action tags useBean Action - used to access a JavaBean class instance (object) on the server setProperty Action - used to set the attributes of a JavaBean class instance (object) on the server getProperty Action - used to get the value of an attribute of a JavaBean class instance (object) on the server

  9. JSP useBean Tag • There are standard action JSP tags that allow you to work with pre-defined JavaBeans • The syntax for referencing a JavaBean within a JSP can be of two forms: <jsp:useBean use_bean_attributes /> or <jsp:useBean use_bean_attribute > [option scriptlets and tags] </jsp:useBean>

  10. JSP useBean Tag • The basic syntax for referencing a JavaBean within a JSP is as follows: <jsp:useBean id = "textProvider" class = "exampleBean.HelloJSP1" scope = “request" /> • If an instance of HellpJSP1 does not already exist then one is created Object name used within JSP (case sensitive) Name of the object’s implementation class See next slide

  11. JSP useBean Tag

  12. Other Useful JSP Tags • Include Tag - includes the output of a servlet in a JSP <jsp:include page=“/servlet/ShowAcctBalance” /> • Forward Tag – forwards processing from a JSP to a servlet <jsp:forward page=“/servlet/CheckCreditLimit” />

  13. Getting Bean Properties Other tags allow you to get or set Bean properties (data attributes) … getProperty tag: • JSP code: <jsp:getProperty name="textProvider" property="textMessage"/> • Java Bean (servlet) code: public String getTextMessage() { Date now = new Date(); return "Hello World! ... It is now " + now.toString() + "."; }

  14. Setting Bean Properties Using the setProperty Tag: • JSP code: <jsp:setProperty name=“calcProvider" property=“a” value=“3" /> • Java Bean (servlet) code: public void setA(int value) { a = value; }

  15. Beans and Forms • Beans interact with forms to generate dynamic Web pages • Use form control elements to interact with bean properties

  16. Setting Properties with form Input Fields <jsp:setParameter name=“bean_name” property=“foo”/> • The value assigned to the property is presumed to come from the form • In order for this technique to work, the bean must have a public setter method called setFoo, and this method takes a String object as an argument • This tag requires the match between the property name and the form’s input parameter name

  17. Setting Properties with form Input Fields <jsp:setProperty name=“bean_name” property=“propertyName” param=“inputFieldName”/> • Used when the name of the form parameter and the name of the property do not match • This tag uses the form input field called “inputFieldName” to set the bean’s property called “propertyName”

  18. Setting Properties with Form Input Fields <jsp:setProperty name=“bean_name” property=“*”/> • Looks through all the input fields provided by the form and all the methods provided by the bean, and links them together automatically • You must have methods in the class to handle this

  19. Demo - Books JavaBean • Demo consists of a JSP “setting” data in a JavaBean and then “getting” the data • An HTML form Bookorder.html drives the demo and calls a JSP Confirmbook.jsp • A JSP parses the HTML form and a call is made to the compiled bean – Bean.class (a compiled JavaBean source code file Books.java

  20. BookOrder.html <HTML> <body> <h1>Jonathan's Book Order Form</h1> Book: Java Server Pages How To Guide<br> Version: 1.0<br> Price Per copy: £2.50 <br> <form action="http://localhost:8080/examples/jsp/jcwexamples/confirmbook.jsp" method="post"> Number of copies: <input type="text" name="quantity"><br> <input type="submit" value="Order Book" > </form> </body> </html>

  21. Books.java – the bean package jonathansbeans; public class Books { private String bookName; private int quantity; private String version; private float price; This is the “header” part of the JavaBean source code for Books.java The bean needs to be placed in a folder called jonathanbeans Note that four variables have been declared for this bean

  22. Confirm.jsp – top half <jsp:useBean id="orderedbook" class="jonathansbeans.Books" /> <jsp:setProperty name="orderedbook" property="bookName" value="JSP How To Guide" /> <jsp:setProperty name="orderedbook" property="version" value="1.0" /> <jsp:setProperty name="orderedbook" property="price" value="2.50" /> <jsp:setProperty name="orderedbook" property="quantityofcopies" param="quantity" /> The values are being set in the JavaBean using The setProperty Notes: quantity value has come from the html form

  23. In the bean public void setbookName(String name){ this.bookName=name; } An example of a “setter” – note the parameter being received in the brackets The “this” just means set the current object variable bookName to the value received The this keyword is optional it will work without it

  24. Response back to browser: confirmbook.jsp <HTML> <body> <h1>Your Book Order</h1> <br><br> Book: <jsp:getProperty name="orderedbook" property="bookname"/><br> Version: <jsp:getProperty name="orderedbook" property="version" /><br> Price: £<jsp:getProperty name="orderedbook" property="price" /><br> Quantity: <jsp:getProperty name="orderedbook" property="quantityofcopies" /><br> Total:&nbsp;£<%=orderedbook.getPrice()*orderedbook.getQuantityofcopies() %> <p></p> <a href="BookOrder.html">Return to order form to adjust quantity</a> </body> </html> This shows the call for the “getter” methods in the Bean

  25. In the bean public String getBookName(){ return this.bookName; } An example of a “getter” The this keyword is optional it will work without it

  26. JSP Bean Tags in Action • Lets have a look at the useBean and getProperty tags in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp For JSP source see (use browser’s source view)http://eagle.acadiau.ca/demo/jsp/HelloJSP1_jsp.txt • Note that the example demonstrates how a bean property value can be obtained using the getProperty tag or by an expression: <%= textProvider.getTextMessage() %>

  27. JSP Bean Tags in Action Lets have a look at the setProperty tag in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp • First we set the property values of a bean “calcApB.class” using <jsp:setProperty name="calcProvider" property="a" value="5" /> • Java Bean (servlet) code: public void setA(int value) { a = value; } • Then we ask the bean to calculate A + B <%= calcProvider.calcResult() %>

  28. JSP Bean Tags in Action • Lets have a look at passing parameters from forms within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp • We call another JSP “calcApB.jsp” from an HTML form passing the values of a and b • Within calcApB.jsp we call the bean “calcApB” and pass the values as per the JavaBean API <jsp:setProperty name="calcProvider" property=“*" /> • Then we ask the bean to calculate A + B <%= calcProvider.calcResult() %>

  29. Setting Bean Properties Using a JavaBean API convention: • HTML code: <FORM METHOD=POST ACTION="calcApB.jsp"> Enter the value of A: <INPUT TYPE=TEXT NAME=a SIZE=4><BR> Enter the value of B: <INPUT TYPE=TEXT NAME=b SIZE=4><BR> <P><INPUT TYPE=SUBMIT> </FORM> • calcApB.jsp JSP code: <jsp:useBean id="calcProvider" scope="request" class="exampleBean.calcApB"> <jsp:setProperty name="calcProvider" property="*" /> </jsp:useBean> • Java Bean (servlet) code: public void setA(int value) { a = value; } • public void setB(int value) • { • b = value; • }

More Related