DT211/3 Internet Application Development

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
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.
 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.
DT228/3 Web Development Introduction to Java Server Pages (JSP)
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
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.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
Java web development Servlet & Java server pages.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
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.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
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)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
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.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
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.
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
3/6/00jsp00 1 Java Server Pages Nancy McCracken Northeast Parallel Architectures Center at Syracuse University.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
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.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
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.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
DT228/3 Web Development JSP: Actions elements and JSTL.
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.
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.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
JSP java server pages.
JSP Based on
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Scripted Page Web App Development (Java Server Pages)
MSIS 655 Advanced Business Applications Programming
Presentation transcript:

DT211/3 Internet Application Development Introduction to Java Server Pages (JSP)

Introduction First – need to know the various “java” related terms: J2EE, J2SE, JDK, JRE, JSP,JSTL, Java Servlets,Tomcat, Apache etc…..

Introduction – J2EE Java Server Pages Servlets Java beans Java Sun Microsystems supply the Java 2 Enterprise Edition (J2EE) platform, enabling developers to develop java based enterprise applications J2EE is a standard set of technologies and APIs (note: J2SE is the standard edition of the java platform but is not geared at large enterprise environments with distributed systems) J2EE includes the following components: Java Server Pages Servlets Java beans Java Messaging JNDI JDBC

Introduction – J2EE Since J2EE is a specification, vendors create products that support the J2EE specifcation e.g. Apache, IBM WebSphere, BEA Weblogic. From a web perspective, the J2EE applications that are particularly relevant are: Java Server Pages Java servlets Java Beans Can be used on its own or with beans/servlets to create a web application Can be used on its own or with JSP/beans to create a web application More complex web applications may use all 3

Introduction – JDK The (JDK) Java Development Kit is the collective name for the various development components supplied in the J2EE (or J2SE). The Java Runtime Environment (JRE) is consists of the components required to run a java application

Introduction to JSP Java Server Pages – A technology for developing web pages that include dynamic content Created by SUN microsystems ‘Equivalent’ of Microsoft’s Active Server Pages technology Pages have a file extension of .JSP JSPs enable web developers to enhance HTML code by adding special JSP elements that are processed by the server Will use JSP v2.0

Advantages of JSP JSP pages are pre-compiled  Fast Part of the Sun’s J2EE - Can be used with other types of java technologies, such as servlets - flexible JSP is a specification  multiple vendors support it  commercially stable/not ‘fly by night’ technology Easy to develop: HTML can be easily maintained with JSP. Relatively minimal programming skills required. JSP page code is not visible on client – only generated HTML is visible

Running JSP pages To develop and run JSP pages, need: Not automatically included with all web servers To develop and run JSP pages, need: - Java Developer Kit (JDK which is part of J2EE) or higher AND a Web server that contains a JSP Container JSP Containers are not automatically included with all Web servers Examples of web servers that contain a JSP container are Apache Tomcat and Blazix.

JSP Containers JSP Container The JSP Container intercepts all client request for JSP pages First time a JSP page is requested, the Container converts the page into a java program called a Servlet and compiles -- Translation phase For all follow-on request, the Container processes each request by invoking the appropriate servlet and then generating the response - Request processing phase Q: What happens if the JSP page is changed?

How a JSP page is processed by server First time through, JSP is translated to a servlet After, container goes directly to the servlet in the request processing phase If JSP page is changed, servlet is re-compiled

JSP and Apache Apache project have a sub project called Jakarta (see http://jakarta.apache.org/index2.html) Jakarta project produces Tomcat web server (nicknamed Catalina) Tomcat webserver incorporates JSP container (nicknamed Jasper)

Software Versions JSP technology developing rapidly  New version contain major new capabilities  Always note the JSP container version you are working with, and check functionality supported (on apache website) This course using Apache Tomcat Version 5 JSP 2 JSLT 1.1 Servlet 2.4

To run a JSP Using apache Tomcat… Create your web application directory Create the subset WEB-INF directory (won’t run without this) Put JSP page into web application directory Call from the browser http://localhost:8080/webapp/somename.jsp

To run a JSP In the background – Tomcat will retrieve the JSP page from the web server If it’s the first time JSP page has been called/run or if page has changed, Tomcat will compile the JSP page (into a servlet) - .java and .class placed in /work directory. - subsequent calls to page will be faster as no compile needed JSP page will be presented back to the user

Simplest JSP.. Helloworld Prints out message to user when JSP is loaded.. Tomcat demo..

Another Simple JSP example <html> <%@ page import="java.util.Date" %> <head> <title>JSP DATE EXAMPLE</title> </head> <body bgcolor=#ffffff> <h1>JSP DATE</h1> <h2> The current date is <%= new Date() %>. </h2> </body> </html> Prints out the current date

Readability of JSP pages Always always always ensure that code is: INDENTED COMMENTED CONTAINS AN ID BLOCK Indented - to reflect level of the code <html> <head>ajsdlkfjads etc 2) Well commented. Comments in JSP appear as <%-- calculate the sum of x and z here --%> Comments in HTML appear as <!--- this is a comment --> HTML comments will be visible by view source in browser, JSP comments won’t.

Readability of JSP pages 3) Titled with an ID block: At the top of each JSP page, should have an ID block explaining who write the script, date, script name, description and any revisions. Can use either JSP or HTML comments (depending on whether users should be able to see the ID block) <%-- ********************************************* *** Script name: addition.jsp *** *** Author: Keith Morneau *** *** Date: July 7th 2006 *** *** Desciption: whatever it does.. *** *** Revisions: *** *** August 8th 2006: added subroutine *** ************************************************ --%> etc

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

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: Include Include directive - Includes a static file in a JSP file at translation 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 are Aug 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 Directive elements Done 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 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)

Scripting elements Scriptlets Three types of scripting elements: Expressions Scriptlets Declarations

Scripting elements: Scriptlets Scriplets are java code fragments with a JSP page Enables more complex functionality than expressions Java code is placed between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.) Can have any number of valid java code statements Scriptlet contains Java code that is executed every time the JSP is invoked Syntax: <% code %>

Scripting elements: Scriptlets Example – jsp page that outputs numbers 1 to 10 onto a web page html> <head> <title>Count to 10 in JSP scriptlet</title> </head> <body> <% for(int i=1;i<=10;i++) {%> <%=i%><br/> } %> </body> </html> Scriptlets Expression used to output to page. Can’t put HTML Tags into the scriptlet. Can only contain valid java code

Scripting elements: Scriptlets Example – jsp page that outputs numbers 1 to 10 onto a web page – Output in browser will be:.. Count to 10 in JSP 1 2 3 4 5 6 7 8 9 10

Scriplets: mixing scriplets with HTML and other tags When you mingle scripting elements with HTML and JSP tags, you must always end a scripting element before you start using tags and then reopen the scripting element afterwards, like this: <% } else { %> <!-- closing the scriptlet before the tags start --> ... tags follow ... <% } %> <!-- reopening the scriptlet to close the language block --> At first, this may look a bit strange, but it ensures that the scripting elements are transformed correctly when the JSP source file is compiled

Scripting elements: Scriptlets Example 2 –jsp page that outputs “Hello! The time is now Wed Sep 03 13:17:58 BST 2006 Your machine's address is 127.0.0.1” onto a web page <HTML> <BODY> <% java.util.Date date = new java.util.Date(); %> Hello! The time is now out.println( date ); out.println( "<BR>Your machine's address is " ); out.println( request.getRemoteHost()); </BODY> </HTML> Note: could have put this line outside the scriptlet as HTML

Scripting elements: Scriptlets Note: Any text, HTML tags, or JSP elements you write must be outside the scriptlet Readability: Mixture of Tags and Java code can be difficult to read – especially for HTML developers

Scripting elements: Scriptlets <% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>lousy</B> day! <% } %> In background, JSP container compiles the JSP page into Java code.. converting the HTML snippets into java code to out.print statements ---

Scripting elements: Scriptlets JSP code on previous page will be converted in background to the following java code.. if (Math.random() < 0.5) { out.println("Have a <B>nice</B> day!"); } else out.println("Have a <B>lousy</B> day!");

Scripting elements: Scriptlets Example 3 – fragment of JSP page that generates a table in HTML containing numbers 1 to n <TABLE BORDER=2> <% for ( int i = 0; i < n; i++ ) { %> <TR> <TD>Number</TD> <TD><%= i+1 %></TD> </TR> } </TABLE> HTML within “for” loop is output n times Note: would need to supply int variable n before it will work…

Scripting elements Declarations Three types of scripting elements: Expressions Scriptlets Declarations

Scripting elements: Declarations Declaration: Declares a variable or method that can be used throughout the JSP page Examples <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> Syntax <%! declarations; %> Note the “ ;”

Scripting elements: Declarations Declarations don’t generate any output onto the page by themselves - usually used with expressions and/or scriptlets Examples <%! private int accessCount; %> Accesses to page since server reboot: <%= ++accessCount %> Prints the number of times the current page has been requested since the server was booted

Scripting elements: Declarations Example – Declares method getDate() <%@ page import="java.util.*" %> <HTML> <BODY> <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now <%= getDate() %> </BODY> </HTML> Method getDate() returns a Date object

Declaration Declarations (between <%! and %> tags) contain one or more variable or method declarations that end or are separated by semicolons: <%! int i = 0; %> <%! int a, b; double c; %> <%! Circle a = new Circle(2.0); %> You must declare a variable or method in a JSP page before you use it in the page. The scope of a declaration is usually a JSP file, but if the JSP file includes other files with the include directive, the scope expands to cover the included files as well.

Declarations Warnings! Declaring variables in a JSP page using declarations can cause synchronisation problems.. Compiled servlets see these as variables which may be shared across all users using the JSP page. On previous date example, Date stays the same when the page is reloaded.. Because only a single instance of the page (and there of the variable theDate is available). Better to use local variables within declared methods or scriptlets