33
1. Installing JSTL : a. JSTL is not part of JSP 2.0 spec b. Having access to Servlet and JSP API’s does not mean u have access to JSTL c. Put jstl.jar and standard.jar in WEB-INF/lib 2. Standard library of custom tags : JSP Standard Tag Library 3. If you need more you can create a custom tag library developed for ur own company 4. U wud learn how to use core jstl tags as well as custom tags from other libraries 1. Even if u don’t mention escapeXml then by default special characters are converted to entity code using <c:out, so that they can be displayed as it is 2. Setting a default value with <c:out

Document9

Embed Size (px)

Citation preview

Page 1: Document9

1. Installing JSTL : a. JSTL is not part of JSP 2.0 specb. Having access to Servlet and JSP API’s does not mean u have access to JSTLc. Put jstl.jar and standard.jar in WEB-INF/lib

2. Standard library of custom tags : JSP Standard Tag Library3. If you need more you can create a custom tag library developed for ur own company4. U wud learn how to use core jstl tags as well as custom tags from other libraries

1. Even if u don’t mention escapeXml then by default special characters are converted to entity code using <c:out, so that they can be displayed as it is

2. Setting a default value with <c:out

Page 2: Document9

But we don’t want “space”

So

Looping without scripting

Page 3: Document9
Page 4: Document9

1. Collection here means either an array or Collection or Map or comma delimited string

2. Getting loop counter

Page 5: Document9

3.

4. Using c: as prefix is a convention , u can give other prefix but u must give one5. The var variable is scoped only to the tag

Page 6: Document9

6.

7.

Page 7: Document9
Page 8: Document9

So what do we do

Page 9: Document9

8. <c:set>With <jsp:setProperty> u can just set the property oif beans

But with <c:set> you can set request attributes, map values,new entry in map

<c:set> comes in 2 flavours

1. Var2. Target

Each of the 2 flavors have 2 versions with and without body

Page 10: Document9
Page 11: Document9
Page 12: Document9

<c:remove>

Page 13: Document9
Page 14: Document9
Page 15: Document9
Page 16: Document9

487 onwards from book

Page 17: Document9
Page 18: Document9

1. Session tracking happens automatically with JSP’s unless you disable with a page directive that has an attribute that says session=”false”.

2. If the client does not support cookies however, then use <c:url> in JSP they provide URL rewriting

What if URL needs encoding

1. If unsafe/reserved characters like “space”are appended in query string they can create problems but with encoding they are encoded.eg a space is replaced with a + sign

Page 19: Document9
Page 20: Document9

Error pages

Page 21: Document9

1. U can declare error pages in DD for entire web app2. U can even configure error pages for different exceptions types or HTTP error

codes(404,500,etc)3. The container uses<error-page> configuration in DD as default but if a JSP has an explicit error

page directive , the container uses the directive

Page 22: Document9

1. In a scriptlet u can use the implicit object exception2. From JSP u use the EL implicit object ${pageContext.exception}3. The object is of type java.lang.Throwable4. So in script u can call methods and in EL u can access stackTrace and message properties

Page 23: Document9

1. <c:catch> serves as both try and catch there’s no separate try tag2. u wrap risky EL or tag calls or whatever in the body of a <c:catch> and the exception is caught

right there3. nothing runs in the <c:catch> body once an exception occurs4. so it is like try in java

Page 24: Document9

Now since we have not moved to error page we don’t have access to exception object

So what do we do?

Page 25: Document9

JSTL has 5 libraries : 4 with custom tags and one with a bunch of functions for String manipulation

Page 26: Document9

Using a tag library that’s not from JSTL

1. with JSTL docs u knowhow to use JSTL custom tags but how do u use a custom tag library created for your company

2. how do u deploy it

Things u have to know

1. the tag name and syntaxa. tag name comes from TLD : in <c:set>, tag name is set and prefix can be anythingb. syntax tells required and optional attributes, if tag can have body, if so what to put

there,c. type of attribute and if the attribute can be expression

2. the library URIa. URI is unique identifier in TLDb. Its what u put in ur taglib directivec. Used to map tag to java code

Page 27: Document9
Page 28: Document9
Page 29: Document9

1. This simple tag handler extends SimpleTagSupport2. Implements 2 key methods

a. doTag() : does the actual workb. setUser() : accepts the attribute value

Page 30: Document9
Page 31: Document9