You are on page 1of 19

JSP Presentation Template

<JSP OVERVIEW> <RAVI KUMAR TIWARI>

CONTENTS OF PRESENTATION

JSP DEFINITION JSP SERVERS WHY WE LIKE JSP IN FIRST PLACE HOW JSP WORKS EXAMPLE OF JSP COMPARISON OF JSP TO OTHER TECHNOLOGIES COMPONENTS OF JSP JSP STANDARD ACTIONS JSP SCRIPTING ELEMENTS JSP DIRECTIVE ELEMENTS STANDARD ACTION TAGS JSP LIFE CYCLE METHODS IMPLICIT OBJECTS JSP CUSTOM TAGS ADVANTAGES AND CONSEQUENCES WHAT IS IRRITATING ABOUT JSP OVERVIEW

JSP DEFINITION:
A text-based document capable of returning both static and dynamic content to a client browser. Static content and dynamic content can be intermixed. Static content HTML, XML, Text Dynamic content Java code Displaying properties of JavaBeans Invoking business logic defined in Custom tags

JSP SERVERS:
Blazix from Desiderata Tomcat from Apache Weblogic from BEA WebSphere from IBM And others

Why we like JSP in the first place:


Open standard with support from many vendors The performance and scalability of servlets (for JSP pages compile into servlets) Extensibility (custom tags) Easy integration with other J2EE and Java technologies (Servlets, EJB)

How JSP works:

How JSP works: (Continued)


When JSP servers needs to use a JSP page, it: Translates the JSP into a Java servlet Compiles the servlet Executes the servlet as normal Java code Hence, when you are writing JSP, you are writing higher-level Java code You have two basic choices when using JSP: Let the JSP do all the work of a servlet Write a servlet that does the work and passes the results to JSP to create the resultant HTML page This works because a servlet can call another servlet

Example JSP

<HTML> <BODY> Hello! The time is now <%= new java.util.Date() %> </BODY> </HTML> Notes: The <%= ... %> tag is used, because we are computing a value and inserting it into the HTML.

Comparison of JSP to
PHP Cold Fusion ASP JSP

Cost

Free

$0 (Express version)$5000 (Enterprise version)

Free with Windows NT, cost for 3rdparty software

Free

Language In Page

PHP

CFML

VBScript, JScript

Java

OS Platform

Unix (Linux), Windows, MacOS, OS/2

Windows NT, Solaris, Linux

Windows 9x, NT, other platforms requires third-party ASP porting products IIS, Personal Web Server (PWS), other servers with thirdparty products (ChiliSoft!)

UNIX, Microsoft Windows, Mac OS, Linux

Supported Web server

iPlanet/Netscape Enterprise Server (NSAPI), MS Internet Information Server (IIS), Apache, Zeus, fhttpd, etc. (ver 4)

IIS, Netscape Enterprise Server , Apache, Website Server (WSAPI), CGI

Any Web server, including Apache, Netscape and IIS

Comparison of JSP to (Contd)


PHP ColdFusion ASP JSP

Supported Database

MySQL, mSQL, ODBC, Oracle, Informix, Sybase, etc.

ODBC, OLE DB, DB2, Oracle, Informix, Sybase, etc.

any ODBCcompliant database

any ODBC- and JDBC-compliant database

Portability

Fair (DB portability)

Good

Fair

Good

Scalability

Fair

Good

Good

Good

Component Support

COM, Java classes (version 4)

COM, CORBA, JavaBeans

COM components

JavaBeans, Enterprise JavaBeans

Learning curve

High (C, Perl)

Low

Medium (VBScript, JScript)

High (Java)

COMPONENTS OF JSP:

JSP STANDARD ACTIONS:

Standard actions are specific tags that affect the runtime behavior of the JSP and affect the response sent back to the client. EXAMPLE: <jsp:forward page="relativeURL"/> Here, Forwarding a request means that even the request object is forwarded to the target page.

JSP SCRIPTING Elements:


Scripting elements are used to include scripting code (Java code) within the JSP. They allow to declare variables and methods, include arbitrary scripting code and evaluate an expression. The three types of scripting element are: <%= expression %> The expression is evaluated and the result is inserted into the HTML page <% code %> The code is inserted into the servlet's service method If code contains declarations, they become local variables of the service method This construction is called a scriptlet <%! declarations %> The declarations are inserted into the servlet class, not into a method Hence, declarations made here become instance variables

JSP DIRECTIVE ELEMENTS:

JSP Directives serve as messages to the JSP container from the JSP. They are used to set global values such as class declaration, methods to be implemented, output content type, etc. All directives have scope of the entire JSP file. A directive is an option setting that affects how the servlet is constructed from a JSP page. The three directives are Page, Include and Taglib. The Page directive can contain the list of all imported packages. Example: <%@ page attribute=value %> Here, attribute can be import, contentType, errorPage, etc. The Include directive is used to physically include the contents of another file. Example: <%@ include file= relativeURL %>

Standard Action Tags:


<jsp:useBean> imports a JavaBeans component. <jsp:getProperty> gets a property value and adds it to the response. <jsp:setProperty> sets a property value. <jsp:param> adds a parameter to a request. <jsp:plugin> generates the OBJECT or EMBED tag for an applet. <jsp:forward page=Account.jsp> Terminates execution of current page Forwards the call to a servlet or JSP page for service. Transfers control to the forwardee <jsp:include page=Account.jsp> Executes the specified page Includes the response from a servlet or JSP page that is called for the request. Then returns control to the current page

JSP Lifecycle Methods:

The javax.servlet.jsp.JspPage interface contains two methods: 1. public void jspInit() - This method is invoked when the JSP is initialized and the page authors are free to provide initialization of the JSP by implementing this method in their JSPs. 2. public void jspDestroy() - This method is invoked when the JSP is about to be destroyed by the container. Similar to above, page authors can provide their own implementation. The javax.servlet.jsp.HttpJspPage interface contains one method: public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException This method generated by the JSP container is invoked, every time a request comes to the JSP. The request is processed and the JSP generates appropriate response. This response is taken by the container and passed back to the client.

Implicit Objects:
JSP Implicit Objects are certain pre-defined variables that can be included in JSP expression and scriptlets and are only visible within the system generated service method. Of the implicit objects, there are three most used implicit objects that are used to handle the current session , the incoming request, and the outgoing response. These three are session, request, and response respectively.

JSP CUSTOM TAGS:


o

Custom tags are user-defined , re-usable tags that is used to perform repetitive tasks in a JSP page. It can be empty, can contain attributes, body or nest with another tags. A Tag Handler is a java class that implements the functionality of a custom tag. A Tag Library Descriptor (TLD) file defines the functionality of a custom tag. A Taglib element is the root element of a TLD file and it imports a custom tag into current JSP page. Tag Files: nature and purpose Solve difficulty of reusing text/HTML within a tag. And makes it much easier to write simple tags, since you can do so in JSP instead of Java. Stand-alone file with <%@ tag %> directive instead of traditional <%@ page %> directive.

ADVANTAGES and CONSEQUENCES:

Advantages Efficient Convenient Server engines typically have built-in infrastructures to handle parsing/decoding HTML form data, reading/setting HTTP headers, session tracking Powerful Java behind the scene Portable Write once, run anywhere Inexpensive Various free or inexpensive engines available

Consequences Need for a Server Engine Another engine to maintain Un-acceptably by the client machine Virtual hosting of unrelated sites on a single machine can cause problems (choose your server engine wisely) High Learning Curve Steeper learning curve than Cold Fusion

Whats irritating about JSP?


The tag-extension protocol is too complicated Also, tags dont support certain kinds of code reuse. Also, Scriptlets complicate abstraction and code reuse.

OVERVIEW OF JSP:

JSP provide excellent server side scripting support for creating database driven dynamic web applications. The Java is enclosed in special tags, such as <% ... %>. JSP files must have the extension .jsp and it is secure, fast and independent of sever platforms. Elements that are processed on server are Directives, Scripting and Actions. JSP lifecycle consists of two phase: TRANSLATION phase and REQUEST PROCESSING phase. In Translation phase, JSP Code is translated into java servlets which is then compiled. It is done by the web container itself. In Request processing phase, a response to the clients request is been sent. JSP Life cycle methods are jspInit(), jspService() and jspDestroy().

You might also like