JSP BASICS AND ARCHITECTURE Goals of JSP Simplify

  • Slides: 51
Download presentation
JSP BASICS AND ARCHITECTURE

JSP BASICS AND ARCHITECTURE

Goals of JSP ü ü Simplify Creation of dynamic pages. Separate Dynamic and Static

Goals of JSP ü ü Simplify Creation of dynamic pages. Separate Dynamic and Static content.

What are JSP? ü ü JSP is a specification and not a product but

What are JSP? ü ü JSP is a specification and not a product but a specification. JSP More on line of the J 2 EE specification. Can utilize the Beans to separate the code out. Is compiled to Servlet the first time call is received for the page.

THE PROCESS First Request Uses JSP <JSP Tags> <Java code> JSP Text <JSP Tags>

THE PROCESS First Request Uses JSP <JSP Tags> <Java code> JSP Text <JSP Tags> Uses <Java code> Client Servlet Generates Response

A SIMPLE JSP CODE %@page import=“java. util. Date”% <html> <body> The current time is

A SIMPLE JSP CODE %@page import=“java. util. Date”% <html> <body> The current time is <%=new Date(). to. String() %> </body> </html>

The Container ü According to the JSP specifications that the servlets implement the Servlets

The Container ü According to the JSP specifications that the servlets implement the Servlets must extend from: Ø Specified by JSP author via. Extends command. Or Ø A container specific Implementation class that implements javax. servlet. jsp. JSPPage interface.

The JSP Life Cycle ü ü ü public void jsp. Init() used for initialization

The JSP Life Cycle ü ü ü public void jsp. Init() used for initialization purposes defined in javax. servlet. jsp. JSPpage Interface. public void jsp. Destroy() used for performing cleanup operations defined in javax. servlet. jsp. JSPPage Interface. public void _jsp. Service(Http. Servlet. Request request, Http. Servlet. Responese response) throws Servlet. Exception, IOException. The implementation of this method is generated by the container and should never be provided by Page authors.

General Rules for JSP Pages ü ü JSP tags are case sensitive. Attribute values

General Rules for JSP Pages ü ü JSP tags are case sensitive. Attribute values in tags always appear quoted, Single or double. Eg: <somejsptag attributename=“attribute value”> body</somejsptag> Or <somejsptag attributename=“attribue value” /> The Second one is valid in case when there is no body.

General Rules for JSP Pages ü ü The character  can be used as

General Rules for JSP Pages ü ü The character can be used as an escape character eg: to use %. URL that does not start with / are interpreted relative to the current JSP. URL starting with a / , called a context relative path, is interpreted with reference to the web application to which the JSP page belongs. Any white spaces with in the body text of a document are not significant although they are preserved during translation into a servlet.

Types of JSP Tags ü ü ü Directives. Scripting Elements. Actions.

Types of JSP Tags ü ü ü Directives. Scripting Elements. Actions.

JSP Directives ü ü They serve as messages to the JSP container from the

JSP Directives ü ü They serve as messages to the JSP container from the JSP. Used to set global values as Ø Class Declarations Ø Methods to be implemented. Ø Output Content Type. They don’t produce any output to the user. Affects the whole file but only that file.

Types of JSP Directives ü ü ü page Directive. include Directive. taglib Directive.

Types of JSP Directives ü ü ü page Directive. include Directive. taglib Directive.

page Directive ü ü ü Used to define and manipulate a number of page

page Directive ü ü ü Used to define and manipulate a number of page dependent attributes that affect the whole JSP. A page can contain any number of page directives , in any order, anywhere in the JSP. They are assimilated during translation. There can be only one occurrence of attribute/value pair defined by the page directives.

Program utilizing Page Directives <%@ page language="Java" session="true" import="java. rmi. *, java. util. *"

Program utilizing Page Directives <%@ page language="Java" session="true" import="java. rmi. *, java. util. *" session="true" buffer="12 kb" auto. Flush="true" info="my page directive jsp" error. Page="error. jsp" is. Error. Page="false" is. Thread. Safe="false" %>

Program utilizing Page Directives <html> <head> <title>Page Directive test Page</title> </head> <h 1>Page directive

Program utilizing Page Directives <html> <head> <title>Page Directive test Page</title> </head> <h 1>Page directive test page</h 1> This is a JSP to test the page directive. </body> </html>

Error in the Code This code will throw an error at present because the

Error in the Code This code will throw an error at present because the language page directive has been kept there for future use if JSP containers support multiple languages. At present this is not supported therefore it will through could not locate style sheet for JAVA error.

include Directive ü ü ü It instructs the container to include the content of

include Directive ü ü ü It instructs the container to include the content of the resource in the current JSP by inserting it inline. The include action occurs at runtime. Eg: <%@ include file=“Filename” %> The only available attribute file specifies the file name to be included. The included file can be a static resource such as an HTML file or another JSP. The code is inserted inline.

A simple Include Directive code <html> <head> <title>Include Directive test page 1</title> </head> <body>

A simple Include Directive code <html> <head> <title>Include Directive test page 1</title> </head> <body> <h 1>Include directive test page 1</h 1> <%@ include file="/copyright. html" %> </body> </html>

The copyright HTML file <p>© 2000 Wrox Press</p>

The copyright HTML file <p>© 2000 Wrox Press</p>

Output Include directive test page 1 © 2000 Wrox Press

Output Include directive test page 1 © 2000 Wrox Press

taglib Directive ü ü ü This directive allows the page to use tag extensions

taglib Directive ü ü ü This directive allows the page to use tag extensions (custom tags). It names the tag library that contains the compiled Java code. Eg: <%@ taglib uri=“tag. Library. URI” prefix=“tagprefix” %>

Scripting Elements ü These allow Java Code – variable or method declarations , scriptlets

Scripting Elements ü These allow Java Code – variable or method declarations , scriptlets (arbitrary Java code) and expressions to be inserted into your JSP page.

Types of Scripting Elements They are of three types: ü Declarations ü Scriptlets ü

Types of Scripting Elements They are of three types: ü Declarations ü Scriptlets ü Expressions

Declarations ü ü ü A declaration is a block of Java code that is

Declarations ü ü ü A declaration is a block of Java code that is used to define class wide variables and methods in the generated servlet. Declarations are initialized when the JSP page is initialized. Syntax : <%! Java Variable and method declaration(s) %>

Scriptlets ü ü A scriptlet is a block of code that is executed during

Scriptlets ü ü A scriptlet is a block of code that is executed during the request processing time. All the scriptlets in the JSP are combined in the order they appear in the code. As expected all the code for the scriptlets is put into the service() method of the servlet. Syntax: <% Valid Java code Statements %>

Expressions ü ü The expression is a shorthand notation for a scriptlet that sends

Expressions ü ü The expression is a shorthand notation for a scriptlet that sends the value of Java expression back to the client. The expression is evaluated at HTTP request processing time and the result is converted to sting and displayed. In case the result of the expression is an object the conversation is done by using the objects to. String() method. Syntax: <%= Java expression to be evaluated %>

Standard Actions ü ü ü Standard Actions are tags that affect the runtime behavior

Standard Actions ü ü ü Standard Actions are tags that affect the runtime behavior of the JSP and the response sent back to the user. They have to provided by the container irrespective of the usage. During compilation into the servlet , the container comes across the this tag and replaces it with Java code that corresponds to the required predefined task.

Types of the Standard Action ü ü ü ü <jsp: usebean> <jsp: set. Property>

Types of the Standard Action ü ü ü ü <jsp: usebean> <jsp: set. Property> <jsp: get. Property> <jsp: param> <jsp: include> <jsp: forward> <jsp: plugin>

<jsp: use. Bean> ü ü ü This tag is used to instantiate a Java

<jsp: use. Bean> ü ü ü This tag is used to instantiate a Java bean , or locate bean instance and assign it to a variable name (or id). We can also specify the lifetime of an object by giving it a specific name. <jsp: use. Bean id=“name” scope=“scope. Name” bean. Details/>

<jsp: set. Property> ü ü ü It is used with the <jsp: use. Bean>

<jsp: set. Property> ü ü ü It is used with the <jsp: use. Bean> action to set the value of bean properties. The properties in a bean can be set either: Ø At request time from parameters in the request object. Ø At request time from an evaluated expression. Ø From a specified string. Syntax: <jsp: set. Property name=“help” property=“word” />

<jsp: get. Property> ü ü ü It is used to access the properties of

<jsp: get. Property> ü ü ü It is used to access the properties of a bean. It accesses a property , converts it into a String, and prints it into the output stream of the client. Syntax: <jsp: get. Property name=“name” property=“property. Name” />

Example utilizing <jsp: use. Bean> <jsp: set. Property> <jsp: get. Property> ü For this

Example utilizing <jsp: use. Bean> <jsp: set. Property> <jsp: get. Property> ü For this example we will first of all create an HTML file beans. html in which user can insert his name and choose his favorite language from the drop down menu. Another file beans. jsp will be used to set the bean properties as per the values entered by the user and then by utilizing the methods defined in the Language. Bean. class it retrieves the information and displays it to the user.

beans. html <html> <head> <title>use. Bean action test page</title> </head> <body> <h 1>use. Bean

beans. html <html> <head> <title>use. Bean action test page</title> </head> <body> <h 1>use. Bean action test page</h 1> <form method="post" action="beans. jsp"> <p>Please enter your user name : <input type="text" name="name"> What is your favorite programming language?

beans. html continued. . <select name="language"> <option value="Java">Java <option value="c++">c++ <option value="Perl">Perl </select> </p>

beans. html continued. . <select name="language"> <option value="Java">Java <option value="c++">c++ <option value="Perl">Perl </select> </p> <p><input type="submit" value="submit information"> </form> </body> </html>

beans. jsp <jsp: use. Bean id="language. Bean" scope="page" class="Language. Bean"> <jsp: set. Property name="language.

beans. jsp <jsp: use. Bean id="language. Bean" scope="page" class="Language. Bean"> <jsp: set. Property name="language. Bean" property="*" /> </jsp: use. Bean> <html> <head> <title>use. Bean action test result</title> </head>

beans. jsp continued… <body> <h 1>use. Bean action test result</h 1> <p> Hello, <jsp:

beans. jsp continued… <body> <h 1>use. Bean action test result</h 1> <p> Hello, <jsp: get. Property name="language. Bean" property="name"/>. </p> <p>Your favorite language is <jsp: get. Property name="language. Bean" property="language"/>. </p>

beans. jsp continued… <p>My comments on your favorite language: </p> <p><jsp: get. Property name="language.

beans. jsp continued… <p>My comments on your favorite language: </p> <p><jsp: get. Property name="language. Bean" property="language. Comments" /> </p> </body> </html>

Language. Bean. class public class Language. Bean{ private String name; private String language; public

Language. Bean. class public class Language. Bean{ private String name; private String language; public Language. Bean() {} public void set. Name(String name){ this. name=name; }

Language. Bean. class continued. . public String get. Name(){ return name; } public void

Language. Bean. class continued. . public String get. Name(){ return name; } public void set. Language(String language){ this. language=language; }

Language. Bean. class continued. . public String get. Language(){ return language; } public String

Language. Bean. class continued. . public String get. Language(){ return language; } public String get. Language. Comments(){ if(language. equals("Java")){ return "The king of OOP languages. "; }

Language. Bean. class continued. . else if(language. equals("c++")){ return "Rather too complex for some

Language. Bean. class continued. . else if(language. equals("c++")){ return "Rather too complex for some folks' liking. "; } else if (language. equals("perl")){ return "OK if you like incomprehensible code. "; }else{ return "Sorry, i have never heard of" +language+". "; } } }

Output of the Code use. Bean action test result Hello, Navdeep Mahajan. Your favorite

Output of the Code use. Bean action test result Hello, Navdeep Mahajan. Your favorite language is Java. My comments on your favorite language: The king of OOP languages.

<jsp: param> ü ü It is used to provide other tags with additional information

<jsp: param> ü ü It is used to provide other tags with additional information in form of name value pairs. Syntax: <jsp: param name=“paramname” value=“paramvalue” />

<jsp: include> ü ü ü This action allows the static or dynamic resource ,

<jsp: include> ü ü ü This action allows the static or dynamic resource , specified by the URL to be included in the current JSP at request processing time. The included page has access to only Jsp. Writer object. It cannot set headers and cookies. It include page cannot have jsp tags. If the page output is buffered then the buffer is flushed prior to the inclusion. It has a small penalty on the efficiency.

<jsp: include> ü Syntax: <jsp: include page=“URL” flush=“true”> <jsp: param name=“paramname” value=“paramvalue” /> ……

<jsp: include> ü Syntax: <jsp: include page=“URL” flush=“true”> <jsp: param name=“paramname” value=“paramvalue” /> …… </jsp: include>

<jsp: forward> ü ü It allows the request to be forwaded to the another

<jsp: forward> ü ü It allows the request to be forwaded to the another JSP, to a servlet , or to a static resource. Execution in the current JSP stops when it encounters the <jsp: forward> tag, the buffer is cleared , and the request is modified. .

<jsp: forward> ü Syntax: <jsp: forward page=“URL”> <jsp: param name=“paramname” value=“paramvalue” /> …. .

<jsp: forward> ü Syntax: <jsp: forward page=“URL”> <jsp: param name=“paramname” value=“paramvalue” /> …. . </jsp: forward>

<jsp: plugin> ü It is used in pages to generate client browser specific HTML

<jsp: plugin> ü It is used in pages to generate client browser specific HTML tags like <OBJECT> or <EMBED> that result in the download of the Java Plug-in Software , if required, followed by the execution of the applet or Java. Beans component that is specified in the tag.

<jsp: plugin> supported tags ü It supports two additional support tags: Ø <jsp: params>,

<jsp: plugin> supported tags ü It supports two additional support tags: Ø <jsp: params>, to pass additional parameters to the applet or the java beans component. Ø <jsp: fallback>, to specify the content to be displayed in the client browser if the plugin cannot be started because the generated tags are not supported.

Syntax <jsp: plugin type=“bean|applet” code=“objectcode” codebase=“objectcodebase” align=“alignment” archive=“archivelist” height=“height” hspace=“hspace” jreversion=“jreversion” name=“componentname” vspace=“vspace” width=“width”

Syntax <jsp: plugin type=“bean|applet” code=“objectcode” codebase=“objectcodebase” align=“alignment” archive=“archivelist” height=“height” hspace=“hspace” jreversion=“jreversion” name=“componentname” vspace=“vspace” width=“width” nspluginurl=“url” iepluginurl=“url”>

Syntax continued. . <jsp: params> <jsp: param name=“name” value= “paramvalue” /> ……… </jsp: params>

Syntax continued. . <jsp: params> <jsp: param name=“name” value= “paramvalue” /> ……… </jsp: params> <jsp: fallback>Alternate text to display </jsp: fallback> </jsp: plugin>