1 / 25

DT228/3 Web Development

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.

ponce
Télécharger la présentation

DT228/3 Web Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DT228/3 Web Development JSP: Directives and Scripting elements

  2. 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

  3. 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.

  4. 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

  5. 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>

  6. 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

  7. 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” …. %>

  8. JSP Pages: Directive Elements • There are Three directives available to use: • <%@ page ….. > • <%@ include …… %> • <%@ taglib ….. %>

  9. 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

  10. 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" %>

  11. 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%>

  12. 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

  13. 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

  14. 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>

  15. 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

  16. 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)

  17. 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

  18. JSP dynamic processing Done • Directive elements • Scripting elements • Action elements and JSTL • Java Beans

  19. 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

  20. 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.

  21. 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%>

  22. Expressions - examples • E.gs: any valid java expression <%= Math.sqrt(2) %><%= items[i] %><%= a + b + c %><%= new java.util.Date() %>

  23. Scripting elements: Expressions Example <html> <body> Current time is: <%= new java.util.Date() %> </body> </html>

  24. 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)

  25. 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

More Related