J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
Chapter 1: Computer Systems
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
The JSP Directives Vijayan Sugumaran School of Business Administration Oakland University Parts of this lecture provided by Coreservlets.com.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
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.
Accelerating Server Side Programming with Java Server Pages (JSP) Atif Aftab Ahmed Jilani.
1 Chapter 1: Overview of Servlets and JavaSerevr Pages.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Chapter 5 HTTP Request Headers. Content 1.Request headers 2.Reading Request Headers 3.Making a Table of All Request Headers 4.Sending Compressed Web Pages.
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.
The Java Programming Language
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
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.
JSP Directive and Objects. JSP Directives As discussed before There are 3 types of elements in JSP Directive Elements Scripting Elements Standard Action.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
JSP Thanks to Course-Materials/csajsp2.html.
JSP Elements Chapter 2.
Developing JavaServer Pages
Java Server Pages.
Knowledge Byte In this section, you will learn about:
Invoking Java Code from JSP
JSP Directives 1-Jan-19.
JSP implicit objects & directive elements
Presentation transcript:

J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting institutions –Slides not permitted for use in commercial training courses except when taught by coreservlets.com (see Slides can be modified however instructor desires Instructor can give PDF or hardcopy to students, but should protect the PowerPoint files –This slide is suppressed in Slide Show mode

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 2 Controlling the Structure of Generated Servlets: The JSP page Directive

J2EE training: 3 Agenda Understanding the purpose of the page directive Designating which classes are imported Specifying the MIME type of the page Generating Excel spreadsheets Participating in sessions Setting the size and behavior of the output buffer Designating pages to handle JSP errors Controlling threading behavior

J2EE training: 4 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

J2EE training: 5 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 –E.g.: …/WEB-INF/classes or …/WEB-INF/classes/directoryMatchingPackage Always use packages for utilities that will be used by JSP!

J2EE training: 6 The Importance of Using Packages What package will the system think that SomeHelperClass and SomeUtilityClass are in?... public class SomeClass { public String someMethod(...) { SomeHelperClass test = new SomeHelperClass(...); String someString = SomeUtilityClass.someStaticMethod(...);... }

J2EE training: 7 The Importance of Using Packages (Continued) What package will the system think that SomeHelperClass and SomeUtilityClass are in?... <% SomeHelperClass test = new SomeHelperClass(...); String someString = SomeUtilityClass.someStaticMethod(...); %>

J2EE training: 8 The import Attribute: Example (Code) … The import Attribute <%! private String randomID() { int num = (int)(Math.random()* ); return("id" + num); } private final String NO_VALUE = " No Value "; %> <% String oldID = CookieUtilities.getCookieValue(request, "userID", NO_VALUE); if (oldID.equals(NO_VALUE)) { String newID = randomID(); Cookie cookie = new LongLivedCookie("userID", newID); response.addCookie(cookie); } %> This page was accessed on with a userID cookie of.

J2EE training: 9 The import Attribute: Example (Results)

J2EE training: 10 The contentType and pageEncoding Attributes 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

J2EE training: 11 Generating Excel Spreadsheets First Last Address Marty Hall Larry Brown Steve Balmer Scott McNealy

J2EE training: 12 Conditionally Generating Excel Spreadsheets You cannot use the contentType attribute for this task, since you cannot make contentType be conditional. –The following always results in the Excel MIME type Solution: use a regular JSP scriptlet with response.setContentType

J2EE training: 13 Conditionally Generating Excel Spreadsheets (Code) … Comparing Apples and Oranges <% String format = request.getParameter("format"); if ((format != null) && (format.equals("excel"))) { response.setContentType("application/vnd.ms-excel"); } %> Apples Oranges First Quarter Second Quarter Third Quarter Fourth Quarter

J2EE training: 14 Conditionally Generating Excel Spreadsheets (Results)

J2EE training: 15 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

J2EE training: 16 The isELIgnored Attribute Format – Purpose –To control whether the JSP 2.0 Expression Language (EL) is ignored (true) or evaluated normally (false). Notes –If your web.xml specifies servlets 2.3 (corresponding to JSP 1.2) or earlier, the default is true But it is still legal to change the default—you are permitted to use this attribute in a JSP-2.0-compliant server regardless of the web.xml version. –If your web.xml specifies servlets 2.4 (corresponding to JSP 2.0) or earlier, the default is false

J2EE training: 17 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

J2EE training: 18 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 web.xml file lets you specify application-wide error pages that apply whenever certain exceptions or certain HTTP status codes result. The errorPage attribute is for page-specific error pages

J2EE training: 19 The isErrorPage Attribute Format – Purpose –Indicates whether or not the current page can act as the error page for another JSP page Notes –A new predefined variable called exception is created and accessible from error pages –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

J2EE training: 20 Error Pages: Example … Computing Speed <%! private double toDouble(String value) { return(Double.parseDouble(value)); } %> <% double furlongs = toDouble(request.getParameter("furlongs")); double fortnights = toDouble(request.getParameter("fortnights")); double speed = furlongs/fortnights; %> Distance: furlongs. Time: fortnights. Speed: furlongs per fortnight.

J2EE training: 21 Error Pages: Example (Continued) … Error Computing Speed ComputeSpeed.jsp reported the following error:. This problem occurred in the following place:

J2EE training: 22 Error Pages: Results

J2EE training: 23 The isThreadSafe Attribute Format – Purpose –To tell the system when your code is not threadsafe, so that the system can prevent concurrent access Normally tells the servlet to implement SingleThreadModel Notes –Avoid this like the plague Causes degraded performance in some situations Causes incorrect results in others

J2EE training: 24 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; %>

J2EE training: 25 Is isThreadSafe Needed Here? No! It is not needed. Synchronize normally: <% synchronized(this) { String userID = "userID" + idNum; out.println("Your ID is " + userID + "."); idNum = idNum + 1; } %> Better performance in high-traffic environments isThreadSafe="false" will totally fail if server uses pool-of-instances approach

J2EE training: 26 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 –Typical purpose is to let you extend classes that come from the server vendor (e.g., to support personalization features), not to extend your own classes.

J2EE training: 27 Summary Used frequently –import Changes the packages imported by the servlet that results from the JSP page –Always use packages for utility classes! –contentType Specifies MIME type of result Cannot be used conditionally (use for that) Used moderately –isELIgnored –session –buffer –errorPage/isErrorpage Avoid like the plague –isThreadSafe Always use explicit synchronization instead

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 28 Questions?