Chapter 8 scriptless JSP

Accessing the JavaBean object using Standard Action (<jsp: >)

<jsp:useBean id="person" class="foo.Person" scope="request" />
<jsp:getProperty name="person" property="name" />

*jsp:useBean action tries to find the attribute name in the defined scope when there is any attribute match id name, it creates a new object using the class info defined in the class attribute.

*The useBean’s id attribute and the getProperty’s name should match.
getProperty’s property attribute is for getter and setter.

*setProperty is to set the member of the java Bean object.
<jsp:setProperty name="person" property="name" value="Fred" />

* if setProperty tag is used within useBean tag body, then it is used only conditionally when the bean object is created

<jsp:useBean id="person" class="foo.Person" scope="page" >
	<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>


Java Bean law : 1. Public no-arg constructor , 2. getter & setter (setter arg and getter return type must identical)

*<jsp:useBean id="person" type="foo.Person" class="foo.Employee" scope="page" />
Type attribute is to declare object and class attribute is to create the object (calling the construct)
Type can be class, abstract or interface
Class must be subclass or concrete implementation of Type attribute

*If only TYPE attrib is used w/o Class attrib, the bean must already exist. If so InstantiateionException occurs even if type defined is concreate class.
*If Class is used (with or w/o type) , the class must not be abstract and must have a public no-arg constructor.

* default scope of useBean is PAGE scope.

*<jsp:setProperty name="person" property="name" param="username" />

- By defining param, you tell it to use form parameter whose its name is “username”
- If form input field name is the same as property name , setProperty tag automatically assigns to the javabean’s properly without param attribute
- If the form input fields all match with javabean’s properties, you can use “*” for Property to read all from request.

<jsp:setProperty name="person" property="*">

(*) EL (Expression Language) Operator

Access Javabeans object’s member’s member. ==> ${person.dog.name}

1. Using dot (.) operator

${person.name}

First named variable in the expression is either implicit object or an attribute name
It should be java.util.Map or bean object

Implicit object : all MAP object except pageContext Attribute name in
pageScope : map - only Attributes in page
requestScope : map - only Attributes in request
sessionScope : map - only Attributes in session
applicationScope : map - only Attributes in application
param : map
paramValues : map
header : map
headerValues : map
cookie : map
initParam : map - Context Init parameter (Not servlet)
pageContext : this is actual reference pageContext
page scope
request scope
session scope
application scope

Second named variable(right side of dot) is either Key name of map entry if map object or bean property name if bean object.
Must start with a letter, _, or $ (Can’t start with a number)

2. Using [ ] operator

${person["name"]} same as ${person.name}

First name can be Map, Bean, and addtionally List or Array. But when List or Array used , second name should be NUMBER(with or without quotes) to refer to the index position of the array.
${array[1]} same as ${array["1"]}

If second name is not number and used without quotes, it means to search all scopes for the attribute name and use its value as the key name.
${test[key1]} means to search scopes for attribute named ‘key1′ and use it’s value as the map key or property name

* requestScope implicit object is just a MAP object contains attributes of request scope. In order to access other bean property of request scope, you need to use pageContext object.
${pageContext.request.method} ==> request.getMethod()

(*) Define custom functions in EL using Taglib .tld file.

  1. JavaClass with a public static non-void return function
  2. Write a Tag Library Descriptor(.tld) file
  3. Put a taglib directive in your JSP setting prefix
  4. use EL to invoke the function

TLD file

<taglib … >
	<tlib-version>1.2</tlib>
	<uri>MyFunctions</uri>
	<function>
		<name>name</name>
		<function-class>com.example.MyFunctions</function-class>
		<function-signature>int myname(java.lang.String name)</function-signature>
	</function>
</taglib>

JSP file

<$@ taglib prefix="myf" uri="MyFunctions" %>
${myf:name("chris")}

(*) EL operator

  • Arithmatic : + , -, * , / or div , % or mod
  • Logical : && or and , || or or , ! or not
  • Relational : == or eq , != or ne , < or lt , > or gt , <= or le , >= or ge
  • True, false, null, instanceof, empty

EL is null friendly : Arithmatic null becomes 0 and Logical null becomes false
Also note that there is no assignment in EL.

${ 10 eq 10 div 0 } ==> This EL evaluates to false
Explanation: standard operator evaluation order applies.
step 1. (10 div 0) is evaluated first which is infinity,
step 2. then (10 eq Infinity ) is evaluated which is false.

(*) Include directive and standard action comparison
<%@ include file ="header.jsp" %>
<jsp:include page="header.jsp" %> header.jsp is compiled and translated for each request on the JSP.

The include directive inserts the content of header.jsp at the JSP translation/compile time. But the standard action “include” inserts the RESPONSE of Header.jsp at Runtime.

Passing variables to the included page.

<jsp:include page="header.jsp">
	<jsp:param name="subtitle" value="This is a subtitle" />
</jsp:include>

<jsp:param> tag is used within the body of jsp:include & jsp:forward tag.
Then within header.jsp , ${param.subtitle}

<jsp:forward page="another.jsp" />
When <jsp:forward> is used after bufferd output is commited, IllegalStateException will occur. But it will not be shown to user, just forward will not work and the output will stop.

(*) Some comments from me
* For multi value inputs
${param.hobbies } and ${param.hobbies[0]} will be the same

*${initParam.master-email} will cause exception because the ‘-’ will be interpreted as arithmetic calculation.
You should use ${initParam["master-email"]} when ‘-’ exists in the name of the key or property.

* When there is Attribute named ‘foo’ in the request object ${requestScope.foo} and ${pageContext.request.foo} are not the same.
You will not be able to access attribute using pageContext because only bean Method name is valid if the object is bean object.
But ${foo} is the same as ${requestScope.foo} if attribute named ‘foo’ is defined only in request scope.

(*) Next: [SCWCD] Cram Sheet 6 - using JSTL



One Response to “[SCWCD] Cram Sheet 5 - scriptless JSP”

  1.   Remember the code? » Blog Archive » [SCWCD] Cram Sheet 4 - using JSP Says:

    [...] Next: [SCWCD] Cram Sheet 5 - scriptless JSP Posted by HanaDaddy Filed in java Tags: java, SCWCD [...]

Leave a Reply