Presentation is loading. Please wait.

Presentation is loading. Please wait.

Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages.

Similar presentations


Presentation on theme: "Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages."— Presentation transcript:

1

2 Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages

3 What’s irritating about Custom Tags? The tag-extension protocol is too complicated Tag handler  doStartTag()doEndTag() doCatch()doFinally() doInitBody() doAfterBody() release() Too hard for Gosling, even?

4 What’s irritating about JSP? Also, tags don’t support certain kinds of code reuse. ”> : out.println(…); for(…) { out.println(…); … } 

5 What’s bad about JSP? (  ) The general consensus says… Scriplets They complicate abstraction and code reuse. They make it harder for nonprogrammers to maintain pages

6 JSP 2.0 Sun introduced the solution to these problems in JSP 2.0. It addresses theses issues by introducing Expression language Tag files Simplified Tag API (SimpleTag versus Tag) Improved XML syntax JSTL, although it is not part of JSP specificiation.

7 JSP Expression Language JSP became beautiful after this The motivation is to lose scriplets whatsoever. Also non java programmers should be able to use it easily, because it is just an expression language. It was inspired by xpath and java script except it is much easier than any of them

8 EL syntax Expressions appear between ${ and }. Note that ${ and } may contain whole expressions, not just variable names E.g., ${myExpression + 2} Access JavaBean properties using. (dot) ${book.title} translates to book.getTitle() Access to Array and Collection elements using collection[] Access to a Map value by key using map[“key”]

9 EL syntax EL searches each scope area if a scope isn’t specified Specific scope areas can be used: ${pageScope.title} ${requestScope.title} ${sessionScope.title} ${applicationScope.title} ${duck.beakColor} can resolve to ((Duck) pageContext.getAttribute(”duck”)).getBeakColor() Automatic Casting, wow

10 EL Operators Arithmetic +, -, *, / (or div), % (or mod) – note on XPath Relational == (or eq), != (or ne), < (or lt) > (or gt), = (or ge) Logical && (or and), || (or or), ! (or not) Validation empty null values Collections or Arrays that are empty Strings that evaluate to “”

11 EL Operators (Examples) Arithmetic${age + 3} Comparisons${age > 21} Equality checks${age = 55} Logical operations${young or beautiful} Emptiness detection${empty a} ‘a’ is empty String (“”), empty List, null, etc. Useful for ${empty param.x}

12 Implicit objects in EL Param and paramvalues Access HTTP request parameters Example: ${param.password} Header and headervalues Request header information Example: ${header[“User-Agent”]} Initparam (for the context init parameters of the webapp) Example: ${pageContext.request.remoteUser} Cookies Example: ${cookie.crumb}

13 EL: Uses JSTL 1.0 introduced the EL, but it could be used only within tags. In JSP 2.0, it can be used almost anywhere Hi, ${user}. You are years old.

14 JSTL JSP Standard Tag Library Set of tags that should be available in all compliant JSP containers Promotes rapid application development at the web page layer

15 Why JSTL? A big reason is that writing your own custom actions, or tags, can be a pain There was lots of reinventing the wheel going on, for tasks like displaying a date in a special format Many open source libraries sprung up, but there was still no standard This fueled the fire behind JSTL

16 JSTL: Features Control flow Iteration, conditions URL management Retrieve data, add session IDs Text formatting and internationalization Dates and numbers Localized messages XML manipulation XPath, XSLT Database access Queries, updates (you do not need to write java any more )

17 JSTL: Libraries Library featuresRecommended prefix Core (control flow, URLs, variable access) c Text formattingfmt XML manipulationx Database accesssql

18 Managing variables Outputting values with EL Storing data <c:set var=”user” scope=”session”> // arbitrary text Note the use of “var” and “scope”: a JSTL convention

19 Iteration A loop with a start, end, and step (typical for statement) <c:forEach var="name" varStatus="name" begin="expression" end="expression" step="expression"> body content A loop using an Iterator for a collection <c:forEach var="name" items="expression" varStatus="name" begin="expression" end="expression" step="expression"> body content

20 Example Value Square <c:forEach var="x" begin="0" end="10" step="2"> ${x} ${x * x}

21 Conditional logic Conditional evaluation a equals b Mutually exclusive conditionals a equals b a equals c I don’t know what ’a’ equals.

22 Example This is an insecure Web session. This is a secure Web session. You are using an unrecognized Web protocol. How did this happen?!

23 URL management Retrieving data <c:import var=”cnn” url=” http://www.cnn.com/cnn.rss ”/> Data exposed as String or Reader All core URLs supported (HTTP, FTP, HTTPS with JSSE) Local, cross-context imports supported Printing URLs Redirection

24 Text formatting Locale-sensitive formatting and parsing Numbers Dates Internationalization Message bundles Message argument substitution “Hi {0}. I would like to {1} your money today. I will use it to buy myself a big {2}.”

25 Example ${blogEntry.title} ${blogEntry.text} [Posted <fmt:formatDate value="${blogEntry.created}" pattern="h:mm a zz"/>]

26 database manipulation Queries (and ResultSet caching) Updates / inserts Transactions ( ) Parametric ( PreparedStatement ) argument substitution ( ) DataSource-based connection management …

27 XML manipulation Use of XPath to access, display pieces of XML documents http://www.cnn.com/cnn.rss Chaining XSLT transformations

28 Why Not XSLT JSTL integrates XPath with convenient, standard access to Java/JSP code. E.g., parse an article URL out of a document, then follow the URL and parse its contents. JSP/JSTL may be more familiar and convenient for simple tasks. Functional versus imperative programming

29 Adding New Tag There are two ways to add news tags Extend the JSTL and override methods Define Tag files (This is much easier, so we gone study this )

30 Tag Files Solve difficulty of reusing text/HTML within a tag. And makes it much easier to write simple tags, since you can do so in JSP instead of Java. Stand-alone file with directive instead of traditional directive.

31 Tag Files Name IQ ${i.fullName} ${i.IQ}

32 Using new Tag.... Your shopping cart: Your wish list: Things we want you to buy:


Download ppt "Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages."

Similar presentations


Ads by Google