Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun...

22
Java Server Pages Java Server Pages Jeffrey Jongko Jeffrey Jongko

Transcript of Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun...

Java Server PagesJava Server Pages

Jeffrey JongkoJeffrey Jongko

IntroductionIntroduction

Java Server Pages (JSP) technology Java Server Pages (JSP) technology was created by Sun Microsystems and was created by Sun Microsystems and is built on top of Sun’s Java Servlet is built on top of Sun’s Java Servlet technologytechnology

Java Servlets was Sun’s first answer Java Servlets was Sun’s first answer for developing web-based applications for developing web-based applications use the Java programming language.use the Java programming language.

IntroductionIntroduction Java Servlet technology suffered similar Java Servlet technology suffered similar

maintenance problems that many maintenance problems that many traditional web-technologies like CGI had traditional web-technologies like CGI had (presentation and logic are combined)(presentation and logic are combined)

JSP is designed to facilitate development of JSP is designed to facilitate development of dynamic web sites by more easily dynamic web sites by more easily dissociating presentation from application dissociating presentation from application logiclogic– allowing page developers to develop page allowing page developers to develop page

presentation without interfering with application presentation without interfering with application logic developmentlogic development

JSP ArchitectureJSP Architecture

The purpose of JSP is to provide a The purpose of JSP is to provide a declarativedeclarative, , presentation-centric presentation-centric method method of developing servlets. of developing servlets.

JSP specification is defined as a standard JSP specification is defined as a standard extension on top the Servlet API. extension on top the Servlet API.

Consequently, it should not be too Consequently, it should not be too surprisingly that under the covers, surprisingly that under the covers, servlets and JSP pages have a lot in servlets and JSP pages have a lot in common. common.

JSP ArchitectureJSP Architecture

Typically, JSP pages exist as simple Typically, JSP pages exist as simple text files and are subject to a text files and are subject to a translation phase translation phase and a and a request request processing phaseprocessing phase. .

The translation phase is carried out The translation phase is carried out only once, unless the JSP page only once, unless the JSP page changes, in which case it is repeated. changes, in which case it is repeated. – The JSP page is transformed into a The JSP page is transformed into a

servlet which subsequently processes all servlet which subsequently processes all future requests to the pagefuture requests to the page

JSP ArchitectureJSP Architecture

Sample JSP fileSample JSP file

<html> <body> <center> <%

String hello = "Hello World"; %> <h1><%= hello %></h1> </center></body></html>

The following JSP file The following JSP file is saved to a file is saved to a file HelloWorld.jspHelloWorld.jsp

It looks like regular It looks like regular HTML will special HTML will special regions delimited regions delimited with special markers with special markers like “like “<%<%” and ” and ““%>%>” which ” which represent JSP represent JSP featuresfeatures

JSP Syntax core elementsJSP Syntax core elements

There are four basic There are four basic core elementscore elements in in JSPJSP– commentscomments– declarationsdeclarations– expressionsexpressions– scriptletsscriptlets

JSP CommentsJSP Comments There are 2 types of JSP commentsThere are 2 types of JSP comments

– HTML commentHTML comment– Hidden commentHidden comment

HTML Comment SyntaxHTML Comment Syntax<!-- comment [ <%= expression %>] --><!-- comment [ <%= expression %>] -->

Hidden Comment SyntaxHidden Comment Syntax<%-- comment --%><%-- comment --%>

A JSP HTML comment is a comment that is sent to A JSP HTML comment is a comment that is sent to the client (appears on the page data)the client (appears on the page data)

JSP expressions JSP expressions (seen later) can be included inside (seen later) can be included inside an HTML commentan HTML comment

Sample JSP CommentsSample JSP Comments

<html> <body> <%@ page import="java.util.*"

%> <%@ page import="java.text.*"

%> <!-- Loaded on <%=

DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREAN).format(new Date()) %> -->

<% String hello = "Hello World"; %> <h1><%= hello %></h1> <%-- This will not appear --%></body></body></html></html>

DATA RECEIVED BY CLIENT:DATA RECEIVED BY CLIENT:

<html> <body> <!-- Loaded on 2000-02-16

--> <h1>Hello World</h1> </body></html>

JSP DeclarationsJSP Declarations JSP Declarations are used to define JSP Declarations are used to define

variables and methods that are visible to variables and methods that are visible to the whole pagethe whole page– variables are translated into an variables are translated into an instanceinstance

variable in the compiled servletvariable in the compiled servlet

JSP Declaration SyntaxJSP Declaration Syntax<%! variable / method declaration %><%! variable / method declaration %>

Variables declared in this way are not Variables declared in this way are not thread-safe.thread-safe. The JSP page has to be The JSP page has to be declared as single-threaded if thread declared as single-threaded if thread safety is needed for these variables.safety is needed for these variables.

Sample JSP DeclarationSample JSP Declaration<%! int counter = 0;

boolean isSameString(String a,

String b) {

return a.equals(b); }

%> <html> <body> bjlee and hjk is <%=

(isSameString(“bjlee”,”hjk”))? ”” : ”not” %>

same string </body> </html>

OUTPUT ON CLIENT:OUTPUT ON CLIENT:

<html> <body> bjlee and hjk is not same string </body> </html>

JSP ExpressionsJSP Expressions

Scripting language expression that is Scripting language expression that is evaluated and converted into a evaluated and converted into a StringString for insertion into the output for insertion into the output pagepage

JSP Expression SyntaxJSP Expression Syntax

<%= variable / method call %><%= variable / method call %>

Sample JSP ExpressionsSample JSP Expressions

<html> <body> <%@ page import="java.util.*"

%> <%@ page import="java.text.*"

%> <!-- Loaded on <%=

DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREAN).format(new Date()) %> -->

<% String hello = "Hello World"; %> <h1><%= hello %></h1> <%-- This will not appear --%></body></body></html></html>

DATA RECEIVED BY CLIENT:DATA RECEIVED BY CLIENT:

<html> <body> <!-- Loaded on 2000-02-16

--> <h1>Hello World</h1> </body></html>

JSP ScriptletsJSP Scriptlets

Scripting language code fragment Scripting language code fragment that is run within the that is run within the service()service() method method of the compiled servletof the compiled servlet– variables declared within a scriptlet are variables declared within a scriptlet are

locallocal unlike those declared by unlike those declared by JSP JSP DeclarationsDeclarations

JSP Scriptlet SyntaxJSP Scriptlet Syntax

<% scripting language code %><% scripting language code %>

Sample JSP ScriptletSample JSP Scriptlet<%@ page import=“java.util.*”%>

<html><body bgcolor=“white”> <%

String name = “Byung Joon Lee”; StringTokenizer st=

new StringTokenizer(name, “ “); while ( st.hasMoreTokens() ) {

%>

<%= st.nextToken() %> <BR>

<% }

%> </body></html>

OUTPUT ON CLIENT:OUTPUT ON CLIENT:

<html><bodybgcolor=“white”>

Byung<BR>Joon<BR>Lee<BR></body></html>

<%@page %> Directive<%@page %> Directive

<%@page %> directive defines attributes <%@page %> directive defines attributes that apply to a whole JSP pagethat apply to a whole JSP page

Example of some attributes:[ language=“java”] [ extends=“package.class”] [ import=“{package.class | package.*}, ...”] [ session=“true|false”] [ isThreadSafe=“true|false”] [ info=“text”] [ errorPage=“relativeURL”] [ isErrorPage=“true|false”]

Implicit ObjectsImplicit Objects Data is passed to JSP pages from the Data is passed to JSP pages from the

outside via HTTP POST or GEToutside via HTTP POST or GET

This data is accessed via an This data is accessed via an implicit objectimplicit object– the name of this object is called the name of this object is called request request of of

type type javax.servlet.ServletRequest– implicit objects are accessed via the scriptlets

Other implicit objects exist such asOther implicit objects exist such as– responseresponse of type of type javax.servlet.ServletResponse– pageContext– session– application

Other JSP featuresOther JSP features

Ability to access Ability to access JavaBeanJavaBean components using components using JSP tagsJSP tags, e.g., e.g.– <jsp:useBean><jsp:useBean>– <jsp:setProperty><jsp:setProperty>– <jsp:getProperty><jsp:getProperty>

This allows access to JavaBean objects This allows access to JavaBean objects without the use of scriptlets/Java codewithout the use of scriptlets/Java code

Other JSP featuresOther JSP features Ability to extend the usable JSP tags using a Ability to extend the usable JSP tags using a

custom tag librarycustom tag library– this is used to reduce the number of scriptlets on this is used to reduce the number of scriptlets on

the page by encapsulating their logic behind the page by encapsulating their logic behind tagstags..

Both these features reduce the need for Both these features reduce the need for people with actual Java language experience people with actual Java language experience which is needed for coding scriptletswhich is needed for coding scriptlets– allows for the development of presentation (JSP allows for the development of presentation (JSP

page) and the actual business logic (JavaBeans) to page) and the actual business logic (JavaBeans) to developed separatelydeveloped separately

SitesSites

Some sites that use Java Server Some sites that use Java Server PagesPages– http://www.sun.comhttp://www.sun.com– http://www.friendster.comhttp://www.friendster.com

ReferencesReferences

http://java.sun.com/products/jsp/http://java.sun.com/products/jsp/ http://www.swpark.pe.kr/lecture/jsp.phttp://www.swpark.pe.kr/lecture/jsp.p

dfdf http://developer.java.sun.com/develohttp://developer.java.sun.com/develo

per/onlineTraining/JSPIntro/per/onlineTraining/JSPIntro/ http://http://archive.coreservlets.comarchive.coreservlets.com