0% found this document useful (0 votes)
46 views33 pages

EL Is Patterned After Javascript and Xpath

This document discusses several topics related to Expression Language (EL) in JavaServer Pages (JSP), including: 1. How EL can be used to access bean properties, request parameters, and other implicit objects without using scriptlets. 2. Different EL operators like ${}, [], and how they can be used to access data. 3. How to call static Java methods from EL using Tag Library Descriptors (TLD). 4. The differences between using the <jsp:include> tag vs the <%@ include %> directive for including content across pages.

Uploaded by

Alok Chandna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views33 pages

EL Is Patterned After Javascript and Xpath

This document discusses several topics related to Expression Language (EL) in JavaServer Pages (JSP), including: 1. How EL can be used to access bean properties, request parameters, and other implicit objects without using scriptlets. 2. Different EL operators like ${}, [], and how they can be used to access data. 3. How to call static Java methods from EL using Tag Library Descriptors (TLD). 4. The differences between using the <jsp:include> tag vs the <%@ include %> directive for including content across pages.

Uploaded by

Alok Chandna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

EL is patterned after javascript and xpath

1. If no such attribute is found the useBean creates one.The generated code


lokks like

2. Why would this be required if you want to set the attribute if its not there
using <jsp: setProperty>
3. <jsp:useBean> can have body

4. With this body the generated servlet code will have if in generated code.

5. For use with JSP the property type should be either String or int, else it would
be a valid bean but wont work with JSP standard actions you have to rely on
scripting.
6. Polymorphic bean references :
a. Make person abstract

7.

8. Using type without class


a. If type is used without class, the bean must already exist
b. If class is used(with or without type), it must be concrete with public
no-arg constructor.

9. What if the request goes straight from html to jsp and u want to set some
bean property from request

This looks bad mixing standard actios and scriptlets


10.The way out : param attribute
a. It lets u set the value of a bean property to the value of a request
parameter.
b. just by naming the request parameter

11.param attribute is not necessary : the input field name becomes request
parameter name so if you match this name with bean property name you
dont have to specify param attribute

12.using * for all fields : if you match all fields with property names then it
gets even better.

13.automatic string to primitive conversion does not work if scripting is used

14.Nested property cannot be obtained using <jsp:useBean>

EL to the rescue

1. ${person.name} : the first named variable in the expression is either an


implicit object or an attribute.

2. THE [] operator
a. [] becomes useful when the left side is anything other than map or
bean
b. That means the thing the right can be number ,or anything that
resolves to a number or an identifier that does not fit the java naming
rules. For example you might have a Map key thats a String with dots
in the name(com.foo.trouble).

3. The EL for accessing an array is the same as the EL for List.


Even this works for both
musicList[1]

4. If its not a string literal its evaluated

5. You can use nested expressions inside the brackets

6. EL renders raw text including HTML.


i.e if u set an attributes value as <b></b> tags make things bold
then <b></b> gets converted to nothing coz its converted to HTML
So the output of

is

Coz the below jsp gets converted to

7. The same is true for JSP expressions and jsp:getProperty standard action

8. But we want the characters to be rendered as it is , i.e we want < to be


displayed.So we want to send &lt; in order for the user to seethe actual
< charcter.
9. But how?
The form action goes straight to the JSP, if only I could use request parameters just
using EL.

EL implicit objects

1. Request parameters in EL

2. More information from request

3. How to get request method?


Will this work :
Method is : ${requestScope.method}

NO no nooooooooo

4. Why not?
a. The implicit requestScope is not a request object its a map containing
request scoped attributes.
b. What we want(HTTP method) is a property of request object.
5. Use pageContext

6. Why use scope objects, if EL looks thru all the scopes,is it just in case there is
a naming conflict or something more?

Getting Cookies and init params


1. Printing the value of username cookie
a. ${cookie.userName.value}

2. Printing the value of context init parameter

a. initParam.mainEmail
3. The EL initParam is not for params confihured using<init-param>

Calling java methods thru EL

The 4 step process


1. Write a java class with a public static method.
a. It can have any return type but ideally it should be returning non-void
only then can we show something.
b. Put it in WEB-INF/classes
2. Write a Tag Library Descriptor (TLD) file
a. It maps java class methods with function names JSP uses
b. So we can give more intuitive names to page designers to work with
rather then the stupid method names.
c. Put it in WEB-INF (though u can put it at other places also).
3. Put a taglib directive in your JSP.
a. The directive has prefix , so if function names match those can be
resolved.
4. Use EL to invoke the function
a. ${prefix:name()}

Note : the uri in the taglib directive tells the container the name of the TLD so the
container knows which method to call when JSP invokes EL function.
This uri matches the one specified in TLD

Points to remember :
1. Its not important that the method the EL invokes shud return something
2. The container finds the TLD by looking into WEB-INF and WEB-INF/lib, if .tld
are in jar
3. EL can have arguments , but the args must havefully qualified names
<function-signature>
int rollDice(java.util.Map)
</function-signature>

And call it like : ${mine:rollDice(aMapAttribute)}

A few other EL operators

An exercise :

EL is null friendly

Null is taken as zero in arithmetic and false in logical expression.

Layout templates
Reusable template pieces using include directive and jsp standard action
<jsp:include>
1. The include directive tells the container : copy everthing in this file and paste
it into this file, right here.
2. <%@ include file=header.jsp %>
3. <jsp: include page=header.jsp />

Difference between include directive and


include standard action

Directive happens at translation time and action happens at runtime


1. With directive its like copying the content to all pages and the container does
so
2. In the standard action the container creates a RequestDispatcher from the
page attribute and applies the include method.
3. The dispatched/included jsp executes against the same request and response
objects , and in the same thread.

Q. So why not use standard action always?


It is great if u feel the included file can change and also the size of the generated
jsp is small .
But the downside is that with every <jsp:include> there is a performance hit, while
for the directive it happens only once when it gets translated.
In some containers though the change in file is detected even without redeploying
but its not guaranteed by the spec , so thats dangerous to rely on.

Points :
1.
2. The include directive is position sensitive.

3.
4. How to remember where file and where page.
a. Directive happens at translation time when container cares about files
b. Action happens at runtime when container cares about pages.
5. The included page can be dynamic but it has limitations : it cannot set
response status code and it cannot set headers. If u try u wont get error but
it just wont happen

6. Do not put opening and closing html in your included files.

You might also like