The JSP Directives Vijayan Sugumaran School of Business Administration Oakland University Parts of this lecture provided by Coreservlets.com.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Introducing JavaScript
Java Script Session1 INTRODUCTION.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
The Web Warrior Guide to Web Design Technologies
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Python and Web Programming
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.
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.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
DAT602 Database Application Development Lecture 14 HTML.
1 Chapter 1: Overview of Servlets and JavaSerevr Pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
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.
JSP Java Server Pages Softsmith Infotech.
HTML and Style. Session overview Leveling-off on the basic concepts of HTML and Styles Discuss Web authoring options.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
Chapter 8 Cookies And Security JavaScript, Third Edition.
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.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
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.
Storing and Retrieving Data
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 14 Using JavaBeans Components in JSP Documents.
JSP Directive and Objects. JSP Directives As discussed before There are 3 types of elements in JSP Directive Elements Scripting Elements Standard Action.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Net-centric Computing Servlets & JSP. Lecture Outline  Tracking Sessions  Cookies  Examples  JSP  Differences between JSP and Servlets  JSP Constructs.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
JSP Elements Chapter 2.
Developing JavaServer Pages
Java Server Pages.
Introduction to Scripting
Pre-assessment Questions
Invoking Java Code from JSP
Chapter 27 WWW and HTTP.
JSP Directives 1-Jan-19.
JSP implicit objects & directive elements
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Presentation transcript:

The JSP Directives Vijayan Sugumaran School of Business Administration Oakland University Parts of this lecture provided by Coreservlets.com

JSP Directives  Directives  Instructions to the JSP container that describes what code should be generated  May be used to specify the scripting language  Include the contents of another page  Indicate that a page uses custom tag library  Directives do not directly produce any output that is visible to the user  They generate side effects that change the way the JSP container processes the page

Directives Syntax  Directives usually placed at the top of the JSP page (except the include directive)  Syntax   Zero or more spaces, tabs, and new line characters can appear after the opening  One or more white space characters can appear after the directive name and between attribute/value pairs  The only restriction is that the opening tag.

Three types of Directives  Page  Controls the structure of the resulting servlet  Include  Lets you insert a file into the servlet class at the time the JSP file is translated into a servlet  Taglib  Used to define custom markup tags

The Include Directive  Merges the content of another file at translation time into the.jsp source input stream, much like the #include statement in C language  Syntax   Where “filename” is an absolute or relative pathname interpreted according to the current servlet context.  Example 

The taglib Directive  The taglib directive makes custom actions available in the current page through the use of a tag library  Syntax   Example   If FancyTableGenerator.tld defines a tag named “table”, then the page can contain tags of the following type:   ……. 

Page Directive  Page Directive – used to specify attributes for the jsp page as a whole   More than one page directive can be in a file and the attributes specified collectively apply to the whole file  However, no attribute can be specified more than once, with the exception of the “import” attribute

Purpose of the page Directive  Give high-level information about the servlet that will result from the JSP page  Can control  Which classes are imported  What class the servlet extends  What MIME type is generated  How multithreading is handled  If the servlet participates in sessions  The size and behavior of the output buffer  What page handles unexpected errors

Common Attributes of Page Directive  The import attribute  The contentType attribute  Generating plain text and Excel documents  The isThreadSafe attribute  The session attribute  The buffer attribute  The autoflush attribute  The extends attribute  The errorPage attribute  The isErrorPage attribute

Common Attributes

The import Attribute  Format   Purpose  Generate import statements at top of servlet definition  Notes  Although JSP pages can be almost anywhere on server, classes used by JSP pages must be in normal servlet dirs  For Tomcat, this is install_dir\webapps\ROOT\WEB-INF\classes or...\ROOT\WEB-INF\classes\directoryMatchingPackage

Example of import Attribute... The import Attribute <%! private String randomID() { int num = (int)(Math.random()* ); return("id" + num); } private final String NO_VALUE = " No Value "; %> (Continued on next slide)

Example of import Attribute (cont) <% Cookie[] cookies = request.getCookies(); String oldID = ServletUtilities.getCookieValue(cookies, "userID", NO_VALUE); String newID; if (oldID.equals(NO_VALUE)) { newID = randomID(); } else { newID = oldID; } LongLivedCookie cookie = new LongLivedCookie("userID", newID); response.addCookie(cookie); %> This page was accessed at with a userID cookie of.

Example of import Attribute: Result  First access  Subsequent accesses

The contentType Attribute  Format   Purpose  Specify the MIME type of the page generated by the servlet that results from the JSP page  Notes  Attribute value cannot be computed at request time  See section on response headers for table of the most common MIME types

Using contentType to Generate Plain Text Documents The contentType Attribute The contentType Attribute This should be rendered as plain text, not as HTML.

Plain Text Documents in Netscape (Correct)

Plain Text Documents in Internet Explorer (Incorrect)

Generating Excel Spreadsheets (Anticipated)

Generating Excel Spreadsheets Conditionally  Excel can interpret HTML tables  Change MIME type based on request parameters  You cannot use page directive  It does not use request-time values.  Solution  Use predefined request variable and call setContentType

Generating Excel Spreadsheets Conditionally Comparing Apples and Oranges <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> Comparing Apples and Oranges <% String format = request.getParameter("format"); if ((format != null) && (format.equals("excel"))) { response.setContentType("application/vnd.ms-excel"); } %> (Continued on next slide)

Generating Excel Spreadsheets Conditionally (Continued) Apples Oranges First Quarter Second Quarter Third Quarter Fourth Quarter

Apples and Oranges: Default Result

Apples and Oranges: Result with format=excel

The isThreadSafe Attribute  Format   Purpose  To tell the system when your code is not threadsafe, so that the system can prevent concurrent access  Notes  Default is true -- system assumes you have synchronized updates to fields and other shared data  Supplying a value of false can degrade performance  Systems are permitted to make multiple instances of the servlet class as long as each is called serially. Moral: static fields are not necessarily safe

Example of Non-Threadsafe Code (IDs Must Be Unique)  What's wrong with this code? <% String userID = "userID" + idNum; out.println("Your ID is " + userID + "."); idNum = idNum + 1; %>

Is isThreadSafe Needed Here?  No <% synchronized(this) { String userID = "userID" + idNum; out.println("Your ID is " + userID + "."); idNum = idNum + 1; } %>  Totally safe, better performance in high-traffic environments

The session Attribute  Format   Purpose  To designate that page not be part of a session  Notes  By default, it is part of a session  Saves memory on server if you have a high-traffic site  All related pages have to do this for it to be useful

The buffer Attribute  Format   Purpose  To give the size of the buffer used by the out variable  Notes  Buffering lets you set HTTP headers even after some page content has been generated (as long as buffer has not filled up or been explicitly flushed)  Servers are allowed to use a larger size than you ask for, but not a smaller size  Default is system-specific, but must be at least 8kb

The autoflush Attribute  Format   Purpose  To designate if buffer should be flushed when full (true) or if an exception should be raised (false)  Notes  A value of false is illegal when also using buffer="none"

The extends Attribute  Format   Purpose  To specify parent class of servlet that will result from JSP page  Notes  Use with extreme caution  Can prevent system from using high-performance custom superclasses

The errorPage Attribute  Format   Purpose  Specifies a JSP page that should process any exceptions thrown but not caught in the current page  Notes  The exception thrown will be automatically available to the designated error page by means of the "exception" variable

The isErrorPage Attribute  Format   Purpose  Indicates whether or not the current page can act as the error page for another JSP page  Notes  Use this for emergency backup only; explicitly handle as many exceptions as possible  Don't forget to always check query data for missing or malformed values  The web.xml file can designate general error pages rather than page-specific ones like this

Error Pages: Example (ComputeSpeed.jsp)... Computing Speed <%! // Note lack of try/catch for NumberFormatException private double toDouble(String value) { return(Double.valueOf(value).doubleValue()); } %>

Error Pages: Example (ComputeSpeed.jsp Cont.) <% double furlongs = toDouble(request.getParameter("furlongs")); double fortnights = toDouble(request.getParameter("fortnights")); double speed = furlongs/fortnights; %> Distance: furlongs. Time: fortnights. Speed: furlongs per fortnight....

Error Pages: Example (SpeedErrors.jsp)... Error Computing Speed ComputeSpeed.jsp reported the following error:. This problem occurred in the following place: <% exception.printStackTrace( new java.io.PrintWriter(out)); %>...

Error Pages: Example

XML Syntax for Directives  JSP allows alternative XML-compatible syntax for directives  Syntax   Example   Same as: 

Summary  The import attribute  Changes the packages imported by the servlet that results from the JSP page  The contentType attribute  Specifies MIME type of result  Cannot be used conditionally  Use instead  The isThreadSafe attribute  Turns off concurrent access  Consider explicit synchronization instead  The errorPage and isErrorPage attributes  Specifies "emergency" error handling pages