74
USING JAVABEANS COMPONENT IN JSP DOCUMENTS

USING JAVABEANS COMPONENT IN JSP DOCUMENTS

Embed Size (px)

DESCRIPTION

USING JAVABEANS COMPONENT IN JSP DOCUMENTS. References. Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. (Chapter 14, 15, 16). Content. Why use Beans What are Beans Using Beans (setting, getting Bean Properties) - PowerPoint PPT Presentation

Citation preview

USING JAVABEANS COMPONENT

IN JSP DOCUMENTS

References

Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. (Chapter 14, 15, 16)

Content

Why use Beans What are Beans Using Beans (setting, getting Bean Properties) <Jsp:useBean Options> (type, scope) Sharing Beans Setting Bean Properties: Advanced Techniques Examples

Strategies for invoking dynamic code from JSP

Why use Beans ?

No Java syntax Simpler object sharing Convenient correspondence between request

parameters and object properties: The JSP bean constructs greatly simplify the process of reading request parameters, converting from strings, and putting the results inside objects.

What are Beans?

Beans are simply Java classes that are written in a standard format A bean class must have a zero-argument

(default) constructor. A bean class should have no public instance

variables (fields). Persistent values should be accessed through

methods called getXxx and setXxx

Using Beans

jsp:useBean. In the simplest case, this element builds a new bean. It is normally used as follows: <jsp:useBean id="beanName“ class="package.Class" />

Ex: <jsp:useBean id=“simBean“ class=“MyBeans.SimpleBean" />

or<%

MyBeans.SimpleBean simBean = new MyBeans.SimpleBean();

%>

Using Beans

jsp:getProperty. This element reads and outputs the value of a bean property. Reading a property is a shorthand notation for calling a method of the form getXxx. This element is used as follows: <jsp:getProperty name="beanName"

property="propertyName" /> jsp:setProperty. This element modifies a bean

property (i.e., calls a method of the form setXxx). It is normally used as follows: <jsp:setProperty name="beanName"

property="propertyName" value="propertyValue" />

Installing Bean Classes

In the same directories where servlets can be installed WEB-INF/classes/subdirectoryMatchingPackageName

Example: MyWebApp/WEB-INF/classes/MyBeans/BookClass.class

WEB-INF/lib for JAR files.

Example 1: SimpleBean.Java

package MyBeans;public class SimpleBean {

private String msg = "No Message";private int id = 0;public String getMessage() {

return msg;}public void setMessage(String msg) {

this.msg = msg;}public void setId(int iId) {

this.id = iId;}public int getId() {

return this.id;}

}

Example 1: useSimpleBean.jsp

<jsp:useBean id="simpleBean" class="MyBeans.SimpleBean"></jsp:useBean><br>Message:<jsp:getProperty name="simpleBean" property="message"></jsp:getProperty><br>ID:<jsp:getProperty name="simpleBean" property="id"></jsp:getProperty>

<jsp:setProperty name="simpleBean" property="id" value="1"></jsp:setProperty><jsp:setProperty name="simpleBean" property="message" value="New

Message"></jsp:setProperty>

<br><b>After Setting: </b><br>Message:<jsp:getProperty name="simpleBean" property="message"></jsp:getProperty><br>ID:<jsp:getProperty name="simpleBean" property="id"></jsp:getProperty>

Using jsp:useBean Options: type

In most cases, you want the local variable to have the same type as the object being created. In a few cases, however, you might want the variable to be declared to have a type that is a superclass of the actual bean type or is an interface that the bean implements. Use the type attribute to control this declaration, as in the following example.<jsp:useBean id="thread1" class="mypackage.MyClass" type="java.lang.Runnable" />

This use results in code similar to the following being inserted into the _jspService method.java.lang.Runnable thread1 = new myPackage.MyClass();

Using jsp:useBean Options: scope

<jsp:useBean ... scope="page" />: default <jsp:useBean ... scope="request" /> : <jsp:useBean ... scope="session" /> : <jsp:useBean ... scope="application" /> :

Sharing Beans

First, a jsp:useBean element results in a new bean being instantiated only if no bean with the same id and scope can be found. If a bean with the same id and scope is found, the preexisting bean is simply bound to the variable referenced by id.

Second, instead of

<jsp:useBean ... /> you can use

<jsp:useBean ...>statements</jsp:useBean>

Sharing Beans

The point of using the second form is that the statements between the jsp:useBean start and end tags are executed only if a new bean is created, not if an existing bean is used. Because jsp:useBean invokes the default (zero-argument) constructor, you frequently need to modify the properties after the bean is created. To mimic a constructor, however, you should make these modifications only when the bean is first created, not when an existing (and presumably updated) bean is accessed. No problem: multiple pages can contain jsp:setProperty statements between the start and end tags of jsp:useBean; only the page first accessed executes the statements.

Sharing Beans

<jsp:useBean ... scope="page" />: in addition to being bound to a local variable, the bean object should be placed in the PageContext object for the duration of the current request. This indicates that the bean is not shared and thus a new bean will be created for each request.

Sharing Beans

<jsp:useBean ... scope="request" />: in addition to being bound to a local variable, the bean object should be placed in the HttpServletRequest object for the duration of the current request

Sharing Beans

<jsp:useBean ... scope=" session" />: in addition to being bound to a local variable, the bean will be stored in the HttpSession object associated with the current request

Sharing Beans

<jsp:useBean ... scope=“application" />: in addition to being bound to a local variable, the bean will be stored in the ServletContext. The ServletContext is shared by all servlets and JSP pages in the Web application

Example 2: AccessCountBean.java

package coreservlets;

/** Simple bean to illustrate sharing beans through * use of the scope attribute of jsp:useBean. */

public class AccessCountBean {

private String firstPage;private int accessCount = 1;

public String getFirstPage() {

return(firstPage);}

Example 2: AccessCountBean.java

public void setFirstPage(String firstPage) {

this.firstPage = firstPage; }

public int getAccessCount() {

return(accessCount);}

public void setAccessCountIncrement(int increment) {

accessCount = accessCount + increment;}

}

Example 2: SharedCounts1.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>Shared Access Counts: Page 1</TITLE></HEAD><BODY><TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Shared Access Counts: Page 1</TABLE><P><jsp:useBean id="counter" class="coreservlets.AccessCountBean" scope="application"> <jsp:setProperty name="counter" property="firstPage" value="SharedCounts1.jsp" /></jsp:useBean>

Example 2: SharedCounts1.jsp

Of SharedCounts1.jsp (this page),<A HREF="SharedCounts2.jsp">SharedCounts2.jsp</A>, and<A HREF="SharedCounts3.jsp">SharedCounts3.jsp</A>,<jsp:getProperty name="counter" property="firstPage" />was the first page accessed.<P>Collectively, the three pages have been accessed<jsp:getProperty name="counter" property="accessCount" />times.<jsp:setProperty name="counter"

property="accessCountIncrement" value="1" /></BODY></HTML>

Example 2

Setting Bean Properties: Advanced Techniques

For example, the SaleEntry class shown in has an itemID property (a String), a numItems property (an int), a discountCode property (a double), and two read-only properties, itemCost and totalCost

Setting Bean Properties: Advanced Techniques

<jsp:useBean id="entry" class="coreservlets.SaleEntry" />

<jsp:setProperty name="entry" property="itemID" value='<%= request.getParameter("itemID") %>' /> ???

Setting Bean Properties: Advanced Techniques – SaleEntry Class

package coreservlets;/** Simple bean to illustrate the various forms * of jsp:setProperty. */public class SaleEntry {

private String itemID = "unknown";private double discountCode = 1.0;

private int numItems = 0;

public String getItemID() { return(itemID); } public void setItemID(String itemID) { if (itemID != null) { this.itemID = itemID; } else { this.itemID = "unknown"; } }

Setting Bean Properties: Advanced Techniques - SaleEntry Class

public double getDiscountCode() { return(discountCode); } public void setDiscountCode(double discountCode) { this.discountCode = discountCode; }

public int getNumItems() { return(numItems); } public void setNumItems(int numItems) { this.numItems = numItems; }

Setting Bean Properties: Advanced Techniques - SaleEntry Class

public double getItemCost() { double cost; if (itemID.equals("a1234")) { cost = 12.99*getDiscountCode(); } else { cost = -9999; } return(roundToPennies(cost)); }

private double roundToPennies(double cost) { return(Math.floor(cost*100)/100.0); }

public double getTotalCost() { return(getItemCost() * getNumItems()); }}

Setting Bean Properties: Advanced Techniques - SaleEntry1-Form.jsp

Setting Bean Properties: Advanced Techniques - SaleEntry1-Form.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>Invoking SaleEntry1.jsp</TITLE></HEAD><BODY><CENTER><TABLE BORDER=5> <TR><TH CLASS="TITLE"> Invoking SaleEntry1.jsp</TABLE><FORM ACTION="SaleEntry1.jsp"> Item ID: <INPUT TYPE="TEXT" NAME="itemID"><BR> Number of Items: <INPUT TYPE="TEXT" NAME="numItems"><BR> Discount Code: <INPUT TYPE="TEXT" NAME="discountCode"><P> <INPUT TYPE="SUBMIT" VALUE="Show Price"></FORM></CENTER></BODY></HTML>

Setting Bean Properties: Advanced Techniques - SaleEntry1.jsp

<jsp:useBean id="entry" class="coreservlets.SaleEntry" /> <jsp:setProperty

name="entry"

property="itemID"

value='<%= request.getParameter("itemID") %>' />

<% int numItemsOrdered = 1;

try { numItemsOrdered = Integer.parseInt(request.getParameter("numItems")); } catch(NumberFormatException nfe) {}

%>

<jsp:setProperty name="entry" property="numItems"

value="<%= numItemsOrdered %>" />

Setting Bean Properties: Advanced Techniques - SaleEntry1.jsp

<% double discountCode = 1.0;

try {

String discountString = request.getParameter("discountCode"); discountCode = Double.parseDouble(discountString);

}

catch(NumberFormatException nfe) {}

%>

<jsp:setProperty name="entry" property="discountCode" value="<%= discountCode %>" />

Setting Bean Properties: Advanced Techniques - SaleEntry1.jsp

…<TD><jsp:getProperty name="entry" property="itemID" /> <TD>$<jsp:getProperty name="entry" property="itemCost" /> <TD><jsp:getProperty name="entry" property="numItems" /> <TD>$<jsp:getProperty name="entry" property="totalCost" />

Setting Bean Properties: Advanced Techniques

<jsp:setProperty name="entry" property="numItems" param="numItems" />

Instead of using the value attribute, you use param to name an input parameter. The value of the named request parameter is automatically used as the value of the bean property, and type conversions from String to primitive types (byte, int, double, etc.) and wrapper classes (Byte, Integer, Double, etc.) are automatically performed.

Setting Bean Properties: Advanced Techniques

You can simplify the code slightly if the request parameter name and the bean property name are the same.

<jsp:setProperty name="entry" property="numItems" />

<%-- param="numItems" is assumed. --%>

Setting Bean Properties: Advanced Techniques – SaleEntry2.jsp

...

<jsp:useBean id="entry" class="coreservlets.SaleEntry" />

<jsp:setProperty name="entry" property="itemID" param="itemID" />

<jsp:setProperty name="entry" property="numItems" param="numItems" />

<jsp:setProperty name="entry" property="discountCode" param="discountCode" />

...

Setting Bean Properties: Advanced Techniques – SaleEntry3.jsp

Associating All Properties with Request Parameters

<jsp:setProperty name="entry" property="*" />

...

<jsp:useBean id="entry" class="coreservlets.SaleEntry" />

<jsp:setProperty name="entry" property="*" />

...

Warnings

No action is taken when an input parameter is missing.

Automatic type conversion does not guard against illegal values as effectively as does manual type conversion

Bean property names and request parameters are case sensitive. So, the property name and request parameter name must match exactly.

INTEGRATING SERVLETS AND JSP:

THE MVC MODEL ARCHITECTURE

References

Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. (Chapter 14, 15, 16)

Content

Understanding the benefits of MVC Using RequestDispatcher to implement MVC sendRedirect method of Response & forward method

of RequestDispatcher Summarizing MVC Code JSP/Servlet & JavaBean MVC Examples

Strategies for invoking dynamic code from JSP

Understanding the benefits of MVC

Servlets are good at data processing: reading and checking data, communicating with databases, invoking business logic, and so on.

JSP pages are good at presentation: building HTML to represent the results of requests.

How to combine servlets and JSP pages to best make use of the strengths of each technology?

Understanding the benefits of MVC

The original request is handled by a servlet Model: The servlet invokes the business-logic and

data-access code and creates beans to represent the results. That's the model.

View: The servlet decides which JSP page is appropriate to present those particular results and forwards the request there (the JSP page is the view)

Controller: The servlet decides what business logic code applies and which JSP page should present the results (the servlet is the controller).

Implementing MVC with RequestDispatcher

Define beans to represent the data: Your first step is define beans to represent the results that will be presented to the user.

Use a servlet to handle requests: In most cases, the servlet reads request parameters.

Populate the beans: The servlet invokes business logic, or data-access code to obtain the results. The results are placed in the beans that were defined in step 1

Implementing MVC with RequestDispatcher

Store the bean in the request, session, or servlet context: The servlet calls setAttribute on the request, session, or servlet context objects to store a reference to the beans that represent the results of the request.

Forward the request to a JSP page: The servlet determines which JSP page is appropriate to the situation and uses the forward method of RequestDispatcher to transfer control to that page.

Extract the data from the beans: The JSP page accesses beans with jsp:useBean and a scope. The page then uses jsp:getProperty to output the bean properties

sendRedirect & RequestDispatcher

sendRedirect requires the client to reconnect to the new resource, whereas the forward method of RequestDispatcher is handled completely on the server.

sendRedirect results in a different final URL, whereas with forward, the URL of the original servlet is maintained.

sendRedirect does not automatically preserve all of the request data; forward does.

Applying MVC Example: Random Number

package coreservlets;public class NumberBean {

private double num = 0;

public NumberBean(double number) {setNumber(number);

}

public double getNumber() { return(num); }

public void setNumber(double number) { num = number; }}

Applying MVC Example: Random Number

package coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/** Servlet that generates a random number, stores it in a bean, * and forwards to JSP page to display it. */public class RandomNumberServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{NumberBean bean = new NumberBean(Math.random());request.setAttribute("randomNum", bean);String address = "/WEB-INF/mvc-sharing/RandomNum.jsp";

RequestDispatcher dispatcher = request.getRequestDispatcher(address);

dispatcher.forward(request, response); }}

Applying MVC Example: Random Number

<jsp:useBean id="randomNum" type="coreservlets.NumberBean"

scope="request" />

<H2>Random Number:

<jsp:getProperty name="randomNum" property="number" />

Applying MVC Example: Random Number

Note

If your JSP pages only make sense in the context of servlet-generated data, place the pages under the WEB-INF directory. That way, servlets can forward requests to the pages, but clients cannot access them directly.

Summarizing MVC Code

Summarize the code that would be used for request-based,session-based,and application-based MVC approaches

Summarizing MVC Code Request-Based Data Sharing

The servlet stores the beans in the HttpServletRequest Servlet

ValueObject value = new ValueObject(...); request.setAttribute("key", value);

RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response);

JSP Page<jsp:useBean id="key" type="somePackage.ValueObject"

scope="request" />

<jsp:getProperty name="key" property="someProperty" />

Summarizing MVC Code Session-Based Data Sharing

The servlet stores the beans in the HttpSession, where they are accessible to the same client in the destination JSP page or in other pages.

ServletValueObject value = new ValueObject(...); HttpSession session = request.getSession(); session.setAttribute("key", value); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response);

JSP Page<jsp:useBean id="key" type="somePackage.ValueObject" scope="session" /> <jsp:getProperty name="key" property="someProperty" />

Summarizing MVC Code Application-Based Data Sharing

The servlet stores the beans in the ServletContext, where they are accessible to any servlet or JSP page in the Web application. To guarantee that the JSP page extracts the same data that the servlet inserted, you should synchronize your code as below.

Summarizing MVC Code Application-Based Data Sharing

Servletsynchronized(this) {

ValueObject value = new ValueObject(...); getServletContext().setAttribute("key", value); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/SomePage.jsp");

dispatcher.forward(request, response);

}

JSP Page<jsp:useBean id="key" type="somePackage.ValueObject" scope="application" />

<jsp:getProperty name="key" property="someProperty" />

Applying MVC Example: Bank Account Balances

package coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

/** Servlet that reads a customer ID and displays * information on the account balance of the customer who has that ID. */

public class ShowBalance extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{ BankCustomer customer =

BankCustomer.getCustomer(request.getParameter("id")); String address; if (customer == null) { address = "/WEB-INF/bank-account/UnknownCustomer.jsp"; }

Applying MVC Example: Bank Account Balances

else if (customer.getBalance() < 0) { address =

"/WEB-INF/bank-account/NegativeBalance.jsp"; request.setAttribute("badCustomer", customer); } else if (customer.getBalance() < 10000) { address =

"/WEB-INF/bank-account/NormalBalance.jsp"; request.setAttribute("regularCustomer", customer); } else { address = "/WEB-INF/bank-account/HighBalance.jsp"; request.setAttribute("eliteCustomer", customer); }

RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response);

}}

Applying MVC Example: Bank Account Balances

NegativeBalance.jsp…

<jsp:useBean id="badCustomer“ type="coreservlets.BankCustomer"

scope="request" />

Watch out,

<jsp:getProperty name="badCustomer" property="firstName" />,

we know where you live.

<P>

Pay us the

$<jsp:getProperty name="badCustomer" property="balanceNoSign" />

you owe us before it is too late!

Applying MVC Example: Bank Account Balances

Applying MVC Example: Bank Account Balances

NormalBalance.jsp…<jsp:useBean id="regularCustomer“ type="coreservlets.BankCustomer" scope="request" /><UL> <LI>First name: <jsp:getProperty name="regularCustomer" property="firstName" /> <LI>Last name: <jsp:getProperty name="regularCustomer" property="lastName" /> <LI>ID: <jsp:getProperty name="regularCustomer" property="id" /> <LI>Balance: $<jsp:getProperty name="regularCustomer" property="balance" /></UL>…

Applying MVC Example: Bank Account Balances

Applying MVC Example: Bank Account Balances

<jsp:useBean id="eliteCustomer" type="coreservlets.BankCustomer" scope="request" />It is an honor to serve you,<jsp:getProperty name="eliteCustomer" property="firstName" /><jsp:getProperty name="eliteCustomer" property="lastName" />!

<P>Since you are one of our most valued customers, we would liketo offer you the opportunity to spend a mere fraction of your $<jsp:getProperty name="eliteCustomer" property="balance" />on a boat worthy of your status. Please visit our boat store formore information.

Applying MVC Example: Bank Account Balances

Applying MVC Example: Bank Account Balances

Applying MVC Example: Show Name (Session-Based Sharing )

Our goal is to display users' first and last names. If the users fail to tell us their name, we want to use whatever name they gave us previously. If the users do not explicitly specify a name and no previous name is found, a warning should be displayed. Data is stored for each client, so session-based sharing is appropriate.

Applying MVC Example: Show Name (Session-Based Sharing )

package coreservlets;

public class NameBean {private String firstName = "Missing first name";private String lastName = "Missing last name";

public NameBean() {}

public NameBean(String firstName, String lastName) { setFirstName(firstName); setLastName(lastName); }

public String getFirstName() { return(firstName); }

Applying MVC Example: Show Name (Session-Based Sharing )

public void setFirstName(String newFirstName) { firstName = newFirstName; }

public String getLastName() { return(lastName); }

public void setLastName(String newLastName) { lastName = newLastName; }}

Applying MVC Example: Show Name (Session-Based Sharing )

package coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

/** Reads firstName and lastName request parameters and forwards * to JSP page to display them. Uses session-based bean sharing * to remember previous values. */

public class RegistrationServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession session = request.getSession(); NameBean nameBean = (NameBean)session.getAttribute("nameBean");

Applying MVC Example: Show Name (Session-Based Sharing )

if (nameBean == null) { nameBean = new NameBean(); session.setAttribute("nameBean", nameBean); } String firstName = request.getParameter("firstName"); if ((firstName != null) && (!firstName.trim().equals(""))) { nameBean.setFirstName(firstName); } String lastName = request.getParameter("lastName"); if ((lastName != null) && (!lastName.trim().equals(""))) { nameBean.setLastName(lastName); } String address = "/WEB-INF/mvc-sharing/ShowName.jsp"; RequestDispatcher dispatcher =

request.getRequestDispatcher(address); dispatcher.forward(request, response); }}

Applying MVC Example: Show Name (Session-Based Sharing )

ShowName.jsp…

<jsp:useBean id="nameBean" type="coreservlets.NameBean"

scope="session" />

<H2>First Name:

<jsp:getProperty name="nameBean" property="firstName" /></H2>

<H2>Last Name:

<jsp:getProperty name="nameBean" property="lastName" /></H2>