Java Server Pages (JSP)
An Overview
Presented by: Rohit Singh Sengar
2
Scope
Need of dynamic pages
History of dynamic pages
Servlets & its example
JSP Overview
JSP: Architecture & Life cycle
JSP: Tags & Example
JavaBeans
Comparison of JSP with other server-side scripting
technologies.
Pros and Cons of JSP
References
3
Need of Dynamic Websites
User Data: Web pages is based upon the user
entered data.
Web pages is derived from data that changes
frequently
Web pages interacts with databases
2 types of code is needed
1)Data representation
2)Data generation
4
History: Common Gateway
Interface (CGI)
Earlier dynamic web pages were developed
with Common Gateway Interface (CGI).
Web Applications eventually outgrew CGI
because of the following:
Datasets became large
Increased traffic on sites
Trouble maintaining state and session information
Performance bottlenecks
Can involve proprietary APIs
5
Servlets
It is regular Java code. There are new APIs, but no new syntax.
It has unfamiliar import statements. The servlet and JSP APIs
are separate specification (and are also part of the Java 2
Platform, Enterprise Edition—J2EE).
It extends a standard class (HttpServlet). Servlets provide a
rich infrastructure for dealing with HTTP.
It overrides the doGet method. Servlets have different methods
to respond to different types of HTTP commands.
Servlet Life Cycle: It consist of followings
The Init method
The Service method
The Destroy method
6
Servlet Code: Example (for
displaying Hello World)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello World</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello World</H1>\n" +
"</BODY></HTML>");
}
}
7
JSP Overview
With a foundation in Java, JSP
Threads incoming requests
Supports custom tags.
Interacts with a wide array of databases
Serves dynamic content in a persistent and efficient manner
Initially the server automatically builds, compiles, and starts the servlet,
from then on the servlets are stored in the web server’s memory in Java
byte code
Extends the same portability, large class library, object-oriented code
methodology, and robust security measures that have made Java a top
choice
Does not require a Java programmer
Better separation of Logic and presentation.
8
Architecture
9
JSP Life Cycle
10
JSP - Tags
Tag Name Purpose
1. Declaration tag <%! %> To declare variables & methods
2. Expression tag <%= %> To embed any Java expressions & display any value
3. Directive tag <%@ %> To set condition that applies to the whole JS Page.
4. Scriptlet tag <% %> To write any valid block of Java code
5. Action tag <jsp: > Enable the use of server side Javabeans, transfer control
between pages, browser independent support for applets
6. Comment tag <%-- --%> Tell JSP engine to ignore the code
11
JSP - Example
# Simple Java server
page showing use of
common tags.
# Java is very case
sensitive.
12
JavaBeans
A JavaBean is a Java Class file that comes with
specifications.
Defines how to create an Object, retrieve and set properties
of the Object
It is used to minimize the java code in JSP.
They are is used to communicate with Database & contain
various logic.
Syntax: <jsp:useBean id=Bean name class=bean class
scope=page/request/session/application/>
Syntax for setting value: <jsp:setProperty name=bean
name property=someProperty value=someValue/>
13
Comparison of JSP
PHP Cold Fusion ASP JSP
$0 (Express version)- Free with Windows
Cost Free Free
$5000 (Enterprise NT, cost for 3rd-
version) party software
PHP CFML VBScript, JScript Java
Language In Page
Windows 9x, NT, UNIX, Microsoft
Unix (Linux), Windows, Windows NT, Solaris,
OS Platform other platforms Windows, Mac OS,
MacOS, OS/2 Linux
requires third-party Linux
ASP porting
products
iPlanet/Netscape
Enterprise Server IIS, Netscape IIS, Personal Web Any Web server,
Supported (NSAPI), MS Internet Enterprise Server , Server (PWS), other including Apache,
Web server
Information Server (IIS), Apache, Website servers with third- Netscape and IIS
Apache, Zeus, fhttpd, etc. Server (WSAPI), CGI party products
(ver 4)
14
Comparison of JSP (Cont’d) …
PHP ColdFusion ASP JSP
MySQL, mSQL, ODBC, any ODBC- and
Supported ODBC, OLE DB, any ODBC-
Oracle, Informix, Sybase, JDBC-compliant
Database DB2, Oracle, compliant database
etc. database
Informix, Sybase, etc.
Portability Fair (DB portability) Good Fair Good
Scalability Fair Good Good Good
JavaBeans,
Component COM, Java classes COM, CORBA,
COM components Enterprise
Support (version 4) JavaBeans
JavaBeans
Medium (VBScript,
Learning curve High (C, Perl) Low High (Java)
JScript)
15
Pros and Cons
Pros
Efficient
Cons
Secure Need for a Server Engine
Another engine to maintain
Convenient
Server engines typically have built- Un-acceptably by the client
in infrastructures to handle machine
parsing/decoding HTML form data, Virtual hosting of unrelated
reading/setting HTTP headers,
sites on a single machine can
session tracking
cause problems (choose your
Powerful
server engine wisely)
Java behind the scene
High Learning Curve
Portable
Steeper learning curve than
Write once, run anywhere
Cold Fusion
Inexpensive
Various free or inexpensive engines
available
16
JSP- A part of J2EE
17
References
www.java.sun.com/products/jsp/
www.java.sun.com/developer/technicalArticles/Programming/jsp/
www.en.wikipedia.org/wiki/JavaServerPages
www.visualbuilder.com/jsp/tutorial
www.cetus-links.org/oo_javaserver_pages.html
www.jspin.com
www.courses.coreservlets.com
www.onjava.com/topics/java/JSP_Servlets
18
Thank You
Any Questions?