Www.torontocollege.com JavaServer Pages Fundamentals.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
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.
 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.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
04/18/20011 JavaServer Page Presented by: Hongmei Yu 04/18/2001.
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.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
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.
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.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
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.
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.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Chapter 6 Server-side Programming: Java Servlets
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 (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
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.
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
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.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
© 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.
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.
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.
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.
Chapter 14 Using JavaBeans Components in JSP Documents.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
Developing JavaServer Pages
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Invoking Java Code from JSP
JSP Directives 1-Jan-19.
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.
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

JavaServer Pages Fundamentals

Concepts After completing this module you will understand the: Advantages of JSP technology JSP architecture Life cycle of a JSP page JSP syntax and semantics Role of JavaBean TM components within JSP pages

Objectives By the end of this module you will be able to: Manage session-related information from JSP Communicate between JSP pages Process forms with JSP

Prerequisites A general familiarity with object-oriented programming concepts and the Java programming language. A general familiarity with HTML tag

Introduction JSP pages typically comprise of: Static HTML/XML components. Special JSP tags Optionally, snippets of code written in the Java programming language called "scriptlets."

JSP Advantages Write Once Run Anywhere: Dynamic content can be served in a variety of formats: Recommended Web access layer for n-tier architecture Completely leverages the Servlet API: Separation of static from dynamic content

Comparing JSP with ASP JavaServer Pages Active Server Pages Web Server Most popular web servers Microsoft IIS Support Platform Platform independent Windows Support Component Relies on reusable, Model cross-platform COM components,Javabean,EJB VBScript and JScript Scripting Java or JavaScript Security Java security model. NT security architecture. Database JDBC ODBC Access Customizable JSP is extensible Cannot use custom tag Tags with custom tag libraries. Libraries. is not extensible.

JSPs and Servlets JSP is nothing but a high-level abstraction of servlets. You can do almost anything that can be done with servlets using JSP--but more easily!

Exercise 1 Installing and Configuring Tomcat Download Tomcat in Apache Site Unzip and Install it Create your own project publish directory under webapps. Here we call project directory, and create subdirectory under Project directory. See the following figure

Web-inf/Classes/ Class file Application name/ For example: / … JSPs file and other data Meta-info/ Tag library

Configure the Tomcat In the server.xml file, add the following lines <Context path="/project" docBase="webapps/project" crossContext="true" debug="0" reloadable="true" trusted="false" >

Set up environment Add the following lines In your autoexec.bat or System Properties/Environment Variables window. set JAVA_HOME=c:\jdk1.3 set ANT_HOME=c:\program files\apache group\jakarta- tomcat set Tomcat_Home=c:\program files\apache group\jakarta- tomcat set CLASSPATH=c:\jdk1.3\lib;c:\jdk1.3\lib\tools.jar;c:\ program files\apache group\jakarta-tomcat\lib\servlet.jar

Start Tomcat & try example Tomcat/bin>Tomcat run or startup Copy ’s jsp files and data files into folder and ’s class files into web-info folder. Launch a web browser and type:

General Architecture User enters value into form and clicks submit button Response displayed in browser window Interprets JSP and uses data from form to generate Response Request Object Response Object

General Architecture(more detail) Web Application Server Client Web Server Plain Documents Servlet Engine Compiled Servlet JSP Engine JSP Documents

The Basics of JSP Everything in a JSP page can be broken into 2 categories  Elements that can be processed on the server  Template data

Element basics Directives Declarations Scriptlets Expressions Standard actions

Directives The page directive The include directive The taglib directive

The page Directive Attribute language extends import session …….

The page Directive Attribute Description Default language Defines the scripting language to be used. For future use if JSP engine “Java” supports multiple language extends The superclass that the generated class(into which this JSP page is compiled) must extend. This attribute should be used with extreme caution because the engines usually Attribute omitted provide specialized superclass by default with a lot of functionality that the generated classes extend

The page Directive Attribute Description Default Import import package and classes Attribute omitted by default Session Specifies if the page participates in an HTTP “true” session Buffer Specifies the buffering model for output stream The default is buffered with to the client. If the value an implementation buffer is none, then no buffering size of not less that 8kb occurs and al output is written directly through to the ServletResponse by a PrintWriter.

The page Directive Attribute Description Default autoFlush If “ture”, the output buffer to the is flushed automatically when it is full. If “false”, a runtime exception is raised to indicate buffer overflow. The default is “ture” isThreadSafe Defines the level of thread safety implemented in the page. If “false” then th e JSP processes quenes up client requests sent to the page for processing. It processes them one at a time, in the order they were received. This is the same as implementing the javax.servlet.SingleThreadModel interface in a servlet. Default is “true”

The page Directive Attribute Description Default Info Define an informative stirng that can subsequently be obtained from the page’s implementation of Servlet.getServletInfo() method. It is omitted by default IsErrorPage Indicates if the current JSP page is intended to be the the URL target of another JSP pages’s errorPage. If “true”, then the implicit variable exception is available, and refers to the instance of the java.lang.Throwable thrown at runtime by the JSP causing the error. The default is “false”

The page Directive Attribute Description Default errorPage Defines a URL to another JSP that is invoked if an unchecked runtime exception is thrown. The page implementation catches the instance of the Throwable object and passes it to the error page processing. See the isErrorPage attribute above contentType Defines the character encoding for the JSP and the MIME type for response of the JSP page.The default is ”text/html”

Example of The Page Directive File name: pagedirective.jsp page language="java" import="java.rmi.*,java.util.*" session="true" buffer="12kb" autoFlush="true" info="my page directive jsp" errorPage="Error.jsp" isErrorPage="false" isThreadSafe="false"%>

This is a JSP to test the page directive tag

Include Directive The include directive lets you separate you content into more manageable elements, such as those for including a common page header or footer. The page included can be a static HTML page or more JSP content. For example, the directive: can be used to include the contents of the indicated file at any location within the JSP page.

The taglib Directive This directive allows the page to use custom user defined tags taglib uri=“ prefix=“pooh” …………..

Scripting Elements Declaration Scriptlets Expressions

Declarations Example Hello World <%! private int i=4; // my counter public void myMethod(){ // do some work here } %>

Scriptlets Example This is a scriptlet example <% for(int i = 0; i < 10; i++){ out.println(" Hello World This is a loop test "+i +" "); System.out.println("This goes to the system.out stream "+i); } %>

Expression Example <% i++; %> Hello world !

Standard Action

Using JavaBean Components The component model for JSP technology is based on JavaBeans component architecture. JavaBeans components are nothing but Java objects which follow a well-defined design/naming pattern: the bean encapsulates its properties by declaring them private and provides public accessor (getter/setter) methods for reading and modifying their values.

jsp:useBean For example: Important: What is scope? We will cover it shortly.

Beandetails: class=“className” class=“className” type=“typename” beanName=“beanName” type=“typeName”

Scope in tag Scope in tag Description page The object exists for every client request to the resource Request The object reference is available as long as the HttpRequest object is not discarded,even if the request is passes/chained to different pages. The object is distinct for every client request Session The object is distinct for every client, and is available as long as the client’s session is valid Application This is not unique for clients, and consequently all clients access the same object.

Scope in tag

What does the container do under the semantics? The container tries to locate an object that has this id, in the specified scope If the object is found, and a type has been specified in the tag, the container tries to cast the found object to the specified type. A java.lang.ClassCastException is thrown if the cast fails. If the object is not found in the specified scope, and no class or beanName is specified in the tag, a java.lang.InstantiationException is throw

If the object is not found in the specified scope. The class specified is instantiated. A new object reference is associated with the variable If the object is not found in the specified scope, and a beanName is specified, then the instantiate() method of the java.bean.Beans is invoked. If the method succeeds, the new object reference is associated with the variable, in the specified scope. What does the container do under the semantics?

Jsp:setProperty <jsp:setProperty name=“beanName” propertydetails Propertydetails property = “*” property=“propertyName” property=“propertyName” param=“parametName” property=“propertyName” value=“propertyValue”

Jsp:getProperty <jsp:getProperty name=“name” property=“propertyName” Name: The name of the bean instance from which the property is obtained. The bean must already have been found or created using Property: The name of the property to retrieve The jsp:getProperty action is complementary to the jsp:setProperty action and is used to access the properties of a bean. It accesses the value of a property, converts is to a String, and prints it to the output stream.

Example of Using JavaBean Components Example : SpellCheck SpellCheck.htm l Wordpro.jsp SpellCheck.class

SpellCheck.java /** * This bean encapsulates the functionality to spell check a String */ public class SpellCheck { private String word; public SpellCheck() {} /** Method to reverse the string the reversed String */

public String reverse() { return (new StringBuffer(word).reverse()).toString(); } /** Checks the spelling of the word.This method has no body, and just returns true for the boolean, true if the spelling is right */ public boolean check() { return true; } /** * Access method for the word property.

the current value of the word property */ public String getWord() { return word; } /** * Sets the value of the word property. * aWord the new value of the word property */ public void setWord(String aWord) { word = aWord; }

Wordpoint.jsp <% System.out.println("Explicitly doing some work on the bean..."); help.setWord(request.getParameter("word")); %>

You entered the input(getParameter), You entered the input(jsp:getProperty), The processed output is:

spellCheck.html Untitled Document

Enter word: Reverse Spellcheck

Exercise of Using JavaBean Components In this exercise, you develop a simple JSP page ( form.jsp ), which can process an HTML form containing typical input elements like textboxes, radio buttons, and checkboxes. You also develop a bean ( FormBean.java ), whose property names mirror the input elements of the form. You will then examine the automatic instantiation of the bean on a form POST operation, using the introspective features provided by the JSP engine.

Exercise of Using JavaBean Components TASKS You are given the JSP page containing the form. Observe that the form posts to itself recursively. Instantiate the bean FormBean when you recognize that a POST operation has taken place. Allow the setter methods to be called on the bean using introspection. Deploy the JSP page within Tomcat. Develop the bean, FormBean.java, with properties matching the form elements. Compile the bean source FormBean.java. Deploy the bean within Tomcat. Run the example.

Jsp:include Example This example shows how the includes work: Including a jsp and html with the include directive...

Including a jsp and html with the include action..

What is the difference between directive include and jsp:incude The jsp:include is being computed at request time The directive include parses the content of the included files into the current JSP while compiling it. The jsp:include can handle the static and dynamic content. The directive include handles the static content

Pass value to another JSP file This example shows how the include work: <jsp:include page=“two.jsp” flush=“true” <jsp:param name=“attribute1” value=“value1” <jsp:param name=“attribute2” value=“value2” In this code, two.jsp can access the values of attribute using request.getParameter(“attribute1”)

JSP:include

Include Example here is a JSP page that inserts four different snippets into a "What's New?" Web page. Each time the headlines change, authors only need to update the four files, but can leave the main JSP page unchanged.

This action lets you forward the request to another page. It has a single attribute, page, which should consist of a relative URL. This could be a static value, or could be computed at request time, as in the two examples below. " /> Jsp:forward

The invoking page can also pass the target resource bean parameters by placing them into the request, as shown in the diagram:

A tag may also have jsp:param subelements that can provide values for some elements in the request used in the forwarding: " >