JSP: Actions elements and JSTL

Slides:



Advertisements
Similar presentations
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Advertisements

 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT211/3 Internet Application Development JSP: Processing User input.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
DT228/3 Web Development multi page applications/ sharing data.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
DT211/3 Internet Application Development
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
DT228/3 Web Development JSP: Directives and Scripting elements.
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
Web programming for project students Dr Jim Briggs.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
JSP Standard Tag Library
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Chapter 7 Using Custom Tag Libraries and the JSP Standard Tag Library.
JSTL The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
DT228/3 Web Development JSP: Actions elements and JSTL.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
Web browser Web server HTML request Scripted pagedatabase The following components will be used: Apache Jakarta Tomcat servlet engine Derby JavaServer.
CS520 Web Programming Servlet and JSP Review
Tutorial 10 Programming with JavaScript
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
CS520 Web Programming Servlet and JSP Review
CS3220 Web and Internet Programming JSP Standard Tag Library (JSTL)
Java Servlets.
CS320 Web and Internet Programming JSP Standard Tag Library (JSTL)
Knowledge Byte In this section, you will learn about:
Intro to PHP & Variables
MSIS 655 Advanced Business Applications Programming
Chapter 27 WWW and HTTP.
JSP Standard Tag Library
PHP.
JSP Directives 1-Jan-19.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
JSP implicit objects & directive elements
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JSP: Actions elements and JSTL DT228/3 Web Development JSP: Actions elements and JSTL

JSP Action Elements JSP pre-defined tags (standard actions elements) Done External Tag library: ------- Custom Done ------- Java Standard tag library

External tag libraries – JSTL JSP Action elements: External tag libraries – JSTL JSP 1.2 introduced supported for a special tag library called the JSP Standard Tag Library (JSTL) Version 1.0 released in June 2002 Version 1.1 released in June 2004 Specification Evaluation 1.2 Maintenance Release 2 released in Aug 2006 The JSTL saves programmers from having to develop custom tag libraries for a range of common tasks, such as database access, conditional loops etc Enabled developers to produce more maintainable, simpler JSP code Important development for JSP technology

JSTL The JSP Standard Tag Library groups actions into four libraries as follows**: Library Contents Core Core functions such as conditional processing and looping, important data from external environments etc Formatting Format and parse information SQL read and write relational database data XMl Processing of XML data ** more may be added in later releases…

JSTL To use any of these libraries in a JSP, need to declare using the taglib directive in the JSP page, specifying the URI and the Prefix Library Prefix URI Core c http://java.sun.com/jsp/jstl/core Formatting fmt http://java.sun.com/jsp/jstl/fmt SQL sql http://java.sun.com/jsp/jstl/sql XMl xml http://java.sun.com/jsp/jstl/xml Example of declaring use of core library: <%@ taglib prefix = “c” uri = “http://java.sun.com/jsp/jstl/core %>

JSTL: Example Example: JSP page using JSTL that outputs 1 to 10 on a webpage using the <c:forEach> and <c:out> tags of the core library <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Count to 10 Example (using JSTL)</title> </head> <body> <c:forEach var="i" begin="1" end="10" step="1"> <c:out value="${i}" /> <br /> </c:forEach> </body> </html> A taglib directive declare use of core library JSTL tag examples

JSTL: Example <c:foreach> Looking more closely at <c:forEach tag> ….. <c:forEach var="i" begin="1" end="10" step="1"> <c:out value="${i}" /> <br /> </c:forEach> </body> </html> The <forEach> tag enables loop logic. In this case, will look through 10 times. Equivalent to java “for” loop Closing tag <c:forEach> placed after the end of body of loop

JSTL: Example <c:foreach> All JSTL tags have a set of attributes (similar to HTML tags..) e.g. <c:foreach> tag has 6 attributes: var, items, varStatus, begin, end, step The full details for each attribute is in the JSTL specification document (on distrib). See p 65 for <c:foreach> tag definition Willl need to use this document to verify WHICH tag should be used and HOW is should be used

JSTL: Example <c:out> <c:out> .. outputs a value to webpage. Usually uses just one attribute value Examples: <c:out value="${i}" /> <c:out value=“The result of 1 + 2 is ${1+2}” /> <c:out value=“param.userName" />

JSTL: Example <c:if> <c:if> .. evaluates a condition. Uses an attribute test to hold the condition Example : <%-- Simple if conditions --%> <c:if test='${param.p == "someValue"}'> Generate this template text if p equals someValue </c:if> Example 2 <c:if test='${param.p}'> Generate this template text if p equals "true“

JSTL: Multiple ‘if’ conditions An if/else action requires the use of the <c:choose> tag Syntax : <c:choose> body content (<when> and <otherwise> subtags) </c:choose> See page 52 of JSTL specification doc.

JSTL: Multiple ‘if’ conditions Uses <c:choose>, <c:when> and <c:otherwise> Example: <c:choose> <c:when test='${param.p == "0"}'> <c:out value = “zero recorded”/> </c:when> <c:when test='${param.p == "1"}'> Generate this <c:out value = “single value”/> <c:otherwise> <c:out value = “Set to ${param.p}”/> </c:otherwise> </c:choose>

JSTL: Other core <c:..> actions Other examples: (NOT a complete list!) <c:set> ….sets the value of a variable <c:remove> ….removes a scoped variable <c:catch> ….catches an exception <c:url> ….. encodes a URL <c:import>… imports the content of a resource <c:redirect>.. redirects to another URL <c:param>.. adds a request parameter to other actions

JSTL: <fmt:…..> example Library Prefix URI Core c http://java.sun.com/jsp/jstl/core Formatting fmt http://java.sun.com/jsp/jstl/fmt SQL sql http://java.sun.com/jsp/jstl/sql XMl xml http://java.sun.com/jsp/jstl/xml JSTL contains a set of actions in the Formatting library – these tags are useful for formatting numbers, times and dates e.g. <fmt:parseDate> (on page 140 of JSTL specification)

JSTL: <fmt:parseDate> example <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix=“fmt" %> <html> etc etc <fmt:parseDate value= “${param.empDate}” var = “parsedEmpDate type = “date” pattern = “yyyy-MM-dd” /> etc etc </html> The fmt:parseDate action takes the date or time string specified by the value attribute (e.g. 2001-09-28) , interprets it according to the pattern defined by the pattern attribute and saves it in a variable called “parsedEmpDate”… more later

JSTL: other <fmt> actions Other examples: <fmt:formatNumber> - formats a numeric value e.g. number of digits, currency, decimal place e.g. <fmt:formatNumber value="12.3" pattern=".000"/> will output “12.300” <fmt:formatDate> --formats a date and time

JSTL: Expression language Up to now, could only use Java expressions to assign dynamic values  syntax errors common JSTL now provides an expression language (EL) to support the tags  simpler syntax, less errors The EL is now part of the JSP specification (as of versions JSP2.0) – can be used in JSTL tags or directly in JSP pages.

JSTL: Expression language All EL expressions are evaluated at runtime The EL usually handles data type conversion and null values -- easy to use An EL expression always starts with a ${ and ends with a }

JSTL: Expression language The expression can include - literals ( “1”, “100” etc) - variables - implicit variables (more later) Examples: <c:out value = “${1+2+3}” /> <c:if test = “${param.Address == “D6”}” /> expression

JSTL: Expression language - operators == != < + - * / or div > <= >= Logical operators consist of && (or and), || (or or), and ! (or not). The empty operator is a prefix operator that can used to determine if a value is null or empty. For example: <c:if test=”${empty param.name}”> Please specify your name. </c:if>

JSP Implicit objects In JSP, need to be able to access information about the environment in which the page is running e.g. the parameters passed in a request for a form, the browser type of the user etc. Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements

JSTL implicit variables The JSTL EL allows these objects to be accessed as ‘Implicit Variables’ Implicit variable are just pre-agreed fixed variable names that can be used in JSTL Expressions -- Think of as “variables that are automatically available to your JSP page”..

JSTL: Expression language – Implicit Objects Full set of implicit variables on page 211 of JSTL documentation (includes: header, pageScope..) or on page 74 of O’Reilly’s Java Server Pages book Very common implicit object is param param refers to parameter passed in a request message (e.g. information entered into a form by a user). e.g. <c:out value = “${param.userName}”/> Further Examples of using param in next topic

Comparing JSTL vs Scriptlets JSTL removes complexity by using tags instead of java code (abstraction) JSP pages using JSTL usually easier to maintain JSTL allows HTML ‘tag’ developers to ‘program’ JSTL often more difficult to debug Note: Using JSTL does not eliminate scriplets entirely.. may still need them sometimes for more complex logic http://www.informit.com/isapi/product_id~{27F6F199-F5FD-474C-AC7F-B3FC2C1F57B6}/st~{94C03A97-1188-4875-8A06-17743BA224B7}/session_id~{6A766315-391B-4F55-9AFB-B5425F6196C5}/content/articlex.asp

Info on JSTL 1. Java Server Pages by Hans Bergsten – full reference on all JSTL tags in the Appendix. 2. Sun’s JSTL 1.0 tag specification on distrib. Good for definition of each tag. Poor on examples. 3. Specification Evaluation 1.2 Maintenance Release 2 on distrib.

Setting up JSTL in a web application JSTL is provided as part of Tomcat 5 Ensure the two JSTL .jar files are copied into the WEB-INF\lib directory of your web application Ensure the taglib directive(s) is in your JSP page