Building JSP Pages Using Standard Actions
Building JSP Pages Using Standard Actions
Durga
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
173 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Agenda:
1) <jsp:useBean> tag
2) <jsp:setProperty> tag
3) <jsp:getProperty> tag
4) <jsp:include> tag
5) <jsp:forward> tag
6) <jsp:param> tag
7) <jsp:plug-in> tag
8) <jsp:params> tag
9) <jsp:fallback> tag
10) Summary of Standard actions
11) JSP Documentation
Introduction :
case 1: problem
OR
case 2 :
public class JspDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname = request.getParameter("userName");
foo.Person p = new foo.Person();
p.setName(uname);
request.setAttribute("person",p);
RequestDispatcher rd = request.getRequestDispatcher("view1.jsp");
rd.forward(request, response);
}
}
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
174 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
view1.jsp
Welcome to :
<%@page import="foo.Person"%>
Welcome to :
<%
Person p=(Person)request.getAttribute("person");
out.println(p.getName());
%>
Person.java
package foo;
public class Person {
String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Solution : view1.jsp
Problem : demo.jsp
<%!
public int squareIt(int n) {
return n*n;
}
%>
<h1>The square of 4 is : <%= squareIt(4) %></h1>
<h1>The square of 2 is : <%= squareIt(2) %></h1>
There is no clear separation of presentation logic and business logic. The person who is writing
the jsp should have compulsory knowledge on both java & html which may not be possible
always, this approach doesn't promote re-usability .
It reduces readability.
We can resolve these problem by separating entire business logic inside java bean.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
175 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
javabean :
package foo;
public class Calculator {
public int squareIt() {
return n*n;
}
}
view.jsp
Separation of presentation and business logic (presentation logic available in Jsp and business
logic available in java bean) , so that readability is improved.
Java developer can concentration on business logic where as Html developer can
concentration on presentation logic as both can work simultaneously so that we can reduce
development time and increases productivity.
It promotes re-usability of the code i.e., where ever this squareIt() functionality is used we can
reuse the same bean with rewriting.
We can purchase bean for file uploading and we can start uploading of the file with in
minutes.
Note : It is never recommended to write scriptlets, expressions, declarations and business logic with in
the Jsp.
To use useBean inside jsp the bean class has to follow the following rules
Jsp engine is responsible to perform instantiation of Java bean for this always executes public
no argument constructor otherwise <jsp:useBean> tag won't be work. (because it internally
calls that setter method only)
For every property bean class should contains public getter methods
otherwise<jsp:getProperty> tag won't be work.(because it internally calls corresponding
getter method only)
It is highly recommended to keep bean class as part of some package.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
176 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
<jsp:useBean> :
We can use this tag to make bean functionality available to the Jsp, There are 2 forms of
<jsp:useBean>
without body :
with body :
The main objective of body is to perform initialization for the newly created bean object, The bean
object is already available then it won't be created new bean object it will reuse existing object only ,
in this case body won't be executed.
1. id
2. class
3. type
4. scope
5. beanName
id :
This id attribute represents name of the reference variable of the bean object.
By means of this id attribute only we can access bean in rest of the Jsp's
This attribute is a mandatory attribute.
class :
This attribute represents fully qualified name of the Java bean class, for this class only jsp
engine perform instantiation hence the value of class attribute should be concrete class.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
177 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
type :
scope :
This attribute specifies in which scope jsp engine has to search for the required bean object, in
that scope if the bean object is not already available then Jsp engine creates a bean object and
store into specified scope for the future purpose.
The allowed values for the scope attributes are page, request, session, application.
This attribute is optional and default scope is "page".
If we specify only type attribute without class attribute then bean object should be already available
with in the specified scope if it is not already available then we will get instantiation exception.
<jsp:useBean id="c" type="CustomerBean" scope="session" />
To use session scope compulsory session object should be available otherwise we will get translation
time error.
<%@page session="false" %>
<jsp:useBean id="c" class="CustomerBean" scope="session" />
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
178 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Exception: Illegal for useBean to use session scope when jsp page declare that it doesn't participate
into the session.
package foo;
public class CalculatorBean {
static int i = 0;
public CalculatorBean() {
System.out.println("calculator bean object created"+ (++i)+"times");
}
public int squareIt(int n) {
return n*n;
}
}
test.jsp
<jsp:useBean id="calc" class="foo.CalculatorBean" scope="session" />
The square of 4 is : <%= calc.squareIt(4) %>
The square of 5 is : <%= calc.squareIt(5) %>
<a href="beanTest.jsp">Click Here</a>
beanTest.jsp
<jsp:useBean id="calc" type="foo.CalculatorBean" scope="session" />
The square of 8 is : <%= calc.squareIt(8) %>
The square of 9 is : <%= calc.squareIt(9) %>
case 2 :
<jsp:useBean id="calc" type="foo.CalculatorBean" scope="session" >
The square of 8 is : <%= calc.squareIt(8) %>
The square of 9 is : <%= calc.squareIt(9) %>
</jsp:useBean>
case 3 :
<jsp:useBean id="c" type="foo.CalculatorBean" scope="session" >
The square of 8 is : <%= calc.squareIt(8) %>
The square of 9 is : <%= calc.squareIt(9) %>
</jsp:useBean>
exception: javax.servlet.ServletException:java.lang.InstantiationException
person.java
package foo;
public abstract class Person {
public abstract String getMessage();
}
Student.java
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
179 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
package foo;
public class Student extends Person {
public String getMessage() {
return "Ashok";
}
public String getName() {
return "Aggidi";
}
}
test.jsp
<jsp:useBean id="p" type="foo.Person" class="foo.Student" scope="session" />
The person name is : <%= p.getMessage() %>
<a href="beanTest.jsp">Click Here</a>
beanTest.jsp
<jsp:useBean id="person" class="foo.Student" scope="session" />
The student name is : <%= person.getName() %>
beanName :
There may be a chance of using serialized bean object from local file system in that case we have to
used beanName attribute.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
180 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Assume that no CustomerBean object is already created then which of the following standard actions
create a new bean object and store in request scope ?
<jsp:getProperty> :
//equalant jsp
1. name
2. property
name :
It represents name of the bean object reference variable from which the required property has to
retrieve , it is exactly equal to id attribute of <jsp:useBean>.
property :
It represents the name of the java bean property whose value has to retrieve.
package foo;
public class CustomerBean {
private String name = "ashok";
private String mail = "ashok@jobs4times.com";
public String getName() {
return name;
}
public String getMail() {
return mail;
}
}
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
181 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
index.jsp
<%@page import="foo.CustomerBean" %>
<%
CustomerBean c=new CustomerBean();
out.println("The customer name is :"+c.getName());
out.println("The customer mail is :"+c.getMail());
%>
(OR)
<jsp:setProperty> :
We can use this tag to set the properties of bean object , <jsp:setProperty> tag contains the following
forms
form 1 :
<jsp:setProperty property="age" name="c" value="30" />
(OR)
c.setAge("30");
form 2 :
<jsp:setProperty property="age" name="c" param="age" />
(OR)
c.setAge(request.getParameter("age"));
Note : The param attribute to retrieve the request parameter value "age" and assign in to java bean
property age.
form 3 :
<jsp:setProperty property="age" name="c" />
(OR)
c.setAge(request.getParameter("age"));
Note: To get this type of advantage it is highly recommended to maintain bean property names same
as form parameter names.
form 4 :
<jsp:setProperty property="*" name="c" />
// "*" specifies all the properties of java bean
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
182 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Note : It iterates all request parameters and if any request parameter name match with java bean
property name then assign request parameter value to the java bean property.
1. name
2. property
3. value
4. param
name :
It represents name of the reference variable of the bean object whose property has to set, this is
exactly same as id attribute of <jsp:useBean> , It is mandatory attribute.
property :
It represents name of the java bean property which has to set, it is mandatory attribute.
value :
It specifies the value which has to set to the java bean property, it is an optional and should not come
combination with param attribute.
param :
This attribute specifies name of the request parameter whose value has to set to the java bean
property, it is an optional attribute and should not come combination with value attribute.
CustomerBean.java
package foo;
public class CustomerBean {
private String name = null;
private String mail = null;
private String age = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
183 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
index.jsp
<form action = "test.jsp" method="post">
<h2>Enter Your Details :</h2>
Enter Name : <input type="text" name="uname">
Enter Email : <input type="text" name="mail">
Enter Age : <input type="text" name="age">
<input type="submit" value="submit">
</form>
test.jsp
<h1>Set Property Example : </h1>
<jsp:useBean id="c" class="foo.CustomerBean" />
<jsp:setProperty property="*" name="c"/>
Entered Name is :
<jsp:getProperty property="name" name="c" />
Entered Mail is :
<jsp:getProperty property="mail" name="c" />
Entered Age is :
<jsp:getProperty property="age" name="c" />
Case 1 :
test.jsp
<h1>Set Property Example : </h1>
Entered Name is :
<jsp:getProperty property="name" name="c" />
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
184 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Entered Mail is :
<jsp:getProperty property="mail" name="c" />
Entered Age is :
<jsp:getProperty property="age" name="c" />
Note : Automatic String to primitive conversions doesn't work if we use scripting elements, it fails
even if we use expression inside <jsp:setProperty> standard actions.
Note : If we are using scripting elements inside jsp standard actions automatic conversions doesn't
work but it is possible scripting elements inside standard actions.
1. <jsp:include>
2. <jsp:forward>
3. <jsp:param>
<jsp:include> :
The response of include page will be included in current response at request processing time, hence it
is a dynamic include.
1. page
2. flush
page : page attribute represents name of the included page and it is mandatory.
flush : It specifies whether the response will be flushed before inclusion or not , it is an
optional attribute and default value is false.
index.jsp
<jsp:include page="header.jsp" />
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
185 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
<h2>Welcome to Jobs4Times.com</h2>
<jsp:include page="footer.jsp" />
header.jsp
<h2> We are Master in Java Certification </h2>
footer.jsp
<h4>copyright © www.jobs4times.com </h4>
4) By using RequestDispatcher :
<%
RequestDispatcher rd=request.getRequestDispatcher("header.jsp");
rd.include(request,response);
%>
<jsp:forward> :
If the first jsp is responsible to perform some preliminary processing activities and second jsp is
responsible for providing complete response then we should go for forward mechanism.
Syntax :
first.jsp
<h2>This is first Jsp page </h2>
<jsp:forward page="second.jsp" />
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
186 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
second.jsp
<h2>This is second Jsp page </h2>
3. By using RequestDispatcher :
<%
RequestDispatcher rd=request.getRequestDispatcher("header.jsp");
rd.forward(request,response);
%>
<jsp:param> :
While forwarding or including if we want to send parameters to the target Jsp we can achieve this by
using <jsp:param> tag
<jsp:param> tag defines the following 2 mandatory attributes
Note : The parameters which are sending by using <jsp:param> tag are available as form parameters
in target Jsp.
first.jsp
<jsp:include page="second.jsp">
<jsp:param value="ashok" name="username"/>
<jsp:param value="25" name="age"/>
</jsp:include>
second.jsp
<%@page isELIgnored="false" %>
<h2>Hi i'm getting the values from Jsp param</h2>
The UserName is : <%=request.getParameter("username") %>
The Age is : <%=request.getParameter("age") %>
Conclusions :
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
187 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
case 1 :
The value of page and file attributes should be a relative path or absolute path but not server port,
protocol and etc.,
case 2 :
In the case of forward and include actions page attribute can pointing to a servlet.
But in the case of include directive file attribute can't pointing to a servlet but it can pointing to a
Jsp,html,xhtml,xml and etc.,
case 3 :
In the case of include and forward actions we are allowed to pass query string,
But in the case of include directive we are not allowed to pass query string.
<jsp:plugin> :
Using <jsp:plugin> action provides the support for including java applet in a jsp page,
If we have java applets that are part of your web-applications.
While it's possible to have appropriate html code embedded in your Jsp page, the use of
<jsp:plugin> allows the functionality to the browser is neutral.
There are the following 2 optional support tags those work with <jsp:plugin>. i.e., <jsp:params> and
<jsp:fallback>
<jsp:params> :
<jsp:fallback> :
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
188 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Which specifies any content that should be display to the browser, if the plugin is not started or
because of some runtime issues.
A plugin specific message will be presented to the end user.
A translation time error will occurs if we use <jsp:params> , <jsp:fallback> any other context other
than child of <jsp:plugin>.
Syntax :
<jsp:plugin code="classFileName .class extention"
codebase="The directory of class file name" type="applet/bean" >
{align="alignment"}
{height=""}
{width=""}
{ <jsp:params>
{ <jsp:param value="pvalue" name="pname"/> }
</jsp:params> }
{} ---->optional attributes
type :
The type of object plugin will execute you must specify either applet or bean, as this attribute there is
no default value.
It is a mandatory attribute.
type="applet/bean"
code : (class="ClassFileName")
The name of the java class file that plugin will execute we must include .classextension, file name is
relative to the directory named in codeBase attribute , it is also mandatory attribute.
codeBase : (codeBase="ClassFileDirectoryName")
This is a absolute path or relative path to the directory that contains applet code, it is also mandatory
attribute.
index.jsp
<jsp:forward page="plugIn.jsp"></jsp:forward>
plugIn.jsp
<h2>Welcome to Jsp Application</h2>
First Applet :
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
189 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Second Applet :
<jsp:plugin code="edu.com.MessageApplet.class"
codebase="applets" type="applet" width="500" height="100">
<jsp:params>
<jsp:param value="Welcome to SCWCD" name="msg"/>
</jsp:params>
<jsp:fallback>No support to this Applet ..... </jsp:fallback>
</jsp:plugin>
ClockApplet.java
public class ClockApplet extends Applet implements Runnable {
String time;
public void init() {
Thread t = new Thread(this);
t.start();
}
while(true) {
time = String.valueOf (new SimpleDateFormat("hh:mm:ss").format(new Date()));
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//run
}
}//class
MessageApplet.java
package edu.com;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
190 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
first.jsp
<h2>This is first Jsp Page</h2>
<%! String nextPage="second.jsp"; %>
<jsp:forward page="<%= nextPage %>" /> //valid
// static or dynamic
<h2>This is after forwarding</h2>
second.jsp
<h2>This is second Jsp Page</h2>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
191 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
JSP Documentation :
Jsp Standard Syntax : If we are writing a Jsp by using Jsp standard syntax such type of Jsp's are
called Jsp pages.
Xml based syntax : If we are writing Jsp by using Xml based syntax such type of Jsp's are called
Jsp document.
directives :
directives Standard Syntax XML based Syntax (all are case sensitive)
page <%@page import="java.util.*" %> <jsp:directive.page import="java.util.*" />
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
192 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
Note :
<jsp:root xmlns:mime="www.jobs4times.com" version="4.3" />
//version is mandatory attribute
//xmlns:mime is an optional attribute
//and we can take any no.of xml name spaces
Standard actions :
There is no difference in standard actions representation between standard syntax and xml based
syntax.
Ex :
Standard Syntax XML based Syntax
useBean <jsp:useBean id="c" class="CustomerBean" /> // same
Comments :
Jsp specification doesn't provide any specific syntax for writing comments , hence we can use normal
syntax
Jsp comment XML comment
<%-- --%> <!-- -->
Template text :
Jsp Standard syntax doesn't provide any specific way to write template text but Xml based syntax it
provides a special tag <jsp:text>
Jsp standard syntax XML based syntax
This is secons jsp <jsp:text> This is second Jsp </jsp:text>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
193 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
<web-app>
<jsp-config>
<jsp-property-group>
<url-pattern>/jspx/*</url-pattern>
<is-xml>true</is-xml>
</jsp-property-group>
</jsp-config>
</web-app>
Note : From servlet 2.4v onwards web-container can identify Jsp document automatically , no special
arrangement is reuired.
It is never recommended to use both standard and Xml based syntax simultaneously.
The main advantage of Xml based syntax is we can use Xml editors like Xml-spy for writing and
debugging jsp's
All xml based tags and attributes are case-sensitive and attributes enclosed either single single
quote('') or double quote("").
Person.java
package foo;
Dog.java
package foo;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JspDemoServlet.java
public class JspDemoServlet extends HttpServlet {
p.setDog(d);
request.setAttribute("person", p);
RequestDispatcher rd=request.getRequestDispatcher("view.jsp");
rd.forward(request, response);
}
}
view.jsp
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
195 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
It is never recommended to use Scripting elements in Jsp , hence the above solution is not proper
approach
<jsp:useBean id="person" class="foo.Person" scope="request" />
The Person's Dog name is :
<jsp:getProperty property="dog" name="person" />
output:
The Person's Dog name is : foo.Dog@1234...
//If request scope is not specified the output is null
Solution : ${Person.dog.name}
The <Jsp:getProperty> tag lets you access only property of bean attributes, there is no capability for
nested properties, where you want a property of property rather than property of attribute.
Standard Actions can handle String and primitive properties , if we know the standard actions can deal
with in a attribute of any type as long as all the attribute properties are String/primitives.
there is no capability for nested properties.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
196 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
ADV. JAVA With SCWCD/ OCWCD Material By Mr. Durga
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
197 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com