250 likes | 374 Views
DT228/3 Web Development. JSP: Directives and Scripting elements. JSP techniques. JSP provides variety of techniques to enable dynamic processing:. Directive elements. In this topic. Scripting elements (java). Action elements and Java Standard Tags Library. Java Beans.
E N D
DT228/3 Web Development JSP: Directives and Scripting elements
JSP techniques JSP provides variety of techniques to enable dynamic processing: • Directive elements In this topic • Scripting elements (java) • Action elements and Java Standard Tags Library • Java Beans
Development JSP Pages • Q: Do you need to use Java to develop JSP pages? • A: You can (using scripting elements), but you don’t have to and there are good reasons not to. Can use other techniques provided as part JSP technology, in particular: The Java Standard Tag Library (JSLT) – provides a set tags which provide the equivalent functionalty of writing java code in JSP pages.
Development JSP Pages: Template Text Template text= all text within the JSP page that is NOT JSP code For web sites, usually template text = HTML but Template text can be anything: WML for mobile devices, javascript, HTML, PDF etc HTML (or whatever) is just "passed through" to the client by the servlet created to handle the page. The HTML can be created by whatever tools you already are using for building Web pages. e.g. FrontPage
Development JSP Pages: Template Text example <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title> JSP is Eazy</title> </head> <body bgcolor="white"> <h1>JSP is as easy as …</h1> <%--Calculate the sum of 1+2+3 dynamically --%> 1+2+3=<c:out value="${1+2+3}" /> </body> </html>
Development JSP Pages: Comments A comment in a JSP page looks like: <%-- calculate the sum of x and z here --%> Everything between the <%-- and --%> is ignored by the JSP container The comments are JSP code elements so are NOT send to the browser (so not visible in View source) - Hidden comments
JSP Pages: Directive Elements • Directive Elements - Used to provide information about the general set-up of the page. e.g. inclusion of header files, name of page to report errors, whether session tracking is required etc • Always enclosed between <%@ …… %> • Syntax: <%@ directivename attribute = “value”, attribute = “value” …. %>
JSP Pages: Directive Elements • There are Three directives available to use: • <%@ page ….. > • <%@ include …… %> • <%@ taglib ….. %>
Directive Elements • Each directive has a set of associated attributes (similar to some tags in HTML) • Usually placed at top of JSP file but doesn’t have to be • Example: • <%@ page import="java.util.*, java.lang.*" %> • Full list of attributes available at: http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html
Directive Elements: Page Page directive - defines attributes that apply to an entire JSP page. Examples <%@page contentType = “text/html” %> <%@page language = “java” %> <%@ page errorPage="error.jsp" %>
Directive Elements: Page List of attributes includes <%@ page [ language="java" ][ import="{package.class | package.*}, ..." ][ session="true|false" ][ isThreadSafe="true|false" ] *multiple threads allowed or not[ info="text" ] *gives info about the page to administration[ errorPage="relativeURL" ][ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ][ isErrorPage="true|false" ] *Specifies whether exception object available or not%>
Directive Elements: Page • Details for all Page directive attribute can be found at http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html • Home page for this is located in ‘Web development’ on distrib/dt228/dt228-3/web development • E.g. extract for page directive
Directive Elements: Include Include directive - Includes a static file in a JSP file attranslation time Syntax <%@ include file="relativeURL" %> The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language Useful for including repetitive HTML content across a web site – headers, navigation bars, footers etc Useful for common logic – e.g. date displays
Directive Elements: Include Example: jsp page name = includexample.jsp <html><head><title>An Include Test</title></head><body bgcolor="white"><font color="blue">The current date and time is<%@ include file="date.jsp" %></font></body></html>
Directive Elements: Include Example (continued) jsp page name = date.jsp <%@ page import="java.util.*" %><%= (new java.util.Date() ).toLocaleString() %> Included into includexample.jsp from previous page When includexample.jsp is run, displays as The current date and time areAug 30, 2006 2:38:40
Directive Elements: Taglib Taglib directive - Defines a tag library and prefix for the custom tags used in the JSP page. Syntax <%@ taglib uri="URIToTagLibrary“ prefix="tagPrefix" %> Example: <%@ taglib uri=“http://java.sun.com/jstl/core“ prefix=“c" %> More on taglib directives later when we use Java Standard Tag Library (JSTL)
Directive Elements: summary • Three directives: page, include, taglib • Used to define general set-up information about the JSP page • By themselves, don’t “do” anything • At least one used in most JSP pages
JSP dynamic processing Done • Directive elements • Scripting elements • Action elements and JSTL • Java Beans
Scripting elements Developers in JSP can insert java code directly into a JSP pages using scripting elements The code is executed when the page is requested • Should be used with extreme care: • Too much code difficult to maintain • Difficult for HTML programmers • More suitable for simple applications with small development team Q: How do you specify that the language being used in page by scripting elements is java? A: Page directive language attribute
Scripting elements • Three types of scripting elements: • Expressions: The expression syntax <%= ... %> defines a scripting language expression .. “result” • Scriptlets: The scriptlet syntax <% ... %> can handle declarations, expressions, or any other type of code fragment valid in the page scripting language. • When you write a scriptlet, end the scriptlet with %> before you switch to HTML, text, or another JSP tag • 3. Declarations: The declaration syntax <%! ... %> declares variables or methods.
Scripting elements: Expressions • Expressions: • Contains any valid java expression in the JSP page • Used to output dynamic values directly onto web page (e.g. result of a calculation, dates) • Enclosed between <% and %> • output as a string • Syntax <%= expression %> e.g. <% = 1+1%>
Expressions - examples • E.gs: any valid java expression <%= Math.sqrt(2) %><%= items[i] %><%= a + b + c %><%= new java.util.Date() %>
Scripting elements: Expressions Example <html> <body> Current time is: <%= new java.util.Date() %> </body> </html>
Scripting elements: Expressions • Note: • Evaluated at run time. Result is added to the response body and output directly to web page • Can use an expression within a line of text, whether or not it is tagged with HTML • Must be a valid java expression • No “;” required at end of expression (unlike scriptlets)
Topic summary Page Include Taglib <%@ > • Directive elements Specify set-up info about the JSP page Expressions <% = %> Scriptlets <% code %> Declarations <%! %> • Scripting elements Enable java code to be embedded into the JSPpage