What is JSP ?

advertisement

Creating JavaServer Pages TM

Rambabu Manchikalapudi

What is JSP

TM

?

Server-Side Scripting language (Basically HTML with Java

Code or JSP tags usage and a file with “.jsp” extension).

Cross Platform/ Cross WebServer method of creating

Dynamic and Interactive webpages.

Part of the Java TM 2 Enterprise Edition by Sun Microsystems and shares the “ Write Once, Run Anywhere TM ” characteristics and the Power of JAVA programming language.

1

Why JavaServer Pages

TM

?

To provide an efficient answer to CGI Scripting technology which lacks scalability.

To Adapt to the requirement for dynamic web pages and separate business logic from Programming logic.

Harness the power of a scripting language that is built on a complete Programming Language (Java).

Ability to access enterprise services ( DB, CORBA, RMI).

JSP and JavaBeans

JavaBeans are reusable software components written in Java.

Similar to ActiveX components,but designed to be platform neutral and running anywhere there is a Java Virtual Machine.

To use a JavaBean with a JSP, use the tags <useBean/>,

<setProperty/>, and <getProperty/>.

(Do getProperty & setProperty 1 st )

-

JavaBeans are used in JSP files to add functionality, enable debugging and provide modularity to the JSP page.

2

JSP and Servlets

Servlets are similar to applets, but run on the server.

They facilitate independent, interactive web applications.

JSP pages eventually get compiled into servlets when sent onto the server side.

Servlets are a secure method of transaction processing as they h have information about the clients and use SSL (Secure socket layer to transmit data).

Both JSP and Java servlets share all the features of the Java 2

Enterprise Edition.

JSP and Java servlets rely on re-usable, cross platform components known as JavaBeans and Enterprise Java Beans.

JSP

TM

Architecture

JSP File

Request

Browser

Complilation

Response

Components :

Servlet

Servlet Container

-

Webserver is a software that accepts requests from the and sends information back to be displayed on the browser.

Ex: Apache Web Server available at http://www.apache.org

- Container is a software that stores JSP files,Servlets,transforms

JSP files to servlets, compiles and runs servlets producing HTML.

Ex: Tomcat Container also developed by Apache Software, available at http://jakarta.apache.org

3

JSP

TM

tags and Syntax

• Directives

- Page Directive tag.

- Include Directive tag.

• Declarations tag.

• Expressions and Scriptlets tag.

• Comments tag.

Directive tags

-

Are Messages to the JSP engine.

-

Do not produce any visible output.

-

JSP directives are always enclosed within the

<%@ ... %> tags.

-

The two primary directives are ‘page’ and ‘include’.

-

JSP version 1.1 provides support for one other type of directive called the “taglib” directive used for custom tag libraries.

4

Page and Include Directives

• A Page directive is typically found at the top of the JSP Page.

There can be any number but attribute/value pair must be unique.

Ex: <%@ page import=“java.util.*,com.abc.*” buffer=“16k” %>

This makes available the types declared within the packages mentioned and sets the page bufferingto 16k.

• An Include directive seperates the page contents into more manageable elements by including a HTML or a JSP file content into the current file.

Ex: <%& include file=“showcart.jsp” %>

Declaration tag

• Page level variables to save information and define methods that the rest of the JSP page will need.

• Declarations are always found within the <%!….. %> tags.

• Always end variable declarations with a semicolon.

Ex: <%! Int I=0; %>

• Methods also can be declared to override an existing method

Ex : <%! Public void JspInit(){

// some code

}

%>

• If there is a lot of logic-intensive components, JavaBeans can be used to help in better debugging and improve re-usability.

5

Expressions and Scriptlets tag

Expressions

Evaluation result of strings directly embedded into HTML page

They have the <%= ….%> tags and do not have semicolons

Ex : <%= somevar %> , <%= somebean.getName() %>

Scriptlets

Any Java code within a scriptlet & can run into multiple lines.

Scriplets have <%…%> tags and are of the following form

Ex: <% for(int I=0; I<=4; I++) {

<b>Hello</b>

}

%>

Page Forward tag

- When multiple JSP pages are required to process a request, a page forward tag is used to send all information to another page

- An include request also often serves the same purpose.

- Two ways to forward, request “parameters” and “attributes”.

- String values only can be passed using the request parameters

shown here

Ex : <jsp:forward page="login.jsp">

<jsp:param name="errMsg“ value="The name or password is not valid" />

</jsp:forward>

If an object needs to be passed, a JavaBean is used with t he request attributes method. The bean is created by the first page and forwarded to the second.

6

Comment/hidden comment tag

This is similar to comments in the regular HTML page except that users cannot view the comments when they

“view source” the page.

The comments are embedded in <%-- ….. --%> tag.

Ex : <%-- This comment is for me --%>

Can be used effectively during the debugging and testing process by blocking scriptlets before page compilation.

JSP

TM

Object Scopes & Implicit Objects

Most

Visible

Least

Visible

Application

Session

Request

Page

Objects are created implicitly using JSP directives, explicitly through actions and directly through Scripting code.

4 types of visibility scopes.

Nine Implicit Objects in JSP

• HttpServletRequest triggers service invocation :

Request Scope.

• HttpServletResponse responds to the request :

Page Scope

• PageContext gives implementation dependant features : Page Scope

• ServletContext obtains servlet configuration :

Application Scope

• JspWriter object writes to the Output stream : Page Scope

• ServletConfig has the configuration of the JSP page : Page Scope

• HttpJspPage refers to the same page and is like a “this” pointer : Page Scope

• HttpSession refers to a particular session that the JSP page is in : Session Scope

• Throwable refers to an uncatched exception that resulted in error page : Page Scope

7

Exception Handling

JSP provides a good mechanism for handling runtime exceptions.

It is possible to provide customized error handling.

By use of the page directive’s “errorPage” object an uncaught exception can be forwarded to an appropriate JSP page for processing. The sending page should then, for example, be marked as

<%@ page isErrorPage=“false” errorPage=“error.jsp” %>

The “error.jsp” page in question has to mark itself as an error handling page by using a similar statement.

<%@ page isErrorPage=“true” %> so that, the throwable object of the exception can be accessed through the implicit exception object of the new page.

Request Chaining

Used primarily to process HTML forms.

It is a method of passing beans around different JSP pages in an application which are embedded in a request object by the initiating page.

The bean is extracted by the servlet or JSP page to which it is passed, alters attributes if necessary, adds new attributes or adds another JavaBean in the request process and sends it to another page.

The <jsp:include> tag can be used to redirect the request to any static or dynamic resource which is in the same context as the calling page.

8

Synchronization

The servicing mechanism of a JSP page is multithreaded.

It has to be ensured that access to the shared state is synchronized.

It can be done by simply including the following in the JSP page

<% page isThreadSafe=“True” %>

This will ensure that multiple copies of servlet are loaded in the memory and client requests are served from these copies in the servlet pool in a round robin fashion.

However, this solution is not scalable and causes delay in the client service method and therefore is ineffective.

Other better approach is to do it by explicitly synchronizing acess to shared objects using scriptlets.

Session Management

All JSP pages take part in HttpSession and all linked JSP pages can access information stored in other JSP files using the “session” implicit object unless excluded by having the statement

<%@ page session=“false” %>.

Primitive data types are not allowed. Only Java Objects.

Beans and Objects that need to be shared across other pages are stored in sessions.

Session objects are identified by session ID stored in browser as cookie.

A session object maybe created and shared across the session as follows :

<% car a= new car(); session.putValue=(“xyz”, a);

%>

It maybe retrieved in another page by using a statement, for example :

<% car newcar=(car).session.getValue(“xyz”);

%> ( Type casting must be done carefully)

- No.of Objects (HeapSpace Consideration) and limit max.objects possible.

9

JSP

TM

Vs ASP

TM

Similarities

- Create Interactive web pages.

- Faster and easier than CGI.

- Use Object Oriented Scripting.

- Use Component Technology.

- ASP+ (Part of .NET from Microsoft) feels more like JSP in that the pages compile dynamically and are executed on a virtual machine written in C#.

JSP

TM

Vs ASP

TM cont’d…

Differences

- Platforms : ASP runs only on Microsoft ® Windows whereas JSP runs also on Solaris, Mac OS and Linux.

- Webservers : ASP runs on Microsoft ® IIS or personal webserver and select servers using third-party software whereas JSP runs on most of the existing servers.

Component Model : ASP uses Win-32 COM model whereas JSP uses JavaBeans and Enterprise JavaBeans.

Scripting : ASP supports VBScript and Jscript while JSP uses pure Java or JavaScript.

Security Model : ASP uses Win NT and JSP uses Java Security model.

DataBase Access : ASP uses “Active Data Objects” while JSP uses JDBC for database access.

Custom tags : Custom tag library is supported in JSP and not in ASP.

10

Advantages of JSP

TM

Open, Extensible & Portable standard for building dynamic webpage.

JSP is more powerful than JavaScript and Server Side Includes.

-

Servlets are closely similar but JSP helps in sharing the design load and also by implementing JavaBeans it is modular.

It wins over ASP in terms of Platform independancy, Web Server compatibility and being portable.

Dynamic content can be catered to HTML/DHTML,Wireless phones and PDA’s.

Customizable tag library to cater to a variety of applications.

Power of JAVA and robust since it is interpreted, not interpreted.

Future of JSP

TM

Entirely dependant on the fate of Java Programming Language.

Here to stay, since the standard is open to change and JSP is Cross platform.

Compatibility and Co-existance with other emerging standards like

XML ensures growth and continued usage.

JSP 1.2 Specifications Final Release http://jcp.org/aboutJava/communityprocess/first/jsr053/index.html

11

References

SUN Microsystems Product website for JSP http://java.sun.com/products/jsp/index.html

APACHE HTTP Web Server for JSP support http://httpd.apache.org/

Jakarta Tomcat Project (Web Container for JSP) http://jakarta.apache.org/tomcat/index.html

12

Download