Professional Documents
Culture Documents
6 JSPAction
6 JSPAction
Standard Actions
• <jsp:useBean>
• makes a java bean component available to a page
• <jsp:getProperty>
• gets a property value from a JavaBean components and adds it to
the response
• <jsp:setProperty>
• Set a JavaBean property value
6
include.jsp
<!DOCTYPE html>
<html> <body>
<jsp:include page="header.jsp"/>
<jsp:include page=“info.jsp">
<jsp:param value="Wong" name="name"/>
<jsp:param value="LWL" name="campus"/>
</jsp:include>
header.jsp
<h3>ITP4912 Web Application development Frameworks</h3>
info.jsp
obtain parameter
name
<center>
<b><i>Name:
<%=request.getParameter("name")%>. </i></b> ,
<span style="font-style:italic">Campus:
<% out.print(request.getParameter("campus"));%>
</span> <br/>
IVE ICT Department Copyright © 2012
</center>
<hr >
9
<jsp:forward>
• Pass control to another JSP page or servlet
• the execution of the current JSP page is terminated and
control is fully passed to the target JSP
• Without Parameter
• <jsp:forward page=“pagePath” />
• With Parameters
• <jsp:forward page=“pagePath” >
• <one or more <jsp:param> actions
• </jsp:forward>
jsp:forward
• <jsp:forward page=“nextPage.jsp”>
• <jsp:param name=“aParam” value=“aValue” />
• </jsp:forward>
forward.jsp
<jsp:forward page="conditional.jsp">
<jsp:param name="chance" value="<%=rand%>" /> forward the control
</jsp:forward> to conditional.jsp
with a parameter of
chance with the
rand value
13
conditional.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<%
double rand = Double.valueOf(request.getParameter("chance"));
if (rand < 0.5) {
out.println(" I like <b>Web Server Programming with Java</b>");
} else {
out.println(" I like <b> Swing Programming with Java </b>");
}%>
</body>
</html>
14
Mark.jsp determines
the whether the student
is passed or failed.
passed.jsp
failed.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
Dear <%=request.getParameter("name")%> : <br/>
<p>
Sorry, You are failed.
</p>
</body>
</html>d
20
url:ex1form.jsp
ex1form.jsp
The
form
with url: formHandler.jsp
different form Hander.jsp
Progress form
input request.getParameter ()
request.getParameterValues
elements :
:
show the
result
Java Beans
• Used in JSP as a containers for dynamic content
properties
id: int (read/write)
name: String (read/write)
species: String (read/write)
This is Pet class
owner: String (read/write)
Pet()
methods
int getId()
void setId(int)
String getName()
void setName(String)
String getSpecies()
void setSpecies(String)
boolean getBirthDate()
String getOwner()
void setOwner(String)
25
the id attribute
• should be a valid Java variable name, e.g. starting with a
letter and lower case
the id must be unique in the page,
the class attribute
• creates an instance of the bean in the container
• Scope attribute define it shareable among JSPs (discuss
it later)
29
• Similar as
• <%
• ict.bean.Pet pet = new ict.bean.Pet();
• %>
An instance of
ict.bean.Pet is
created in the
page scope. The
variable name is
pet
How to use update the Property in Java Bean
<jsp:setProperty>
• Same as
• <%= pet.getName() %>
• <% out.println( pet.getSpecies()); %>
Output of Pet.jsp
37
The
form
with
different
input
elements url: formHandler.jsp form Hander.jsp
Progress form
request.getParameter ()
request.getParameterValues
:
:
show the
result
38
•
40
Attribute : value
• Set bean property at request time with input parameters
item is of type String
Get the input value form the request
<%
String name = request.getParameter("name");
int grade = Integer.parseInt(request.getParameter("grade"));
String[] course = request.getParameterValues("course");
%>
use jspExpression to update the property in the
the bean object
<jsp:setProperty name="s" property="name" value="<%=name%>"/>
<jsp:setProperty name="s" property="grade" value="<%=grade%>"/>
<jsp:setProperty name="s" property="course" value="<%=course%>"/>
41
course is the parameter name for the request. i.e input field name
course is the property name in the bean class
value = request.getParameter(“course”);
s.setCourse (value);
each input field name must match with the property in the bean
class.
• Notes:
• No action is taken if an input parameter is missing. No null is
passed
• Error page might be needed to guard against invalid input, like
inputting 123abc for numItems
44
Student.java
public class Student implements Serializable {
studentForm.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="studentByValue.jsp" method="get">
Name: <input type="text" name="name" value="" /> <br/>
Garde: <input type="text" name="grade" value="" /> grade 1 to 12<br/>
Course: <input type="checkbox" name="course" value="maths" /> Maths
<input type="checkbox" name="course" value="physics" /> Physics
<input type="checkbox" name="course" value="chemistry" />
Chemistry<br/>
<input type="submit" value="submit" />
</form>
</body>
</html>
46
studentByValue.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<jsp:useBean id="s" class="ict.bean.Student" scope="page" />
<%
String name = request.getParameter("name");
int grade = Integer.parseInt(request.getParameter("grade"));
String[] course = request.getParameterValues("course");
%>
<jsp:setProperty name="s" property="name" value="<%=name%>"/>
<jsp:setProperty name="s" property="grade" value="<%=grade%>"/>
<jsp:setProperty name="s" property="course" value="<%=course%>"/>
<jsp:forward page="studentInfo.jsp" />
</body>
</html>
47
studentByParam.jsp
studentByProperty.jsp
Request Controller
(Servlet) Data
Model
(JavaBean)
Response View
(JSP) Data