Java Server Pages In this lesson you will

  • Slides: 36
Download presentation
Java. Server Pages In this lesson you will be learning about: What JSP Technology

Java. Server Pages In this lesson you will be learning about: What JSP Technology is and how you can use it. How to define and write JSP Page. Syntax of JSP Page. How do JSP pages work. How is a JSP page invoked and compiled. Use of Beans in a JSP Page. How one could create XML pages using JSP technology. Java. Server Pages v 1. 2 Copyright @ 2000 Jordan Anastasiade. All rights reserved. 1

Java. Server Pages Technology Java. Server Pages (JSP) technology provides a simplified, fast way

Java. Server Pages Technology Java. Server Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamicallygenerated content. The JSP 1. 2 specification is an important part of the Java 2 Platform, Enterprise Edition. Using JSP and Enterprise Java. Beans technologies together is a great way to implement distributed enterprise applications with webbased front ends. The first place to check for information on JSP technology is http: //java. sun. com/products/jsp/ Copyright @ 2000 Jordan Anastasiade. All rights reserved. 2

JSP Page A JSP page is a page created by the web developer that

JSP Page A JSP page is a page created by the web developer that includes JSP technology-specific tags, declarations, and possibly scriptlets, in combination with other static HTML or XML tags. A JSP page has the extension. jsp; this signals to the web server that the JSP engine will process elements on this page. Pages built using JSP technology are typically implemented using a translation phase that is performed once, the first time the page is called. The page is compiled into a Java Servlet class and remains in server memory, so subsequent calls to the page have very fast response times. Put. jsp pages into a WAR file (see Web ARchive) Deploy (upload) the WAR file as a Web Application to your Sun Java System Application Server Platform. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 3

Overview Java. Server Pages (JSP) lets you separate the dynamic part of your pages

Overview Java. Server Pages (JSP) lets you separate the dynamic part of your pages from the static HTML tags and text <% some JSP code here %> HTML tags and text <I> <%= request. get. Parameter("title") %> </I> You normally give your file a. jsp extension, and typically install it in any place you could place a normal Web page Copyright @ 2000 Jordan Anastasiade. All rights reserved. 4

Client and Server with JSP Copyright @ 2000 Jordan Anastasiade. All rights reserved. 5

Client and Server with JSP Copyright @ 2000 Jordan Anastasiade. All rights reserved. 5

Translation Time A JSP application is usually a collection of JSP files, HTML files,

Translation Time A JSP application is usually a collection of JSP files, HTML files, graphics and other resources. A JSP page is compiled when your user loads it into a Web browser 1. When the user loads the page for the first time, the files that make up the application are all translated together, without any dynamic data, into one Java source file (a. java file) 2. The. java file is compiled to a. class file. In most implementations, the. java file is a Java servlet that complies with the Java Servlet API. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 6

Simple JSP Page <%@ page info=“A Simple JSP Sample” %> <HTML> <H 1> First

Simple JSP Page <%@ page info=“A Simple JSP Sample” %> <HTML> <H 1> First JSP Page </H 1> <BODY> <% out. println(“Welcome to JSP world”); %> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 7

How JSP Works? User Request – JSP File Requested Server Create Source from JSP

How JSP Works? User Request – JSP File Requested Server Create Source from JSP Compile File Changed Execute Servlet Copyright @ 2000 Jordan Anastasiade. All rights reserved. 8

JSP Elements Declarations <%! code %> <jsp: declaration> </jsp: declaration > Expressions <%= expression

JSP Elements Declarations <%! code %> <jsp: declaration> </jsp: declaration > Expressions <%= expression %> <jsp: expression> </jsp: expression> Scriplets <% code %> <jsp: scriplet> </jsp: scriplet > Copyright @ 2000 Jordan Anastasiade. All rights reserved. 9

HTML Comment Generates a comment that is sent to the client. Ø Syntax <!--

HTML Comment Generates a comment that is sent to the client. Ø Syntax <!-- comment [ <%= expression %> ] --> Ø Example: <!-- This page was loaded on <%= (new java. util. Date()). to. Locale. String() %> --> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 10

Declaration Declares a variable or method valid in the scripting language used in the

Declaration Declares a variable or method valid in the scripting language used in the JSP page. Ø Syntax <%! declaration; [ declaration; ]+ . . . %> Ø Examples <%! String destin; %> <%! Public String get. Destination() {return destin; }%> <%! Circle a = new Circle(2. 0); %> Ø You can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. Ø The declaration must be valid in the Java programming language. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 11

Declaration Example <HTML> <HEAD><TITLE>JSP Declarations</TITLE></HEAD> <BODY><H 1>JSP Declarations</H 1> <%! private int keep. Count

Declaration Example <HTML> <HEAD><TITLE>JSP Declarations</TITLE></HEAD> <BODY><H 1>JSP Declarations</H 1> <%! private int keep. Count = 0; %> <H 2> Page accessed: <%= ++keep. Count %> times </H 2> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 12

Predefined Variable – Implicit Objects request – cookies Object of Http. Servlet. Request (request

Predefined Variable – Implicit Objects request – cookies Object of Http. Servlet. Request (request parameters, HTTP headers, response – out - Object of Http. Servlet. Response Object of Print. Writer buffered version Jsp. Writer session - Object of Http. Session associated with the request application config - Object of Servlet. Context shared by all servlets in the engine Object of Servlet. Config page. Context page – Object of Page. Context in JSP for a single point of access variable synonym for this object Copyright @ 2000 Jordan Anastasiade. All rights reserved. 13

Expression Contains an expression valid in the scripting language used in the JSP page.

Expression Contains an expression valid in the scripting language used in the JSP page. Ø Syntax <%= expression %> <%! String name = new String(“JSP World”); %> <%! public String get. Name() { return name; } %> <B><%= get. Name() %></B> Ø Description: An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Ø Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or@not it is. Anastasiade. tagged. Allwith HTML, in a 14 Copyright 2000 Jordan rights reserved. JSPfile. Expressions are evaluated from left to right.

Expression Example <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> </HEAD> <BODY> <H 2>JSP Expressions</H 2> <UL> <LI>Current

Expression Example <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> </HEAD> <BODY> <H 2>JSP Expressions</H 2> <UL> <LI>Current time: <%= new java. util. Date() %> <LI>Your hostname: <%= request. get. Remote. Host() %> <LI>Your session ID: <%= session. get. Id() %> </UL> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 15

Scriptlet Contains a code fragment valid in the page scripting language. Ø Syntax <%

Scriptlet Contains a code fragment valid in the page scripting language. Ø Syntax <% code fragment %> <% String var 1 = request. get. Parameter("name"); out. println(var 1); %> This code will be placed in the generated servlet method: _jsp. Service() Copyright @ 2000 Jordan Anastasiade. All rights reserved. 16

Scriplet Example <HTML> <HEAD><TITLE>Weather</TITLE></HEAD> <BODY> <H 2>Today's weather</H 2> <% if (Math. random() <

Scriplet Example <HTML> <HEAD><TITLE>Weather</TITLE></HEAD> <BODY> <H 2>Today's weather</H 2> <% if (Math. random() < 0. 5) { %> Today will be a <B>suny</B> day! <% } else { %> Today will be a <B>windy</B> day! <% } %> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 17

JSP Lifecycle Servlet from JSP Init Event Request Response Destroy Event jsp. Init() jsp.

JSP Lifecycle Servlet from JSP Init Event Request Response Destroy Event jsp. Init() jsp. Service() jsp. Destroy() Copyright @ 2000 Jordan Anastasiade. All rights reserved. 18

JSP Page Directives are messages to the JSP container and do not produce output

JSP Page Directives are messages to the JSP container and do not produce output into the current output stream Ø Syntax: <%@ directive attribute=“value” %> <%@ directive attribute 1=“value 1” attribute 1 =“value 2” … There are three types of directives: %> 1. page 2. include 3. taglib XML form: <jsp: directive. Type attribute=“value” /> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 19

Page Directive Defines attributes that apply to an entire JSP page. <%@ page [

Page Directive Defines attributes that apply to an entire JSP page. <%@ page [ language="java" ] [ extends="package. class" ] [ import="{package. class | package. *}, . . . " ] [ session="true|false" ] [ buffer="none|8 kb|sizekb" ] [ auto. Flush="true|false" ] [ is. Thread. Safe="true|false" ] [ info="text" ] [ error. Page="relative. URL" ] [ content. Type="mime. Type [ ; charset=character. Set ]" [ is. Error. Page="true|false" ] %> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 20

Include Directive Includes a static file in a JSP file, parsing the file's JSP

Include Directive Includes a static file in a JSP file, parsing the file's JSP elements. Ø Syntax <%@ include file="relative. URL" %> The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. <%@ include %> process is static. A static include means that the text of the included file is added to the JSP file. The included file can be: 1. JSP file, 2. HTML file, 3. text file. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 21

Taglib Directive Defines a tag library and prefix for the custom tags used in

Taglib Directive Defines a tag library and prefix for the custom tags used in the JSP page. Ø Syntax <%@ taglib uri="URITo. Tag. Library" prefix="tag. Prefix" %> <%@ taglib uri="http: //thathost/tags" prefix="public" %> <public: loop> </public: loop> The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 22

Server Redirection One can forward to a text file (HTML), a CGI script, a

Server Redirection One can forward to a text file (HTML), a CGI script, a servlet or another JSP page. One can only forward to a new page, provided no output of the original page has been sent to the browser. One may pass as many parameters as one needs with this method by using the param tag. The forward action ends execution of the current JSP page and removes any existing buffered output. The new page has access to application, request, and session objects as the starting file. A new page. Context object is generated for the page. To the browser, it will appear you have the originally requested page, not the page to which you are transferred. Example: <jsp: forward page="home/Default. jsp" > <jsp: param name="source" value="entry"/> </jsp: forward> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 23

<jsp: forward> Forwards a client request to an HTML file, JSP file, or servlet

<jsp: forward> Forwards a client request to an HTML file, JSP file, or servlet for processing. ØSyntax <jsp: forward page="{relative. URL | <%= expression %>}" /> <jsp: forward page="{relative. URL | <%= expression %>}" > <jsp: param name="parameter. Name" value="{parameter. Value | <%= expression %>}" />+ </jsp: forward> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 24

<jsp: use. Bean> Locates or instantiates a bean with a specific name and scope.

<jsp: use. Bean> Locates or instantiates a bean with a specific name and scope. <jsp: use. Bean id="bean. Instance. Name" scope="page|request|session|application" { class="package. class" | type="package. class" | class="package. class" type="package. class" | bean. Name="{package. class | <%= expression %>}“ } { /> | > other elements </jsp: use. Bean> } Copyright @ 2000 Jordan Anastasiade. All rights reserved. 25

Attributes and Usage id="bean. Instance. Name" A variable that identifies the bean in the

Attributes and Usage id="bean. Instance. Name" A variable that identifies the bean in the scope you specify. The name is case sensitive and must conform to the naming conventions of the scripting language used in the JSP page scope="page|request|session|application“ page One can use the bean within the JSP page with the <jsp: use. Bean> element or any of the page's static include files, until the page sends a response back to the client or forwards a request to another resource request One can use the bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another resource. One can use the request object to access the bean, for example, request. get. Attribute(bean. Instance. Name). session One can use the bean from any JSP page in the same session as the JSP page that created the bean. The bean exists across the entire session, and any page that participates in the session can use it. The page in which you create the bean must have a page directive with session="true". Copyright @ 2000 Jordan Anastasiade. All rights reserved. application One can use the bean from any JSP page in the same 26

Different Scope Most visible Application Objects accessible from pages Belong to the same application.

Different Scope Most visible Application Objects accessible from pages Belong to the same application. Session Objects accessible from pages Belonging to the same session. Request Objects accessible from pages processing the request. Page Objects accessible only within pages where they were created. Least visible Copyright @ 2000 Jordan Anastasiade. All rights reserved. 27

<jsp: set. Property> Sets a property value or values in a bean. Ø Syntax

<jsp: set. Property> Sets a property value or values in a bean. Ø Syntax <jsp: set. Property name="bean. Instance. Name" { property="*" | property="property. Name" [ param="parameter. Name" ] | property="property. Name" value="{string | <%= expression %>}" } /> Ø Examples <jsp: set. Property name="mybean" property="*" /> <jsp: set. Property name="mybean" property="username" value="Steve" /> The <jsp: set. Property> element sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with<jsp: use. Bean> before you set a property value with <jsp: set. Property>. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 28

<jsp: get. Property> Gets the value of a bean property so that you can

<jsp: get. Property> Gets the value of a bean property so that you can display it in a result page. Ø Syntax <jsp: get. Property name="bean. Instance. Name“ property="property. Name" /> Ø Example: <jsp: use. Bean id="calendar" scope="page" class="employee. Calendar" /> <h 2> Calendar of <jsp: get. Property name="calendar" property="username" /> </h 2> The <jsp: get. Property> element gets a bean property value using the property's getter methods and displays the property value in a JSP page. You must create or locate a bean with <jsp: use. Bean> before you use <jsp: get. Property>. Copyright @ 2000 Jordan Anastasiade. All rights reserved. 29

Jsp with Beans public class Message. Bean { private String message = "No Message";

Jsp with Beans public class Message. Bean { private String message = "No Message"; public String get. Message() { return message; } public void set. Message(String message) { this. message = message; } } Copyright @ 2000 Jordan Anastasiade. All rights reserved. 30

Java. Beans with JSP Page <jsp: use. Bean id="first. Bean" scope="session" class="pack. Beans. Message.

Java. Beans with JSP Page <jsp: use. Bean id="first. Bean" scope="session" class="pack. Beans. Message. Bean"/> <jsp: set. Property name="first. Bean" property="message" value="This is a message from a bean" /> <H 1>Message: <I><font color="#0000 FF" size=+3> <jsp: get. Property name="first. Bean" property="message" /> </I></font> </H 1> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 31

Handling Forms with JSP package pack. Beans; public class Name. Bean { private String

Handling Forms with JSP package pack. Beans; public class Name. Bean { private String name; public Name. Bean() { name = null; } public String get. Name() { return name; } public void set. Name(String a. Name) { name = a. Name; } } Copyright @ 2000 Jordan Anastasiade. All rights reserved. 32

JSP Form <jsp: use. Bean id='nb' scope='session' class=‘pack. Beans. Name. Bean'/> <jsp: set. Property

JSP Form <jsp: use. Bean id='nb' scope='session' class=‘pack. Beans. Name. Bean'/> <jsp: set. Property name='nb' property="*"/> <HTML> <BODY> <H 1>Please enter your name to be registered to JSP Course? </H 1> <FORM method="get"> <INPUT type="text" name="name" size="25"> <INPUT type="submit" value="Submit"> </FORM> <% if ( request. get. Parameter("name") != null ) } %> <%= "Click<a href=Get. Name. jsp> here</a> to confirm your registration" %> <% } %> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 33

JSP Results <jsp: use. Bean id='nb' scope="session“class=“pack. Beans. Name. Bean"/> <HTML> <HEAD> <TITLE>Registered Name</TITLE>

JSP Results <jsp: use. Bean id='nb' scope="session“class=“pack. Beans. Name. Bean"/> <HTML> <HEAD> <TITLE>Registered Name</TITLE> </HEAD> <BODY> <jsp: get. Property name="nb" property="name"/> </BODY> </HTML> Copyright @ 2000 Jordan Anastasiade. All rights reserved. 34

JSP and Java. Beans Copyright @ 2000 Jordan Anastasiade. All rights reserved. 35

JSP and Java. Beans Copyright @ 2000 Jordan Anastasiade. All rights reserved. 35

Conclusion Java. Server Pages (JSP) lets you separate the dynamic part of your pages

Conclusion Java. Server Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. 1. One can simply write the regular HTML in the normal manner, using whatever Web-page-building tools you normally use. 2. One can enclose then the code for the dynamic parts in special tags, most of which start with "<%" and end with "%>" Copyright @ 2000 Jordan Anastasiade. All rights reserved. 36