SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler offline
© 2010 Marty Hall
Integrating Servlets and JSP:
The Model View Controller
(MVC) Architecture(MVC) Architecture
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.2
© 2010 Marty Hall
For live Java EE training, please see training courses
at http://courses.coreservlets.com/.at http://courses.coreservlets.com/.
Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo,
Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),
Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g
Hibernate/JPA, and customized combinations of topics.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP and this tutorial Available at public
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets and JSP, and this tutorial. Available at public
venues, or customized versions can be held on-site at your
organization. Contact hall@coreservlets.com for details.
Agenda
• Understanding the benefits of MVC
• Using RequestDispatcher to implement MVC
• Forwarding requests from servlets to JSP
pages
• Handling relative URLs
• Choosing among different display options
• Comparing data-sharing strategies
4
© 2010 Marty Hall
MVC Motivation
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.5
Uses of JSP Constructs
• Scripting elements calling servletSimple Scripting elements calling servlet
code directly
• Scripting elements calling servlet
p
Application
g g
code indirectly (by means of utility
classes)
B• Beans
• Servlet/JSP combo (MVC)
MVC ith JSP i l• MVC with JSP expression language
• Custom tags
MVC ith b t t d
Complex
Application
• MVC with beans, custom tags, and
a framework like Struts or JSF
6
Why Combine Servlets & JSP?
• Typical picture: use JSP to make it easier to
d l d i i h HTMdevelop and maintain the HTML content
– For simple dynamic code, call servlet code from
scripting elementsscripting elements
– For slightly more complex applications, use custom
classes called from scripting elements
– For moderately complex applications,
use beans and custom tags
• But that’s not enough• But, that s not enough
– For complex processing, starting with JSP is awkward
– Despite the ease of separating the real code into separatep p g p
classes, beans, and custom tags, the assumption behind
JSP is that a single page gives a single basic look
7
Possibilities for Handling a
Single RequestSingle Request
• Servlet only. Works well when:
O i bi E i– Output is a binary type. E.g.: an image
– There is no output. E.g.: you are doing forwarding or redirection as
in Search Engine example.
– Format/layout of page is highly variable. E.g.: portal.
• JSP only. Works well when:
– Output is mostly character data. E.g.: HTMLp y g
– Format/layout mostly fixed.
• Combination (MVC architecture). Needed when:
A i l t ill lt i lti l b t ti ll diff t– A single request will result in multiple substantially different-
looking results.
– You have a large development team with different team members
doing the Web development and the business logicdoing the Web development and the business logic.
– You perform complicated data processing, but have a relatively
fixed layout.
8
MVC Misconceptions
• An elaborate framework is necessary
– Frameworks are sometimes useful
• Struts
• JavaServer Faces (JSF)JavaServer Faces (JSF)
– They are not required!
• Implementing MVC with the builtin RequestDispatcher
works very well for most simple and moderately complexworks very well for most simple and moderately complex
applications
• MVC totally changes your overall system
design
– You can use MVC for individual requests
Thi k f it th MVC h t th– Think of it as the MVC approach, not the
MVC architecture
• Also called the Model 2 approach9
© 2010 Marty Hall
Beans
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.10
Review: Beans
• Java classes that follow certain conventions
– Must have a zero-argument (empty) constructor
• You can satisfy this requirement either by explicitly
defining such a constructor or by omitting all constructorsg y g
• In this version of MVC, it is not required to have zero arg
constructor if you only instantiate from Java code
– Should have no public instance variables (fields)Should have no public instance variables (fields)
• I hope you already follow this practice and use accessor
methods instead of allowing direct access to fields
Persistent values should be accessed through methods– Persistent values should be accessed through methods
called getXxx and setXxx
• If class has method getTitle that returns a String, class
i id t h St i t d i lis said to have a String property named title
• Boolean properties can use isXxx instead of getXxx
11
Bean Properties: Examples
Method Names Property Name Example JSP Usage
getFirstName
setFirstName
firstName <jsp:getProperty … property="firstName"/>
<jsp:setProperty … property="firstName"/>
${customer.firstName}
isExecutive
setExecutive
(boolean property)
executive <jsp:getProperty … property="executive"/>
<jsp:setProperty … property="executive"/>
${customer.executive}
getExecutive
setExecutive
(boolean property)
executive <jsp:getProperty … property="executive"/>
<jsp:setProperty … property="executive"/>
${customer.executive}
getZIP
setZIP
ZIP <jsp:getProperty … property="ZIP"/>
<jsp:setProperty … property="ZIP"/>
${address.ZIP}
12
Note 1: property name does not exist anywhere in your code. It is just a shortcut for the method name.
Note 2: property name is derived only from method name. Instance variable name is irrelevant.
Example: StringBean
package coreservlets;
public class StringBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
hithis.message = message;
}
}
• Beans installed in normal Java directory
– Eclipse: src/folderMatchingPackage
l d / / l /f ld h k– Deployed: …/WEB-INF/classes/folderMatchingPackage
• Beans (and utility classes) must always be in packages!
13
© 2010 Marty Hall
Basic MVC Design
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.14
MVC Flow of Control
HTML or JSP
Java Code
(Business Logic)
R l
Form
Servletsubmit form
(Form ACTION matches
url-pattern of servlet)
Results
(beans)
(Store beans in request,
session, or application scope)
request.setAttribute("customer",
JSP
q ( ,
currentCustomer);
JSP1
JSP2
JSP3
(Extract data from beans
and put in output)
15
p p )
${customer.firstName}
Implementing MVC with
RequestDispatcherRequestDispatcher
1. Define beans to represent result data
– Ordinary Java classes with at least one getBlah method
2. Use a servlet to handle requests
S l d h k f i i– Servlet reads request parameters, checks for missing
and malformed data, calls business logic, etc.
3. Obtain bean instances3. Obtain bean instances
– The servlet invokes business logic (application-specific
code) or data-access code to obtain the results.
4. Store the bean in the request, session, or
servlet context
Th l t ll tAtt ib t th t i– 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.16
Implementing MVC with
RequestDispatcher (Continued)RequestDispatcher (Continued)
5. 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.RequestDispatcher to transfer control to that page.
6. Extract the data from the beans.
– JSP 1.2: the JSP page accesses beans with jsp:useBeanp g j p
and a scope matching the location of step 4. The page
then uses jsp:getProperty to output the bean properties.
JSP 2 0: the JSP page uses– JSP 2.0: the JSP page uses
${nameFromServlet.property} to output bean properties
– Either way, the JSP page does not create or modify the
bean; it merely extracts and displays data that the servlet
created.
17
Request Forwarding Example
public void doGet(HttpServletRequest request,
HttpServletResponse response)p p p )
throws ServletException, IOException {
... // Do business logic and get data
String operation = request.getParameter("operation");
if (operation == null) {if (operation == null) {
operation = "unknown";
}
String address;
if (operation.equals("order")) {
address = "/WEB-INF/Order.jsp";
} else if (operation.equals("cancel")) {
address = "/WEB-INF/Cancel jsp";address = /WEB INF/Cancel.jsp ;
} else {
address = "/WEB-INF/UnknownOperation.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}18
jsp:useBean in MVC vs.
in Standalone JSP Pagesin Standalone JSP Pages
• The JSP page should not create
h bjthe objects
– The servlet, not the JSP page, should create all the data
objects So to guarantee that the JSP page will not createobjects. So, to guarantee that the JSP page will not create
objects, you should use
<jsp:useBean ... type="package.Class" />
i d finstead of
<jsp:useBean ... class="package.Class" />
• The JSP page should not modify
the objects
S h ld j b– So, you should use jsp:getProperty but not
jsp:setProperty.
19
© 2010 Marty Hall
Scopes:
request, session, and
application (ServletContext)application (ServletContext)
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.20
Reminder: jsp:useBean
Scope Alternatives (JSP 1 2 Only!)Scope Alternatives (JSP 1.2 Only!)
• request
– <jsp:useBean id="..." type="..." scope="request" />
• session
j B id " " " " " i " /– <jsp:useBean id="..." type="..." scope="session" />
• application
<jsp:useBean id=" " type=" " scope="application" />– <jsp:useBean id= ... type= ... scope= application />
• page
– <jsp:useBean id=" " type=" " scope="page" /><jsp:useBean id ... type ... scope page />
or just
<jsp:useBean id="..." type="..." />
Thi i t d i MVC (M d l 2) hit t– This scope is not used in MVC (Model 2) architecture
21
Request-Based Data Sharing
• Servlet
ValueObject value = LookupService.findResult(...);
request.setAttribute("key", value);
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"<jsp:useBean id key type somePackage.ValueObject
scope="request" />
<jsp:getProperty name="key" property="someProperty" />
JSP 2 0
Name chosen by the servlet
• JSP 2.0
${key.someProperty}
22
Name chosen by the servlet.
Name of accessor method, minus the
word "get", with next letter changed
to lower case.
Request-Based Data Sharing:
Simplified ExampleSimplified Example
• Servlet
C t C t
Assume that the Customer constructor
handles missing/malformed data.
Customer myCustomer =
Lookup.findCust(request.getParameter("customerID"));
request.setAttribute("customer", myCustomer);
RequestDispatcher dispatcher =RequestDispatcher dispatcher
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="customer" type="somePackage.Customer"j p yp g
scope="request" />
<jsp:getProperty name="customer" property="firstName"/>
• JSP 2 0• JSP 2.0
${customer.firstName}
23
Note: the Customer class must
have a method called “getFirstName”.
Session-Based Data Sharing
• Servlet
l bj l k i fi d l ( )ValueObject value = LookupService.findResult(...);
HttpSession session = request.getSession();
session.setAttribute("key", value);
RequestDispatcher dispatcher =RequestDispatcher dispatcher
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"j p y yp g j
scope="session" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2 0JSP 2.0
${key.someProperty}
24
Session-Based Data Sharing:
VariationVariation
• Redirect to page instead of forwarding to it
U dR di i d f R Di h f d– Use response.sendRedirect instead of RequestDispatcher.forward
• Distinctions: with sendRedirect:
– User sees JSP URL (user sees only servlet URL with( y
RequestDispatcher.forward)
– Two round trips to client (only one with forward)
• Advantage of sendRedirectAdvantage of sendRedirect
– User can visit JSP page separately
• User can bookmark JSP page
Disadvantages of sendRedirect• Disadvantages of sendRedirect
– Two round trips to server is more expensive
– Since user can visit JSP page without going through servlet first,
b d i h b il blbean data might not be available
• So, JSP page needs code to detect this situation
25
ServletContext-Based Data
Sharing (Rare)Sharing (Rare)
• Servlet
synchronized(this) {
ValueObject value = SomeLookup.findResult(...);
getServletContext().setAttribute("key", value);
Req estDispatcher dispatcherRequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher forward(request response);dispatcher.forward(request, response);
}
• JSP 1.2
<j B id "k " t " P k V l Obj t"<jsp:useBean id="key" type="somePackage.ValueObject"
scope="application" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2.0
${key.someProperty}
26
Relative URLs in JSP Pages
• Issue:
– Forwarding with a request dispatcher is transparent to the
client. Original URL is only URL browser knows about.
• Why does this matter?• Why does this matter?
– What will browser do with tags like the following?
<img src="foo.gif" …>
<link rel="stylesheet"
href="my-styles.css"href my styles.css
type="text/css">
<a href="bar.jsp">…</a>
– Browser treats addresses as relative to servlet URL
27
© 2010 Marty Hall
Examples
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.28
Applying MVC:
Bank Account BalancesBank Account Balances
• Bean
B kC t– BankCustomer
• Business Logic
– BankCustomerLookup
• Servlet that populates bean and forwards to
appropriate JSP page
– Reads customer ID calls BankCustomerLookup’sReads customer ID, calls BankCustomerLookup s
data-access code to obtain BankCustomer
– Uses current balance to decide on appropriate result page
• JSP pages to display results• JSP pages to display results
– Negative balance: warning page
– Regular balance: standard page
Hi h b l ith d ti t dd d– High balance: page with advertisements added
– Unknown customer ID: error page
29
Bank Account Balances:
Servlet CodeServlet Code
public class ShowBalance extends HttpServlet {
public void doGet(HttpServletRequest request,p ( p q q ,
HttpServletResponse response)
throws ServletException, IOException {
BankCustomer currentCustomer =
BankCustomerLookup getCustomerBankCustomerLookup.getCustomer
(request.getParameter("id"));
request.setAttribute("customer", currentCustomer);
String address;
if (currentCustomer == null) {
address =
"/WEB-INF/bank-account/UnknownCustomer.jsp";
} else if (currentCustomer getBalance() < 0) {} else if (currentCustomer.getBalance() < 0) {
address =
"/WEB-INF/bank-account/NegativeBalance.jsp";
} ...
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
30
Bank Account Balances:
BeanBean
public class BankCustomer {
private final String id, firstName, lastName;p g , , ;
private final double balance;
public BankCustomer(String id,
String firstName
Since the constructor is called from Java only
(never from JSP), the requirement for a zero-arg
constructor is eliminated. Also, since bean state
is set only with constructor, rather than with
jsp:setProperty, we can eliminate setter
methods and make the class immutable.
String firstName,
String lastName,
double balance) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}}
// Getters for four instance variables. No setters.
public double getBalanceNoSign() {
return(Math.abs(balance));
}
}31
Bank Account Balances:
Business LogicBusiness Logic
public class BankCustomerLookup {
private static Map<String,BankCustomer> customers;private static Map<String,BankCustomer> customers;
static {
// Populate Map with some sample customersp p p
}
…
public static BankCustomer getCustomer(String id) {
return(customers.get(id));
}}
}
32
Bank Account Balances:
Input FormInput Form
…
<fieldset><fieldset>
<legend>Bank Account Balance</legend>
<form action="./show-balance">
Customer ID: <input type="text" name="id"><br>p yp
<input type="submit" value="Show Balance">
</form>
</fieldset> For the servlet, use the address http://host/appName/show-balance that is set via url-pattern in web.xml
…
33
Bank Account Balances:
JSP 1 2 Code (Negative Balance)JSP 1.2 Code (Negative Balance)
…
<BODY>
<TABLE BORDER=5 ALIGN="CENTER">
<TR><TH CLASS="TITLE">
We Know Where You Live!</TABLE>
<P><P>
<IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">
<jsp:useBean id="customer"
type="coreservlets.BankCustomer"
scope="request" />
Watch out,
<jsp:getProperty name="customer"
property="firstName" />property= firstName />,
we know where you live.
<P>
Pay us the $<jsp:getProperty name="customer"
/property="balanceNoSign" />
you owe us before it is too late!
</BODY></HTML>
34
Bank Account Balances:
JSP 2 0 Code (Negative Balance)JSP 2.0 Code (Negative Balance)
…
<BODY><BODY>
<TABLE BORDER=5 ALIGN="CENTER">
<TR><TH CLASS="TITLE">
We Know Where You Live!</TABLE>
<P>
<IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">
Watch out, ${customer.firstName},
we know where you live.
<P>
Pay us the $${customer.balanceNoSign}
b f it i t l t !you owe us before it is too late!
</BODY></HTML>
35
Bank Account Balances:
web xmlweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2 4" ><web app version 2.4 ...>
<!-- Use the URL http://host/app/show-balance instead of
http://host/app/servlet/coreservlets.ShowBalance -->
<servlet>
<servlet-name>ShowBalance</servlet-name>
<servlet-class>coreservlets.ShowBalance</servlet-class>
</servlet>
l i<servlet-mapping>
<servlet-name>ShowBalance</servlet-name>
<url-pattern>/show-balance</url-pattern>
</servlet-mapping></servlet-mapping>
...
</web-app>
36
Bank Account Balances:
ResultsResults
37
Comparing Data-Sharing
Approaches: RequestApproaches: Request
• Goal
– Display a random number to the user
Type of sharing• Type of sharing
– Each request should result in a new number, so request-
based sharing is appropriate.g pp p
38
Request-Based Sharing: Bean
package coreservlets;
public class NumberBean {
private final double num;
public NumberBean(double number) {
this.num = number;
}}
public double getNumber() {
return(num);etu ( u );
}
}
The property name in JSP will be “number”. The property name is derived from the method name, not from
39
The property name in JSP will be number . The property name is derived from the method name, not from
the instance variable name. Also note the lack of a corresponding setter.
Request-Based Sharing: Servlet
public class RandomNumberServlet extends HttpServlet {
public void doGet(HttpServletRequest requestpublic void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
NumberBean bean =NumberBean bean
RanUtils.getRandomNum(request.getParameter("range"));
request.setAttribute("randomNum", bean);
String address = "/WEB-INF/mvc-sharing/RandomNum.jsp";g / / g/ j p ;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);p q p
}
}
40
Request-Based Sharing:
Business LogicBusiness Logic
public class RanUtils {
public static NumberBean getRandomNum(String rangeString) {public static NumberBean getRandomNum(String rangeString) {
double range;
try {
range = Double.parseDouble(rangeString);range Double.parseDouble(rangeString);
} catch(Exception e) {
range = 10.0;
}}
return(new NumberBean(Math.random() * range));
}
}
41
Request-Based Sharing:
URL Pattern (web xml)URL Pattern (web.xml)
...
<servlet><servlet>
<servlet-name>RandomNumberServlet</servlet-name>
<servlet-class>
coreservlets RandomNumberServletcoreservlets.RandomNumberServlet
</servlet-class>
</servlet>
<servlet-mapping><servlet mapping>
<servlet-name>RandomNumberServlet</servlet-name>
<url-pattern>/random-number</url-pattern>
</servlet-mapping>/se et app g
...
42
Request-Based Sharing:
Input FormInput Form
...
<fieldset><fieldset>
<legend>Random Number</legend>
<form action="./random-number">
Range: <input type="text" name="range"><br/>Range: <input type= text name= range ><br/>
<input type="submit" value="Show Number">
</form>
</fieldset></fieldset>
...
43
Request-Based Sharing: JSP 1.2
…
<BODY><BODY>
<jsp:useBean id="randomNum"
type="coreservlets.NumberBean"
scope="request" />scope= request />
<H2>Random Number:
<jsp:getProperty name="randomNum"
property="number" />property number />
</H2>
</BODY></HTML>
44
Request-Based Sharing: JSP 2.0
…
<BODY><BODY>
<H2>Random Number: ${randomNum.number}</H2>
</BODY></HTML>
45
Request-Based Sharing:
ResultsResults
46
Comparing Data-Sharing
Approaches: SessionApproaches: Session
• Goal
– Display users’ first and last names.
– If the users fail to tell us their name, we want to use
whatever name they gave us previouslywhatever 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.
• Type of sharing
D i d f h li i b d h i i– Data is stored for each client, so session-based sharing is
appropriate.
47
Session-Based Sharing: Bean
public class NameBean implements Serializable {
private String firstName = "Missing first name";p g g
private String lastName = "Missing last name";
public String getFirstName() {
return(firstName);return(firstName);
}
public void setFirstName(String firstName) {
if (!isMissing(firstName)) {if (!isMissing(firstName)) {
this.firstName = firstName;
}
}
... // getLastName, setLastName
private boolean isMissing(String value) {
return((value == null) || (value.trim().equals("")));
}
}
48
Session-Based Sharing: Servlet
public class RegistrationServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
synchronized(session) {y ( ) {
NameBean nameBean =
(NameBean)session.getAttribute("name");
if (nameBean == null) {
nameBean = new NameBean();();
session.setAttribute("name", nameBean);
}
nameBean.setFirstName(request.getParameter("firstName"));
nameBean.setLastName(request.getParameter("lastName"));nameBean.setLastName(request.getParameter( lastName ));
String address = "/WEB-INF/mvc-sharing/ShowName.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher forward(request response);dispatcher.forward(request, response);
}
}
}
49
Session-Based Sharing: JSP 1.2
…
<BODY><BODY>
<H1>Thanks for Registering</H1>
<jsp:useBean id="name"
type="coreservlets NameBean"type= coreservlets.NameBean
scope="session" />
<H2>First Name:
<jsp:getProperty name="name"<jsp:getProperty name name
property="firstName" /></H2>
<H2>Last Name:
<jsp:getProperty name="name"jsp:get ope ty a e a e
property="lastName" /></H2>
</BODY></HTML>
50
Session-Based Sharing: JSP 2.0
…
<BODY><BODY>
<H1>Thanks for Registering</H1>
<H2>First Name: ${name.firstName}</H2>
<H2>Last Name: ${name.lastName}</H2>
</BODY></HTML>
51
Session-Based Sharing:
ResultsResults
Note: url patternNote: url-pattern
in web.xml is
"register".
52
Comparing Data-Sharing
Approaches: ServletContextApproaches: ServletContext
• Goal
– Display a prime number of a specified length.
– If the user fails to tell us the desired length, we want to
use whatever prime number we most recently computeduse whatever prime number we most recently computed
for any user.
• Type of sharing
– Data is shared among multiple clients, so application-
based sharing is appropriatebased sharing is appropriate.
53
ServletContext-Based Sharing:
BeanBean
package coreservlets;
import java.math.BigInteger;p j g g ;
public class PrimeBean {
private BigInteger prime;
public PrimeBean(String lengthString) {
int length = 150;
try {t y {
length = Integer.parseInt(lengthString);
} catch(NumberFormatException nfe) {}
this.prime = Primes.nextPrime(Primes.random(length));
}}
public BigInteger getPrime() {
return(prime);return(prime);
}
…
}54
ServletContext-Based Sharing:
ServletServlet
public class PrimeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,p ( p q q ,
HttpServletResponse response)
throws ServletException, IOException {
String length = request.getParameter("primeLength");
ServletContext context = getServletContext();ServletContext context = getServletContext();
synchronized(this) {
if ((context.getAttribute("primeBean") == null) ||
(!isMissing(length))) {
PrimeBean primeBean = new PrimeBean(length);
context.setAttribute("primeBean", primeBean);
}
String address = "/WEB-INF/mvc-sharing/ShowPrime.jsp";g / / g/ j p ;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}}
}
… // Definition of isMissing: null or empty string
}55
ServletContext-Based Sharing:
JSP 1 2JSP 1.2
…
<BODY><BODY>
<H1>A Prime Number</H1>
<jsp:useBean id="primeBean"
type="coreservlets PrimeBean"type= coreservlets.PrimeBean
scope="application" />
<jsp:getProperty name="primeBean"
property="prime" />property prime />
</BODY></HTML>
56
ServletContext-Based Sharing:
JSP 2 0JSP 2.0
…
<BODY><BODY>
<H1>A Prime Number</H1>
${primeBean.prime}
</BODY></HTML></BODY></HTML>
57
ServletContext-Based Sharing:
ResultsResults
58
Note: url-pattern in web.xml is "prime".
© 2010 Marty Hall
Forwarding andForwarding and
Includingg
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.59
Forwarding from JSP Pages
<% String destination;
if (Math random() > 0 5) {if (Math.random() > 0.5) {
destination = "/examples/page1.jsp";
} else {
destination = "/examples/page2 jsp";destination = /examples/page2.jsp ;
}
%>
<jsp:forward page="<%= destination %>" /><jsp:forward page <% destination %> />
• Legal but bad idea• Legal, but bad idea
– Business and control logic belongs in servlets
– Keep JSP focused on presentationp p
60
Including Pages Instead of
Forwarding to ThemForwarding to Them
• With the forward method of
RequestDispatcher:RequestDispatcher:
– New page generates all of the output
– Original page cannot generate any outputg p g g y p
• With the include method of
RequestDispatcher:
O t t b t d b lti l– Output can be generated by multiple pages
– Original page can generate output before and after the
included page
– Original servlet does not see the output of the included
page (for this, see later topic on servlet/JSP filters)
– Applicationspp
• Portal-like applications (see first example)
• Setting content type for output (see second example)
61
Using RequestDispatcher.include:
portalsportals
response.setContentType("text/html");
String firstTable, secondTable, thirdTable;
if ( di i ) {if (someCondition) {
firstTable = "/WEB-INF/Sports-Scores.jsp";
secondTable = "/WEB-INF/Stock-Prices.jsp";
thirdTable = "/WEB-INF/Weather.jsp";
} else if ( ) { }} else if (...) { ... }
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/Header.jsp");
dispatcher.include(request, response);
dispatcher =dispatcher =
request.getRequestDispatcher(firstTable);
dispatcher.include(request, response);
dispatcher =
request.getRequestDispatcher(secondTable);request.getRequestDispatcher(secondTable);
dispatcher.include(request, response);
dispatcher =
request.getRequestDispatcher(thirdTable);
dispatcher.include(request, response);p q p
dispatcher =
request.getRequestDispatcher("/WEB-INF/Footer.jsp");
dispatcher.include(request, response);
62
Using RequestDispatcher.include:
Setting Content-Type of OutputSetting Content Type of Output
// From Ajax example
public void doGet( ) {public void doGet(...) ... {
...
if ("xml".equals(format)) {
response.setContentType("text/xml");p yp ( )
outputPage = "/WEB-INF/results/cities-xml.jsp";
} else if ("json".equals(format)) {
response.setContentType("application/json");
outputPage = "/WEB-INF/results/cities-json.jsp";
} else {
response.setContentType("text/plain");
t tP "/WEB INF/ lt / iti t i j "outputPage = "/WEB-INF/results/cities-string.jsp";
}
RequestDispatcher dispatcher =
request getRequestDispatcher(outputPage);request.getRequestDispatcher(outputPage);
dispatcher.include(request, response);
}
63
cities-xml.jsp
<?xml version="1.0" encoding="UTF-8"?>
<cities><cities>
<city>
<name>${cities[0].name}</name>
<time>${cities[0] shortTime}</time><time>${cities[0].shortTime}</time>
<population>${cities[0].population}</population>
</city>
......
</cities>
• Notes
– Because I use .jsp (not .jspx) and classic JSP syntax, the
default content-type is text/html
– I could use <%@ page contentType="text/xml" %> hereI could use <%@ page contentType text/xml %> here,
but it is more convenient to do it in calling servlet and
keep this page simple and focused on presentation
64
Summary
• Use MVC (Model 2) approach when:
O b i i ill l i h b i l k– One submission will result in more than one basic look
– Several pages have substantial common processing
• ArchitectureArchitecture
– A servlet answers the original request
– Servlet does the real work & stores results in beans
• Beans stored in HttpServletRequest, HttpSession, or
ServletContext
– Servlet forwards to JSP page via forward method of
RequestDispatcher
– JSP page reads data from beans
• JSP 1 2: jsp:useBean with appropriate scope (requestJSP 1.2: jsp:useBean with appropriate scope (request,
session, or application) plus jsp:getProperty
• JSP 2.x: ${beanName.propertyName}
65
© 2010 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.66

Mais conteúdo relacionado

Mais procurados

Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
AkramWaseem
 

Mais procurados (20)

Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
 
JSP
JSPJSP
JSP
 
Jsf2 overview
Jsf2 overviewJsf2 overview
Jsf2 overview
 
Using RequireJS for Modular JavaScript Code
Using RequireJS for Modular JavaScript CodeUsing RequireJS for Modular JavaScript Code
Using RequireJS for Modular JavaScript Code
 
Advance java session 13
Advance java session 13Advance java session 13
Advance java session 13
 
Resthub framework presentation
Resthub framework presentationResthub framework presentation
Resthub framework presentation
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Ajax basics
Ajax basicsAjax basics
Ajax basics
 
Chap4 4 2
Chap4 4 2Chap4 4 2
Chap4 4 2
 
Jsp basic
Jsp basicJsp basic
Jsp basic
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Jsp
JspJsp
Jsp
 
Resthub lyonjug
Resthub lyonjugResthub lyonjug
Resthub lyonjug
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
 

Destaque (17)

01 overview-and-setup
01 overview-and-setup01 overview-and-setup
01 overview-and-setup
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
07 cookies
07 cookies07 cookies
07 cookies
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 
The Change! Tool
The Change! ToolThe Change! Tool
The Change! Tool
 
03 form-data
03 form-data03 form-data
03 form-data
 
ICT observatories for better governance
ICT observatories for better governanceICT observatories for better governance
ICT observatories for better governance
 
06 response-headers
06 response-headers06 response-headers
06 response-headers
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
04 request-headers
04 request-headers04 request-headers
04 request-headers
 
Linea Viso-Corpo "Naturale" by Delta BkB
Linea Viso-Corpo "Naturale" by Delta BkBLinea Viso-Corpo "Naturale" by Delta BkB
Linea Viso-Corpo "Naturale" by Delta BkB
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
Digital Engagement for CSPs
Digital Engagement for CSPsDigital Engagement for CSPs
Digital Engagement for CSPs
 
11 page-directive
11 page-directive11 page-directive
11 page-directive
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 

Semelhante a 14 mvc

Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
divzi1913
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
Ramamohan Chokkam
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
Rajesh Moorjani
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
Long Nguyen
 
Csajsp Chapter15
Csajsp Chapter15Csajsp Chapter15
Csajsp Chapter15
Adil Jafri
 

Semelhante a 14 mvc (20)

java beans
java beansjava beans
java beans
 
My04_MVC.pdf
My04_MVC.pdfMy04_MVC.pdf
My04_MVC.pdf
 
10 jsp-scripting-elements
10 jsp-scripting-elements10 jsp-scripting-elements
10 jsp-scripting-elements
 
20jsp
20jsp20jsp
20jsp
 
Jsp
JspJsp
Jsp
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
 
Coursejspservlets00
Coursejspservlets00Coursejspservlets00
Coursejspservlets00
 
Jsf Framework
Jsf FrameworkJsf Framework
Jsf Framework
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp
JspJsp
Jsp
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
JSP overview
JSP overviewJSP overview
JSP overview
 
Csajsp Chapter15
Csajsp Chapter15Csajsp Chapter15
Csajsp Chapter15
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

14 mvc

  • 1. © 2010 Marty Hall Integrating Servlets and JSP: The Model View Controller (MVC) Architecture(MVC) Architecture Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/csajsp2.html Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.2 © 2010 Marty Hall For live Java EE training, please see training courses at http://courses.coreservlets.com/.at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo, Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT), Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g Hibernate/JPA, and customized combinations of topics. Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial Available at public Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your organization. Contact hall@coreservlets.com for details.
  • 2. Agenda • Understanding the benefits of MVC • Using RequestDispatcher to implement MVC • Forwarding requests from servlets to JSP pages • Handling relative URLs • Choosing among different display options • Comparing data-sharing strategies 4 © 2010 Marty Hall MVC Motivation Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.5
  • 3. Uses of JSP Constructs • Scripting elements calling servletSimple Scripting elements calling servlet code directly • Scripting elements calling servlet p Application g g code indirectly (by means of utility classes) B• Beans • Servlet/JSP combo (MVC) MVC ith JSP i l• MVC with JSP expression language • Custom tags MVC ith b t t d Complex Application • MVC with beans, custom tags, and a framework like Struts or JSF 6 Why Combine Servlets & JSP? • Typical picture: use JSP to make it easier to d l d i i h HTMdevelop and maintain the HTML content – For simple dynamic code, call servlet code from scripting elementsscripting elements – For slightly more complex applications, use custom classes called from scripting elements – For moderately complex applications, use beans and custom tags • But that’s not enough• But, that s not enough – For complex processing, starting with JSP is awkward – Despite the ease of separating the real code into separatep p g p classes, beans, and custom tags, the assumption behind JSP is that a single page gives a single basic look 7
  • 4. Possibilities for Handling a Single RequestSingle Request • Servlet only. Works well when: O i bi E i– Output is a binary type. E.g.: an image – There is no output. E.g.: you are doing forwarding or redirection as in Search Engine example. – Format/layout of page is highly variable. E.g.: portal. • JSP only. Works well when: – Output is mostly character data. E.g.: HTMLp y g – Format/layout mostly fixed. • Combination (MVC architecture). Needed when: A i l t ill lt i lti l b t ti ll diff t– A single request will result in multiple substantially different- looking results. – You have a large development team with different team members doing the Web development and the business logicdoing the Web development and the business logic. – You perform complicated data processing, but have a relatively fixed layout. 8 MVC Misconceptions • An elaborate framework is necessary – Frameworks are sometimes useful • Struts • JavaServer Faces (JSF)JavaServer Faces (JSF) – They are not required! • Implementing MVC with the builtin RequestDispatcher works very well for most simple and moderately complexworks very well for most simple and moderately complex applications • MVC totally changes your overall system design – You can use MVC for individual requests Thi k f it th MVC h t th– Think of it as the MVC approach, not the MVC architecture • Also called the Model 2 approach9
  • 5. © 2010 Marty Hall Beans Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.10 Review: Beans • Java classes that follow certain conventions – Must have a zero-argument (empty) constructor • You can satisfy this requirement either by explicitly defining such a constructor or by omitting all constructorsg y g • In this version of MVC, it is not required to have zero arg constructor if you only instantiate from Java code – Should have no public instance variables (fields)Should have no public instance variables (fields) • I hope you already follow this practice and use accessor methods instead of allowing direct access to fields Persistent values should be accessed through methods– Persistent values should be accessed through methods called getXxx and setXxx • If class has method getTitle that returns a String, class i id t h St i t d i lis said to have a String property named title • Boolean properties can use isXxx instead of getXxx 11
  • 6. Bean Properties: Examples Method Names Property Name Example JSP Usage getFirstName setFirstName firstName <jsp:getProperty … property="firstName"/> <jsp:setProperty … property="firstName"/> ${customer.firstName} isExecutive setExecutive (boolean property) executive <jsp:getProperty … property="executive"/> <jsp:setProperty … property="executive"/> ${customer.executive} getExecutive setExecutive (boolean property) executive <jsp:getProperty … property="executive"/> <jsp:setProperty … property="executive"/> ${customer.executive} getZIP setZIP ZIP <jsp:getProperty … property="ZIP"/> <jsp:setProperty … property="ZIP"/> ${address.ZIP} 12 Note 1: property name does not exist anywhere in your code. It is just a shortcut for the method name. Note 2: property name is derived only from method name. Instance variable name is irrelevant. Example: StringBean package coreservlets; public class StringBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { hithis.message = message; } } • Beans installed in normal Java directory – Eclipse: src/folderMatchingPackage l d / / l /f ld h k– Deployed: …/WEB-INF/classes/folderMatchingPackage • Beans (and utility classes) must always be in packages! 13
  • 7. © 2010 Marty Hall Basic MVC Design Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.14 MVC Flow of Control HTML or JSP Java Code (Business Logic) R l Form Servletsubmit form (Form ACTION matches url-pattern of servlet) Results (beans) (Store beans in request, session, or application scope) request.setAttribute("customer", JSP q ( , currentCustomer); JSP1 JSP2 JSP3 (Extract data from beans and put in output) 15 p p ) ${customer.firstName}
  • 8. Implementing MVC with RequestDispatcherRequestDispatcher 1. Define beans to represent result data – Ordinary Java classes with at least one getBlah method 2. Use a servlet to handle requests S l d h k f i i– Servlet reads request parameters, checks for missing and malformed data, calls business logic, etc. 3. Obtain bean instances3. Obtain bean instances – The servlet invokes business logic (application-specific code) or data-access code to obtain the results. 4. Store the bean in the request, session, or servlet context Th l t ll tAtt ib t th t i– 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.16 Implementing MVC with RequestDispatcher (Continued)RequestDispatcher (Continued) 5. 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.RequestDispatcher to transfer control to that page. 6. Extract the data from the beans. – JSP 1.2: the JSP page accesses beans with jsp:useBeanp g j p and a scope matching the location of step 4. The page then uses jsp:getProperty to output the bean properties. JSP 2 0: the JSP page uses– JSP 2.0: the JSP page uses ${nameFromServlet.property} to output bean properties – Either way, the JSP page does not create or modify the bean; it merely extracts and displays data that the servlet created. 17
  • 9. Request Forwarding Example public void doGet(HttpServletRequest request, HttpServletResponse response)p p p ) throws ServletException, IOException { ... // Do business logic and get data String operation = request.getParameter("operation"); if (operation == null) {if (operation == null) { operation = "unknown"; } String address; if (operation.equals("order")) { address = "/WEB-INF/Order.jsp"; } else if (operation.equals("cancel")) { address = "/WEB-INF/Cancel jsp";address = /WEB INF/Cancel.jsp ; } else { address = "/WEB-INF/UnknownOperation.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); }18 jsp:useBean in MVC vs. in Standalone JSP Pagesin Standalone JSP Pages • The JSP page should not create h bjthe objects – The servlet, not the JSP page, should create all the data objects So to guarantee that the JSP page will not createobjects. So, to guarantee that the JSP page will not create objects, you should use <jsp:useBean ... type="package.Class" /> i d finstead of <jsp:useBean ... class="package.Class" /> • The JSP page should not modify the objects S h ld j b– So, you should use jsp:getProperty but not jsp:setProperty. 19
  • 10. © 2010 Marty Hall Scopes: request, session, and application (ServletContext)application (ServletContext) Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.20 Reminder: jsp:useBean Scope Alternatives (JSP 1 2 Only!)Scope Alternatives (JSP 1.2 Only!) • request – <jsp:useBean id="..." type="..." scope="request" /> • session j B id " " " " " i " /– <jsp:useBean id="..." type="..." scope="session" /> • application <jsp:useBean id=" " type=" " scope="application" />– <jsp:useBean id= ... type= ... scope= application /> • page – <jsp:useBean id=" " type=" " scope="page" /><jsp:useBean id ... type ... scope page /> or just <jsp:useBean id="..." type="..." /> Thi i t d i MVC (M d l 2) hit t– This scope is not used in MVC (Model 2) architecture 21
  • 11. Request-Based Data Sharing • Servlet ValueObject value = LookupService.findResult(...); request.setAttribute("key", value); RequestDispatcher dispatcher = request.getRequestDispatcher ("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response); • JSP 1.2 <jsp:useBean id="key" type="somePackage.ValueObject"<jsp:useBean id key type somePackage.ValueObject scope="request" /> <jsp:getProperty name="key" property="someProperty" /> JSP 2 0 Name chosen by the servlet • JSP 2.0 ${key.someProperty} 22 Name chosen by the servlet. Name of accessor method, minus the word "get", with next letter changed to lower case. Request-Based Data Sharing: Simplified ExampleSimplified Example • Servlet C t C t Assume that the Customer constructor handles missing/malformed data. Customer myCustomer = Lookup.findCust(request.getParameter("customerID")); request.setAttribute("customer", myCustomer); RequestDispatcher dispatcher =RequestDispatcher dispatcher request.getRequestDispatcher ("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response); • JSP 1.2 <jsp:useBean id="customer" type="somePackage.Customer"j p yp g scope="request" /> <jsp:getProperty name="customer" property="firstName"/> • JSP 2 0• JSP 2.0 ${customer.firstName} 23 Note: the Customer class must have a method called “getFirstName”.
  • 12. Session-Based Data Sharing • Servlet l bj l k i fi d l ( )ValueObject value = LookupService.findResult(...); HttpSession session = request.getSession(); session.setAttribute("key", value); RequestDispatcher dispatcher =RequestDispatcher dispatcher request.getRequestDispatcher ("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response); • JSP 1.2 <jsp:useBean id="key" type="somePackage.ValueObject"j p y yp g j scope="session" /> <jsp:getProperty name="key" property="someProperty" /> • JSP 2 0JSP 2.0 ${key.someProperty} 24 Session-Based Data Sharing: VariationVariation • Redirect to page instead of forwarding to it U dR di i d f R Di h f d– Use response.sendRedirect instead of RequestDispatcher.forward • Distinctions: with sendRedirect: – User sees JSP URL (user sees only servlet URL with( y RequestDispatcher.forward) – Two round trips to client (only one with forward) • Advantage of sendRedirectAdvantage of sendRedirect – User can visit JSP page separately • User can bookmark JSP page Disadvantages of sendRedirect• Disadvantages of sendRedirect – Two round trips to server is more expensive – Since user can visit JSP page without going through servlet first, b d i h b il blbean data might not be available • So, JSP page needs code to detect this situation 25
  • 13. ServletContext-Based Data Sharing (Rare)Sharing (Rare) • Servlet synchronized(this) { ValueObject value = SomeLookup.findResult(...); getServletContext().setAttribute("key", value); Req estDispatcher dispatcherRequestDispatcher dispatcher = request.getRequestDispatcher ("/WEB-INF/SomePage.jsp"); dispatcher forward(request response);dispatcher.forward(request, response); } • JSP 1.2 <j B id "k " t " P k V l Obj t"<jsp:useBean id="key" type="somePackage.ValueObject" scope="application" /> <jsp:getProperty name="key" property="someProperty" /> • JSP 2.0 ${key.someProperty} 26 Relative URLs in JSP Pages • Issue: – Forwarding with a request dispatcher is transparent to the client. Original URL is only URL browser knows about. • Why does this matter?• Why does this matter? – What will browser do with tags like the following? <img src="foo.gif" …> <link rel="stylesheet" href="my-styles.css"href my styles.css type="text/css"> <a href="bar.jsp">…</a> – Browser treats addresses as relative to servlet URL 27
  • 14. © 2010 Marty Hall Examples Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.28 Applying MVC: Bank Account BalancesBank Account Balances • Bean B kC t– BankCustomer • Business Logic – BankCustomerLookup • Servlet that populates bean and forwards to appropriate JSP page – Reads customer ID calls BankCustomerLookup’sReads customer ID, calls BankCustomerLookup s data-access code to obtain BankCustomer – Uses current balance to decide on appropriate result page • JSP pages to display results• JSP pages to display results – Negative balance: warning page – Regular balance: standard page Hi h b l ith d ti t dd d– High balance: page with advertisements added – Unknown customer ID: error page 29
  • 15. Bank Account Balances: Servlet CodeServlet Code public class ShowBalance extends HttpServlet { public void doGet(HttpServletRequest request,p ( p q q , HttpServletResponse response) throws ServletException, IOException { BankCustomer currentCustomer = BankCustomerLookup getCustomerBankCustomerLookup.getCustomer (request.getParameter("id")); request.setAttribute("customer", currentCustomer); String address; if (currentCustomer == null) { address = "/WEB-INF/bank-account/UnknownCustomer.jsp"; } else if (currentCustomer getBalance() < 0) {} else if (currentCustomer.getBalance() < 0) { address = "/WEB-INF/bank-account/NegativeBalance.jsp"; } ... RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); 30 Bank Account Balances: BeanBean public class BankCustomer { private final String id, firstName, lastName;p g , , ; private final double balance; public BankCustomer(String id, String firstName Since the constructor is called from Java only (never from JSP), the requirement for a zero-arg constructor is eliminated. Also, since bean state is set only with constructor, rather than with jsp:setProperty, we can eliminate setter methods and make the class immutable. String firstName, String lastName, double balance) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance; }} // Getters for four instance variables. No setters. public double getBalanceNoSign() { return(Math.abs(balance)); } }31
  • 16. Bank Account Balances: Business LogicBusiness Logic public class BankCustomerLookup { private static Map<String,BankCustomer> customers;private static Map<String,BankCustomer> customers; static { // Populate Map with some sample customersp p p } … public static BankCustomer getCustomer(String id) { return(customers.get(id)); }} } 32 Bank Account Balances: Input FormInput Form … <fieldset><fieldset> <legend>Bank Account Balance</legend> <form action="./show-balance"> Customer ID: <input type="text" name="id"><br>p yp <input type="submit" value="Show Balance"> </form> </fieldset> For the servlet, use the address http://host/appName/show-balance that is set via url-pattern in web.xml … 33
  • 17. Bank Account Balances: JSP 1 2 Code (Negative Balance)JSP 1.2 Code (Negative Balance) … <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> We Know Where You Live!</TABLE> <P><P> <IMG SRC="/bank-support/Club.gif" ALIGN="LEFT"> <jsp:useBean id="customer" type="coreservlets.BankCustomer" scope="request" /> Watch out, <jsp:getProperty name="customer" property="firstName" />property= firstName />, we know where you live. <P> Pay us the $<jsp:getProperty name="customer" /property="balanceNoSign" /> you owe us before it is too late! </BODY></HTML> 34 Bank Account Balances: JSP 2 0 Code (Negative Balance)JSP 2.0 Code (Negative Balance) … <BODY><BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> We Know Where You Live!</TABLE> <P> <IMG SRC="/bank-support/Club.gif" ALIGN="LEFT"> Watch out, ${customer.firstName}, we know where you live. <P> Pay us the $${customer.balanceNoSign} b f it i t l t !you owe us before it is too late! </BODY></HTML> 35
  • 18. Bank Account Balances: web xmlweb.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 4" ><web app version 2.4 ...> <!-- Use the URL http://host/app/show-balance instead of http://host/app/servlet/coreservlets.ShowBalance --> <servlet> <servlet-name>ShowBalance</servlet-name> <servlet-class>coreservlets.ShowBalance</servlet-class> </servlet> l i<servlet-mapping> <servlet-name>ShowBalance</servlet-name> <url-pattern>/show-balance</url-pattern> </servlet-mapping></servlet-mapping> ... </web-app> 36 Bank Account Balances: ResultsResults 37
  • 19. Comparing Data-Sharing Approaches: RequestApproaches: Request • Goal – Display a random number to the user Type of sharing• Type of sharing – Each request should result in a new number, so request- based sharing is appropriate.g pp p 38 Request-Based Sharing: Bean package coreservlets; public class NumberBean { private final double num; public NumberBean(double number) { this.num = number; }} public double getNumber() { return(num);etu ( u ); } } The property name in JSP will be “number”. The property name is derived from the method name, not from 39 The property name in JSP will be number . The property name is derived from the method name, not from the instance variable name. Also note the lack of a corresponding setter.
  • 20. Request-Based Sharing: Servlet public class RandomNumberServlet extends HttpServlet { public void doGet(HttpServletRequest requestpublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { NumberBean bean =NumberBean bean RanUtils.getRandomNum(request.getParameter("range")); request.setAttribute("randomNum", bean); String address = "/WEB-INF/mvc-sharing/RandomNum.jsp";g / / g/ j p ; RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response);p q p } } 40 Request-Based Sharing: Business LogicBusiness Logic public class RanUtils { public static NumberBean getRandomNum(String rangeString) {public static NumberBean getRandomNum(String rangeString) { double range; try { range = Double.parseDouble(rangeString);range Double.parseDouble(rangeString); } catch(Exception e) { range = 10.0; }} return(new NumberBean(Math.random() * range)); } } 41
  • 21. Request-Based Sharing: URL Pattern (web xml)URL Pattern (web.xml) ... <servlet><servlet> <servlet-name>RandomNumberServlet</servlet-name> <servlet-class> coreservlets RandomNumberServletcoreservlets.RandomNumberServlet </servlet-class> </servlet> <servlet-mapping><servlet mapping> <servlet-name>RandomNumberServlet</servlet-name> <url-pattern>/random-number</url-pattern> </servlet-mapping>/se et app g ... 42 Request-Based Sharing: Input FormInput Form ... <fieldset><fieldset> <legend>Random Number</legend> <form action="./random-number"> Range: <input type="text" name="range"><br/>Range: <input type= text name= range ><br/> <input type="submit" value="Show Number"> </form> </fieldset></fieldset> ... 43
  • 22. Request-Based Sharing: JSP 1.2 … <BODY><BODY> <jsp:useBean id="randomNum" type="coreservlets.NumberBean" scope="request" />scope= request /> <H2>Random Number: <jsp:getProperty name="randomNum" property="number" />property number /> </H2> </BODY></HTML> 44 Request-Based Sharing: JSP 2.0 … <BODY><BODY> <H2>Random Number: ${randomNum.number}</H2> </BODY></HTML> 45
  • 23. Request-Based Sharing: ResultsResults 46 Comparing Data-Sharing Approaches: SessionApproaches: Session • Goal – Display users’ first and last names. – If the users fail to tell us their name, we want to use whatever name they gave us previouslywhatever 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. • Type of sharing D i d f h li i b d h i i– Data is stored for each client, so session-based sharing is appropriate. 47
  • 24. Session-Based Sharing: Bean public class NameBean implements Serializable { private String firstName = "Missing first name";p g g private String lastName = "Missing last name"; public String getFirstName() { return(firstName);return(firstName); } public void setFirstName(String firstName) { if (!isMissing(firstName)) {if (!isMissing(firstName)) { this.firstName = firstName; } } ... // getLastName, setLastName private boolean isMissing(String value) { return((value == null) || (value.trim().equals(""))); } } 48 Session-Based Sharing: Servlet public class RegistrationServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); synchronized(session) {y ( ) { NameBean nameBean = (NameBean)session.getAttribute("name"); if (nameBean == null) { nameBean = new NameBean();(); session.setAttribute("name", nameBean); } nameBean.setFirstName(request.getParameter("firstName")); nameBean.setLastName(request.getParameter("lastName"));nameBean.setLastName(request.getParameter( lastName )); String address = "/WEB-INF/mvc-sharing/ShowName.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher forward(request response);dispatcher.forward(request, response); } } } 49
  • 25. Session-Based Sharing: JSP 1.2 … <BODY><BODY> <H1>Thanks for Registering</H1> <jsp:useBean id="name" type="coreservlets NameBean"type= coreservlets.NameBean scope="session" /> <H2>First Name: <jsp:getProperty name="name"<jsp:getProperty name name property="firstName" /></H2> <H2>Last Name: <jsp:getProperty name="name"jsp:get ope ty a e a e property="lastName" /></H2> </BODY></HTML> 50 Session-Based Sharing: JSP 2.0 … <BODY><BODY> <H1>Thanks for Registering</H1> <H2>First Name: ${name.firstName}</H2> <H2>Last Name: ${name.lastName}</H2> </BODY></HTML> 51
  • 26. Session-Based Sharing: ResultsResults Note: url patternNote: url-pattern in web.xml is "register". 52 Comparing Data-Sharing Approaches: ServletContextApproaches: ServletContext • Goal – Display a prime number of a specified length. – If the user fails to tell us the desired length, we want to use whatever prime number we most recently computeduse whatever prime number we most recently computed for any user. • Type of sharing – Data is shared among multiple clients, so application- based sharing is appropriatebased sharing is appropriate. 53
  • 27. ServletContext-Based Sharing: BeanBean package coreservlets; import java.math.BigInteger;p j g g ; public class PrimeBean { private BigInteger prime; public PrimeBean(String lengthString) { int length = 150; try {t y { length = Integer.parseInt(lengthString); } catch(NumberFormatException nfe) {} this.prime = Primes.nextPrime(Primes.random(length)); }} public BigInteger getPrime() { return(prime);return(prime); } … }54 ServletContext-Based Sharing: ServletServlet public class PrimeServlet extends HttpServlet { public void doGet(HttpServletRequest request,p ( p q q , HttpServletResponse response) throws ServletException, IOException { String length = request.getParameter("primeLength"); ServletContext context = getServletContext();ServletContext context = getServletContext(); synchronized(this) { if ((context.getAttribute("primeBean") == null) || (!isMissing(length))) { PrimeBean primeBean = new PrimeBean(length); context.setAttribute("primeBean", primeBean); } String address = "/WEB-INF/mvc-sharing/ShowPrime.jsp";g / / g/ j p ; RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); }} } … // Definition of isMissing: null or empty string }55
  • 28. ServletContext-Based Sharing: JSP 1 2JSP 1.2 … <BODY><BODY> <H1>A Prime Number</H1> <jsp:useBean id="primeBean" type="coreservlets PrimeBean"type= coreservlets.PrimeBean scope="application" /> <jsp:getProperty name="primeBean" property="prime" />property prime /> </BODY></HTML> 56 ServletContext-Based Sharing: JSP 2 0JSP 2.0 … <BODY><BODY> <H1>A Prime Number</H1> ${primeBean.prime} </BODY></HTML></BODY></HTML> 57
  • 29. ServletContext-Based Sharing: ResultsResults 58 Note: url-pattern in web.xml is "prime". © 2010 Marty Hall Forwarding andForwarding and Includingg Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.59
  • 30. Forwarding from JSP Pages <% String destination; if (Math random() > 0 5) {if (Math.random() > 0.5) { destination = "/examples/page1.jsp"; } else { destination = "/examples/page2 jsp";destination = /examples/page2.jsp ; } %> <jsp:forward page="<%= destination %>" /><jsp:forward page <% destination %> /> • Legal but bad idea• Legal, but bad idea – Business and control logic belongs in servlets – Keep JSP focused on presentationp p 60 Including Pages Instead of Forwarding to ThemForwarding to Them • With the forward method of RequestDispatcher:RequestDispatcher: – New page generates all of the output – Original page cannot generate any outputg p g g y p • With the include method of RequestDispatcher: O t t b t d b lti l– Output can be generated by multiple pages – Original page can generate output before and after the included page – Original servlet does not see the output of the included page (for this, see later topic on servlet/JSP filters) – Applicationspp • Portal-like applications (see first example) • Setting content type for output (see second example) 61
  • 31. Using RequestDispatcher.include: portalsportals response.setContentType("text/html"); String firstTable, secondTable, thirdTable; if ( di i ) {if (someCondition) { firstTable = "/WEB-INF/Sports-Scores.jsp"; secondTable = "/WEB-INF/Stock-Prices.jsp"; thirdTable = "/WEB-INF/Weather.jsp"; } else if ( ) { }} else if (...) { ... } RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/Header.jsp"); dispatcher.include(request, response); dispatcher =dispatcher = request.getRequestDispatcher(firstTable); dispatcher.include(request, response); dispatcher = request.getRequestDispatcher(secondTable);request.getRequestDispatcher(secondTable); dispatcher.include(request, response); dispatcher = request.getRequestDispatcher(thirdTable); dispatcher.include(request, response);p q p dispatcher = request.getRequestDispatcher("/WEB-INF/Footer.jsp"); dispatcher.include(request, response); 62 Using RequestDispatcher.include: Setting Content-Type of OutputSetting Content Type of Output // From Ajax example public void doGet( ) {public void doGet(...) ... { ... if ("xml".equals(format)) { response.setContentType("text/xml");p yp ( ) outputPage = "/WEB-INF/results/cities-xml.jsp"; } else if ("json".equals(format)) { response.setContentType("application/json"); outputPage = "/WEB-INF/results/cities-json.jsp"; } else { response.setContentType("text/plain"); t tP "/WEB INF/ lt / iti t i j "outputPage = "/WEB-INF/results/cities-string.jsp"; } RequestDispatcher dispatcher = request getRequestDispatcher(outputPage);request.getRequestDispatcher(outputPage); dispatcher.include(request, response); } 63
  • 32. cities-xml.jsp <?xml version="1.0" encoding="UTF-8"?> <cities><cities> <city> <name>${cities[0].name}</name> <time>${cities[0] shortTime}</time><time>${cities[0].shortTime}</time> <population>${cities[0].population}</population> </city> ...... </cities> • Notes – Because I use .jsp (not .jspx) and classic JSP syntax, the default content-type is text/html – I could use <%@ page contentType="text/xml" %> hereI could use <%@ page contentType text/xml %> here, but it is more convenient to do it in calling servlet and keep this page simple and focused on presentation 64 Summary • Use MVC (Model 2) approach when: O b i i ill l i h b i l k– One submission will result in more than one basic look – Several pages have substantial common processing • ArchitectureArchitecture – A servlet answers the original request – Servlet does the real work & stores results in beans • Beans stored in HttpServletRequest, HttpSession, or ServletContext – Servlet forwards to JSP page via forward method of RequestDispatcher – JSP page reads data from beans • JSP 1 2: jsp:useBean with appropriate scope (requestJSP 1.2: jsp:useBean with appropriate scope (request, session, or application) plus jsp:getProperty • JSP 2.x: ${beanName.propertyName} 65
  • 33. © 2010 Marty Hall Questions? Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.66