Java Server Pages Copyright Wipro Technologies Talent Transformation

  • Slides: 27
Download presentation
Java Server Pages Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Java Server Pages Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Objectives At the end of this session, you will be able to: • Distinguish

Objectives At the end of this session, you will be able to: • Distinguish JSP architecture vis-à-vis servlets • Define and use the basic JSP Elements • Create and use Java Beans • Work with Cookies • Define Session Tracking and use the same in JSPs Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

What is JSP stands for Java Server Pages, a technology invented by Sun to

What is JSP stands for Java Server Pages, a technology invented by Sun to allow the easy creation of server side HTML pages. Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

JSP Request Model Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

JSP Request Model Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

What makes JSP so attractive? • • High performance Convenient to code and maintain

What makes JSP so attractive? • • High performance Convenient to code and maintain as against servlets Separates presentation from content Powered by Java – Has access to all Java APIs – Has access to all J 2 EE APIs – Inherent Platform independence Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

JSP compared to ASP • Similarities – Both provide built in session tracking –

JSP compared to ASP • Similarities – Both provide built in session tracking – Designed to create interactive pages in a Web application – Separate presentation logic from programming logic • Differences – JSPs are designed for Platform independence and server independence – Open development process, has come through the JCP (Java Community Process) and hence broader acceptance – JSP enables developers to extend the tags – JSP uses a full fledged language like Java for scripting while ASP uses VBScript or Jscript, which are limited Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The Architecture Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The Architecture Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The flow of JSP request Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0

The flow of JSP request Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Elements of JSP The three basic elements of a JSP are: • Scripting elements

Elements of JSP The three basic elements of a JSP are: • Scripting elements • Directives • Actions Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Scripting Elements • Lets you insert Java code into the servlet that will be

Scripting Elements • Lets you insert Java code into the servlet that will be generated from the current JSP page • There are three forms: - Expressions - Scriptlets - Declarations Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Expressions A JSP expression is used to insert Java values directly into the output.

Expressions A JSP expression is used to insert Java values directly into the output. It has the following form: <%= Java Expression %> Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Predefined Variables • To simplify the expressions, there a number of predefined variables that

Predefined Variables • To simplify the expressions, there a number of predefined variables that you can use • The most important ones are: • • Copyright Wipro Technologies Talent Transformation request response session out JSP Ver 1. 0 Page

Scriptlets • Are defined as any block of valid Java code that resides between

Scriptlets • Are defined as any block of valid Java code that resides between <% and %> tags • Code that is defined within a scriptlet can access any variable and any beans that have been declared Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Declaration • Used to define methods or fields that get inserted into the main

Declaration • Used to define methods or fields that get inserted into the main body of the servlet class • It has the form: <%! Java Code %> • 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 Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Directives • Affect the overall structure of the servlet class generated from this JSP.

Directives • Affect the overall structure of the servlet class generated from this JSP. • format: <%@ directive attribute="value" %> • There are two main types of directives: – page – include Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Page Directives • Defines attributes that apply to an entire JSP page • Lets

Page Directives • Defines attributes that apply to an entire JSP page • Lets you do things like: - Copyright Wipro Technologies Talent Transformation Import classes Handle error messages Define if the JSP is thread-safe Define is session object is available Set the page content type JSP Ver 1. 0 Page

The Include Directive · Inserts the contents of another file in the main JSP

The Include Directive · Inserts the contents of another file in the main JSP file, where the directive is located · Useful for including copyright information, scripting language files, or anything you might want to reuse in other applications · The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Actions • Control the behavior of the servlet engine • You can dynamically insert

Actions • Control the behavior of the servlet engine • You can dynamically insert a file, reuse Java. Beans components • Available actions include: - jsp: include - jsp: forward - jsp: use. Bean Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The jsp: include Action • Lets you insert files into the page being generated

The jsp: include Action • Lets you insert files into the page being generated • The syntax looks like this: <jsp: include page="relative URL" /> • Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The jsp: forward Action • Forwards a client request to an HTML file, JSP

The jsp: forward Action • Forwards a client request to an HTML file, JSP file, or servlet for processing. • Syntax – <jsp: forward page= “relative. URL" /> Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

The jsp: use. Bean Action • Lets you load in a Java. Bean to

The jsp: use. Bean Action • Lets you load in a Java. Bean to be used in the JSP page • The simplest syntax for specifying that a bean should be used is: <jsp: use. Bean id="name" class="package. class" /> • This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id. " Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

<jsp: get. Property> • Converts property names following the bean standards • Has two

<jsp: get. Property> • Converts property names following the bean standards • Has two attributes: - name="bean. Instance. Name" • The name of the Bean instance as declared in a <jsp: use. Bean> tag - property="property. Name" • The name of the Bean property whose value you want to display Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

<jsp: set. Property> • Sets the value of one or more properties in a

<jsp: set. Property> • Sets the value of one or more properties in a Java. Bean component • Syntax: <jsp: set. Property name="bean. Instance. Name" property= "*" | property="property. Name" [param=parameter. Name" ] | property= "property. Name" value="{ string | <%= expression %> }" } /> Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Properties of JSP: set. Property • • name property value param Copyright Wipro Technologies

Properties of JSP: set. Property • • name property value param Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Cookies • Allow the web server to store small pieces of data on the

Cookies • Allow the web server to store small pieces of data on the client that can be sent back to the server on subsequent page requests • Are often used to store user IDs or basic configuration information • To read cookies from the browser, use the request. get. Cookies() function Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Session Management • JSP maintains session through the Http. Session object • Session information

Session Management • JSP maintains session through the Http. Session object • Session information is stored on the server, and a session ID number is stored in a cookie on the client machine • Sessions are usually set by server default to expire after 30 minutes of user inactivity Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page

Summary In this section, you have learnt to: • Distinguish JSP architecture vis-à-vis servlets

Summary In this section, you have learnt to: • Distinguish JSP architecture vis-à-vis servlets • Define and use the basic JSP Elements • Create and use Java Beans • Work with Cookies • Define Session Tracking and use the same in JSPs Copyright Wipro Technologies Talent Transformation JSP Ver 1. 0 Page