Chapter 8 Script-free pages

Download Report

Transcript Chapter 8 Script-free pages

Chapter 8 Script-free pages
Problem with scripting in JSP
• When you use scripting (declaration, scriplet,
expressions) in your JSP, you actually put Java
code in your JSP files
– This can become a maintenance nightmare
• Page designer does not know Java and programmer is
not be able to design prettier pages
Example
Suppose we have a small web program that asks user
the name and age, and then calculate the monthly fee
of a sports club.
Here is the HTML code for this
page
<html><head>
<meta content="text/html; charset=ISO-8859-1" httpequiv="content-type"><title>test</title>
</head><body>
<br>
<form action="process.jsp" name="question">
What is your name? <input name="name"
type="text"><br>
<br>
What is your age? <input name="age" type="text"><br>
<br>
<br>
<input value="submit" type="submit">
<br>
</form>
<br>
</body></html>
Using scripting to generate the
dynamic content
Here is the HTML code for this
page
Here is the JSP code to process
the user form data
<html><head>
<meta content="text/html; charset=ISO-8859-1" httpequiv="content-type"><title>test</title>
<html><head>
<meta content="text/html; charset=ISO-8859-1" httpequiv="content-type"><title>process</title>
</head><body>
<br>
<form action="process.jsp" name="question">
What is your name? <input name="name"
type="text"><br>
<br>
What is your age? <input name="age" type="text"><br>
<br>
<br>
<input value="submit" type="submit">
<br>
</form>
<br>
</body></html>
</head><body>
Thanks, <%= request.getParameter("name") %> !<br>
<br>
Your monthly payment for the sports club is
<% int age=Integer.parseInt(request.getParameter("age"));
int payment = 0;
if (age <6)
payment =0;
else if (age < 18)
payment = 10;
else if (age < 60)
payment = 30;
else
payment = 10;
%>
$<%= payment %> .
</body></html>
MVC
• When program becomes more complex, it is hard
for both web designer and program to implement
and maintain it
• By applying the Model-View-Controller (MVC)
architecture to a J2EE application, you separate
core business model functionality from the
presentation and control logic that uses this
functionality
• Such separation make it easier to implement,
test, and maintain the application
MVC
MVC
• Model - The model represents enterprise data and the
business rules that govern access to and updates of this
data.
• View -The view renders the contents of a model. It accesses
enterprise data through the model and specifies how that
data should be presented. It is the view's responsibility to
maintain consistency in its presentation when the model
changes.
• Controller - The controller translates interactions with the
view into actions to be performed by the model. Very
often in a J2EE application, controller is responsible for
dispatching request to appropriate modules
MVC
• In J2EE application
– Model can be Java Beans, Enterprise Java Beans
(EJB), or other services
– View normally is JSP pages that is responsible for
rendering the HTML page
– Controller normally is a servlet that is responsible
for dispatching user request to appropriate
components such as other servlet or JSP pages
JavaBeans
• It is a java class that follow the following rules:
– You must have a public, no-arg constructor
– You must name your public getter and setter methods
starting with get (or is, for a boolean) and set,
followed by the name of the instance variable (also
called bean property) with the first letter of the word
capitalized
– The setter argument type and the getter return type
MUST be identical. This defines the property type.
int getFoo() void setFoo(int foo)
Example JavaBean
public class UserInfo {
private String name;
public void setName(String sname){
name = sname;
}
public String getName(){
return name;
}
}
Pay attention that we have a capitalized
N in the setter and getter method
JavaBeans
• By the way, you need to package your class for its
use in Tomcat
– That is, you need to write somehthing like
package edu.nku.cs
package foo
at the beginning of your Java Beans, otherwise,
tomcat cannot find your class
• Also, sometimes, you do not have to have both
getter and setter for a property. See the following
example for detail.
JavaBeans
• You can use Java Beans to automatically extract the
HTTP GET/POST parameters and set these parameters
in corresponding instance variables for you
– Thus you do not need to use request.getParameter() or
request.getParameterValues() in your JSP file
• To do this, you need to define instance variables in Java
Beans that have the exact names (variable names) as
the name of the parameters passed by HTTP GET/POST
method
– Of course, you also need to follow the rules of Java Beans
such as no-arg constructor, rules for getter and setter
method
Example of using JavaBeans
<html><head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type"><title>test</title>
</head><body>
<br>
<form action="process.jsp" name="question">
What is your name? <input name="name"
type="text"><br>
<br>
What is your age? <input name="age"
type="text"><br>
<br>
<br>
<input value="submit" type="submit">
<br>
</form>
<br>
</body></html>
package foo;
public class UserInfo {
private String name;
private int age;
public void setName(String sname){
name = sname;
}
public String getName(){
return name;
}
public void setAge(int sage){
age = sage;
}
public int getAge(){
return age;
}
public int getMonthlyFee(){
int payment;
if (age <6)
payment =0;
else if (age < 18)
payment = 10;
else if (age < 60)
payment = 30;
else
payment = 10;
return payment;
Example of using Java Beans
• In your JSP file, you need to declare and
initalize a bean attribute with <jsp:useBean>
<jsp:useBean id="userData" class="foo.UserInfo" scope="request" />
Identifier for
the bean object.
Think it as a
variable name
for one instance
of the bean
class
Class name
Example of using Java Beans
• Then you can set a bean attribute’s property
value with <jsp:setProperty>
<jsp:setProperty name="userData" property="age" />
Identifies the actual
bean object. This
will match the “id”
value from the
<jsp:useBean> tag
Identifies the
property name (in
other words, the
thing with the getter
and setter in the
bean class)
Example of using Java Beans
package foo;
This is your jsp file process.jsp
public class UserInfo {
private String name;
private int age;
public void setName(String sname){
name = sname;
}
<jsp:useBean id="userData" class="foo.UserInfo"
scope="request" />
<jsp:setProperty name="userData" property="name" />
<jsp:setProperty name="userData" property="age" />
<html><head>
<meta content="text/html; charset=ISO-8859-1" httpequiv="content-type"><title>process</title>
public String getName(){
return name;
}
public void setAge(int sage){
age = sage;
}
</head><body>
Thanks, <jsp:getProperty name="userData"
property="name" /> !<br>
<br>
Your monthly payment for the sports club is $
<jsp:getProperty name="userData"
property="monthlyFee" /> .
</body></html>
public int getAge(){
return age;
}
public int getMonthlyFee(){
int payment;
if (age <6)
payment =0;
else if (age < 18)
payment = 10;
else if (age < 60)
payment = 30;
else
payment = 10;
The property name in JSP and
the variable name in bean have
to match
return payment;
}
}
Example of using JavaBeans
• There is a even better approach
– You can set multiple property values with
<jsp:setProperty name="userData" property=“*" />
Identifies the actual
bean object. This
will match the “id”
value from the
<jsp:useBean> tag
Using * to set the
values of all
properties
Example of using Java Beans
package foo;
<jsp:useBean id="userData"
class="foo.UserInfo" scope="request" />
<jsp:setProperty name="userData"
property="*" />
<html><head>
<meta content="text/html; charset=ISO-88591" http-equiv="contenttype"><title>process</title>
public class UserInfo {
private String name;
private int age;
public void setName(String sname){
name = sname;
}
public String getName(){
return name;
}
public void setAge(int sage){
age = sage;
}
</head><body>
Thanks, <jsp:getProperty name="userData"
property="name" /> !<br>
<br>
Your monthly payment for the sports club is $
<jsp:getProperty name="userData"
property="monthlyFee" /> .
</body></html>
public int getAge(){
return age;
}
public int getMonthlyFee(){
int payment;
if (age <6)
payment =0;
else if (age < 18)
payment = 10;
else if (age < 60)
payment = 30;
else
payment = 10;
return payment;
}
}
<jsp:setProperty>
here will call both
setName(…) and
setAge(…) to set
property name and
age
Example of using Java Beans
• Please be noted that one nice feature of using
Java Beans is that if Java Bean properties are
Strings or primitives, <jsp:setProperty> action
will do the coercing for you
– For example, we have age as an int type,
<jsp:setProperty> action takes the String request
parameter age, converts it to an int, and passes
that int to the bean’s setter method setAge(…)
Example of using Java Beans
• After you extract the HTTP GET/POST request
parameters and set them in Java Beans, you
can get these info out of Beans by using
<jsp:getProperty name="userData" property="name" />
• Pay attention to
<jsp:getProperty name="userData" property="monthlyFee" />
which only has one getter method without setter method
(which we do not need)
– We put our programming logic in getMonthlyFee() method.
Example of using Java Beans
package foo;
<jsp:useBean id="userData"
class="foo.UserInfo" scope="request" />
<jsp:setProperty name="userData"
property="*" />
<html><head>
<meta content="text/html; charset=ISO-88591" http-equiv="contenttype"><title>process</title>
public class UserInfo {
private String name;
private int age;
public void setName(String sname){
name = sname;
}
public String getName(){
return name;
}
public void setAge(int sage){
age = sage;
}
</head><body>
Thanks, <jsp:getProperty name="userData"
property="name" /> !<br>
<br>
Your monthly payment for the sports club is $
<jsp:getProperty name="userData"
property="monthlyFee" /> .
</body></html>
public int getAge(){
return age;
}
public int getMonthlyFee(){
int payment;
if (age <6)
payment =0;
else if (age < 18)
payment = 10;
else if (age < 60)
payment = 30;
else
payment = 10;
return payment;
}
}
<jsp:getProperty> calls
corresponding getter
method
Scriptless JSPs using Expression
Language (EL)
• EL stands for Expression Language
• It has some implicit objects
– Such as
• param
– Use this when you have only one parameter for that particular
parameter name
• paramValues
– Use paramValues when you might have more than one
parameter value for a given parameter name
Example of using EL
Example of using EL
Example of using EL
Example of using EL
Example of using EL
You can use EL for Java Bean also
<jsp:useBean id="userData" class="foo.UserInfo" scope="request" />
<jsp:setProperty name="userData" property="*" />
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>process</title>
</head><body>
Thanks, ${userData.name} !<br>
<br>
Your monthly payment for the sports club is $
${userData.monthlyFee}.
</body></html>
We can use
${Name_of_Bean.name_of_property}
to access Java Bean’s property easily
without resorting to
<jsp:getProperty>