+ All Categories
Home > Documents > JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and...

JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and...

Date post: 18-Jan-2016
Category:
Upload: benedict-tucker
View: 218 times
Download: 0 times
Share this document with a friend
51
JSP BASICS AND ARCHITECTURE
Transcript
Page 1: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

JSP BASICS AND ARCHITECTURE

Page 2: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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

Page 3: JSP BASICS AND ARCHITECTURE. 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 a specification. JSP More on line of the J2EE

specification. Can utilize the Beans to separate the code

out . Is compiled to Servlet the first time call is

received for the page.

Page 4: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

THE PROCESS

JSP <JSP Tags><Java code>

Servlet

Client

JSP <JSP Tags><Java code>

JSP <JSP Tags><Java code>

First Request

Generates Response

Text

Uses

Uses

Page 5: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

A SIMPLE JSP CODE%@page import=“java.util.Date”%

<html>

<body>

The current time is <%=new Date().toString() %>

</body>

</html>

Page 6: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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 .

Page 7: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

The JSP Life Cycle public void jspInit() used for initialization

purposes defined in javax.servlet.jsp.JSPpage Interface.

public void jspDestroy() used for performing cleanup operations defined in javax.servlet.jsp.JSPPage Interface.

public void _jspService(HttpServletRequest request, HttpServletResponese response) throws ServletException,IOException.The implementation of this method is generated by the container and should never be provided by Page authors.

Page 8: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 9: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 10: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Types of JSP Tags Directives. Scripting Elements. Actions.

Page 11: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 12: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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

Page 13: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 14: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Program utilizing Page Directives<%@ page language="Java" session="true"

import="java.rmi.*,java.util.*" session="true" buffer="12kb"

autoFlush="true" info="my page directive jsp" errorPage="error.jsp"

isErrorPage="false" isThreadSafe="false" %>

Page 15: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Program utilizing Page Directives<html><head><title>Page Directive test Page</title></head><h1>Page directive test page</h1>

This is a JSP to test the page directive.</body></html>

Page 16: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 17: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 18: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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

<%@ include file="/copyright.html" %></body></html>

Page 19: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

The copyright HTML file<p>&copy; 2000 Wrox Press</p>

Page 20: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

OutputInclude directive test page 1

© 2000 Wrox Press

Page 21: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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=“tagLibraryURI”

prefix=“tagprefix” %>

Page 22: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Scripting Elements These allow Java Code – variable or

method declarations , scriptlets (arbitrary Java code) and expressions to be inserted into your JSP page.

Page 23: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Types of Scripting ElementsThey are of three types: Declarations Scriptlets Expressions

Page 24: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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) %>

Page 25: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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 %>

Page 26: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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 toString() method.

Syntax: <%= Java expression to be evaluated %>

Page 27: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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.

Page 28: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Types of the Standard Action <jsp:usebean> <jsp:setProperty> <jsp:getProperty> <jsp:param> <jsp:include> <jsp:forward> <jsp:plugin>

Page 29: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<jsp:useBean> 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:useBean id=“name” scope=“scopeName” beanDetails/>

Page 30: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<jsp:setProperty> It is used with the <jsp:useBean> 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:setProperty name=“help” property=“word” />

Page 31: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<jsp:getProperty> 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:getProperty name=“name” property=“propertyName” />

Page 32: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Example utilizing <jsp:useBean> <jsp:setProperty> <jsp:getProperty>

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 LanguageBean.class it retrieves the information and displays it to the user.

Page 33: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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

Page 34: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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>

Page 35: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

beans.jsp<jsp:useBean id="languageBean" scope="page"

class="LanguageBean"><jsp:setProperty name="languageBean" property="*" /></jsp:useBean>

<html><head><title>useBean action test result</title></head>

Page 36: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

beans.jsp continued…<body>

<h1>useBean action test result</h1>

<p> Hello, <jsp:getProperty name="languageBean" property="name"/>.</p>

<p>Your favorite language is

<jsp:getProperty name="languageBean" property="language"/>.</p>

Page 37: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

beans.jsp continued…<p>My comments on your favorite

language:</p>

<p><jsp:getProperty name="languageBean" property="languageComments" />

</p>

</body>

</html>

Page 38: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

LanguageBean.classpublic class LanguageBean{

private String name;private String language;

public LanguageBean() {}

public void setName(String name){this.name=name;}

Page 39: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

LanguageBean.class continued..

public String getName(){

return name;

}

public void setLanguage(String language){

this.language=language;

}

Page 40: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

LanguageBean.class continued..public String getLanguage(){

return language;

}

public String getLanguageComments(){

if(language.equals("Java")){

return "The king of OOP languages.";

}

Page 41: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

LanguageBean.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+".";}}}

Page 42: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

Output of the CodeuseBean action test result

Hello, Navdeep Mahajan.

Your favorite language is Java.

My comments on your favorite language:

The king of OOP languages.

Page 43: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<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” />

Page 44: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<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 JspWriter 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.

Page 45: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<jsp:include> Syntax:

<jsp:include page=“URL” flush=“true”>

<jsp:param name=“paramname” value=“paramvalue” />

……

</jsp:include>

Page 46: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<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..

Page 47: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<jsp:forward> Syntax:

<jsp:forward page=“URL”>

<jsp:param name=“paramname” value=“paramvalue” />

…..

</jsp:forward>

Page 48: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<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 JavaBeans component that is specified in the tag.

Page 49: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

<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.

Page 50: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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”>

Page 51: JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.

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

“paramvalue” />………</jsp:params><jsp:fallback>Alternate text to display

</jsp:fallback></jsp:plugin>


Recommended