Anda di halaman 1dari 32

Java Server Pages

- An Introduction

Sowmya Malagikar
26th October 2006

MindTree Consulting Pvt Ltd Proprietary & Confidential

Objectives
To be able to define JSP

To be able to discuss the use of JSP in

enterprise architectures To demonstrate the ability to write, debug and deploy JSPs

Page 1

MindTree Consulting Pvt Ltd Proprietary & Confidential

Agenda
What is JSP ?

Relatives of JSP technology


JSP Elements

Page 2

MindTree Consulting Pvt Ltd Proprietary & Confidential

What is JSP ?
Definition : * Mixing Static HTML With Dynamically Generated Content using Java technology * JSP is a dynamic scripting capability for web pages that allows java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix traditionally ends with .jsp to indicate to the web server that the file is a JSP file.

Page 3

MindTree Consulting Pvt Ltd Proprietary & Confidential

What is JSP ? contd..


A standard component of J2EE A mixture of HTML and Java Effective way for generating dynamic HTML Server side processing Separates the graphical design from the dynamic content Can be customized by using Tags http://java.sun.com/products/jsp

Page 4

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Advantages
Separation of static from dynamic content

Write Once run anywhere


Dynamic content can be served in a variety

of formats

Page 5

MindTree Consulting Pvt Ltd Proprietary & Confidential

Separation of static from dynamic content


Not a new idea Graphical Design and System Design are two separate and distinct specialities:
Different

languages (HTML vs. Java), Different goals, and Different Training.

Should be separated for optimal project management. JSP does this by allowing special Java tags in HTML
Page 6
MindTree Consulting Pvt Ltd Proprietary & Confidential

A Simple Example
package example.servlets;

import javax.servlet.*; import java.io.*;


public class MyServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("<html>"); out.println("<title> Hello World Example </title>"); out.println("<body><B>Hello World ! </B></body>"); out.println("</html>"); } }
Page 7
MindTree Consulting Pvt Ltd Proprietary & Confidential

A Simple Example contd..


<HTML> <HEAD> <TITLE>Hello World Example</TITLE> </HEAD> <BODY> <B><% out.println("Hello World !"); %></B> </BODY> </HTML>

Page 8

MindTree Consulting Pvt Ltd Proprietary & Confidential

Server Side Processing


JSP-enabled server
picks

up .jsp page parses it converts it to runnable form runs it

Converts page to a Java servlet (JspPage),

with your code inside the _jspService() method


compiles

it runs servlet program


Page 9
MindTree Consulting Pvt Ltd Proprietary & Confidential

Server side processing contd..


A JSP page is executed in a JSP container, generally installed in a Web server
Think

of a JSP container as a JVM with suitable software installed

The underlying semantic model is that of a servlet A typical JSP container will translate the JSP page to a Java servlet

By default, translation and compilation of a JSP page is likely to occur the first time the page is accessed
Page 10
MindTree Consulting Pvt Ltd Proprietary & Confidential

Server Side Processing contd..

Page 11

MindTree Consulting Pvt Ltd Proprietary & Confidential

Server Side Processing contd..

Page 12

MindTree Consulting Pvt Ltd Proprietary & Confidential

Life cycle of a JSP page


Initialize - jspInit()

Render service - jspService()


Complete - jspDestroy()

Page 13

MindTree Consulting Pvt Ltd Proprietary & Confidential

Relatives of JSP Technology contd..


JSP versus Servlets Differences Servlets: HTML in Java code HTML code inaccessible to Graphics Designer Everything is accessible to Programmer JSP: Java Code Scriptlets in HTML HTML code very accessible to Graphics Designer Java code accessible to Programmer
Page 14
MindTree Consulting Pvt Ltd Proprietary & Confidential

Relatives of JSP technology contd..


JSP versus Servelets
But JSP pages are converted to Servlets? Arent they the same?
Similarities
Provide

identical results to the end user JSP is an additional module to the Servlet Engine

Page 15

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Elements
Scriptlets of the form <% code %> that are inserted into the servlet's service method, and Directives g of the form <%@ ...%> gives JSP engine information for the page Expressions of the form <%= expression %> that are evaluated and inserted into the output Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods

Page 16

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Elements example

Page 17

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Elements contd..


JSP expression A JSP expression is used to insert Java values directly into the output It has the following form:
<%= Java Expression %>

The Java expression is


evaluated, converted to a string, and inserted into the page

This evaluation is performed at runtime (when the

page is requested)

Page 18

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Elements contd..


Pre-defined variables The following pre-defined variables can be used: request, the HttpServletRequest response, the HttpServletResponse session, the HttpSession associated with the request (if any) out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client

Page 19

MindTree Consulting Pvt Ltd Proprietary & Confidential

Implicit Objects - example


<HTML> <HEAD> <TITLE>JSP Expressions</TITLE> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time: <%= new java.util.Date() %> <LI>Your hostname: <%= request.getRemoteHost() %> <LI>Your session ID: <%= session.getId() %> <LI>The <CODE>testParam</CODE> form parameter: <%= request.getParameter("testParam") %> </UL> </BODY> </HTML>
MindTree Consulting Pvt Ltd Proprietary & Confidential

Page 20

Implicit Objects contd..


There are eight automatically defined variables, sometimes called implicit objects The available variables are request, response, out, session, application, config, pageContext, and page

Page 21

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP Elements - Directives


There are three main types of directive: page, which lets you do things like
import classes customize the servlet superclass

include, which lets you

insert a file into the servlet class at the time the JSP file is translated into a servlet
indicates a library of custom tags that the page can include

taglib directive

Page 22

MindTree Consulting Pvt Ltd Proprietary & Confidential

Directives - examples
The page directive lets you define the

following attributes:
import="package.class

<%@ page import="java.util.*" %> contentType="MIME-Type" <%@ page contentType="text/plain" %>

(it is the same as


<% response.setContentType "text/plain");%> )
MindTree Consulting Pvt Ltd Proprietary & Confidential

Page 23

More Directives
isThreadSafe=true|false Normal servlet processing or implementing SingleThreadModel session=true|false Allowing/disallowing sessions buffer=sizekb|none specifies the buffer size for the JspWriter out autoflush=true|false Flush buffer when full or throws an exception when buffer isfull
Page 24

MindTree Consulting Pvt Ltd Proprietary & Confidential

More Directives contd..


extends=package.class

info=message

message for the getServletInfo method errorPage=url Define a JSP page that handles uncaught exceptions isErrorPage=true|false language=java

Page 25

MindTree Consulting Pvt Ltd Proprietary & Confidential

More Directives contd..

This directive lets you include files at

the time the JSP page is translated into a servlet The directive looks like this: <%@ include file="relative url" %>

Page 26

MindTree Consulting Pvt Ltd Proprietary & Confidential

Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet engine You can
dynamically

insert a file,

reuse

JavaBeans components,
the user to another page, or HTML for the Java plugin

forward

generate

Page 27

MindTree Consulting Pvt Ltd Proprietary & Confidential

Available Actions
Available actions include:
jsp:include -

Include a file at the time the page is

requested jsp:useBean - Find or instantiate a JavaBean jsp:setProperty - Set the property of a JavaBean jsp:getProperty - Insert the property of a JavaBean into the output jsp:forward - Forward the requester to a new page jsp:plugin - Generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin

Page 28

MindTree Consulting Pvt Ltd Proprietary & Confidential

For Further Learning


Using JavaBean Components

Forwarding Requests to JSP/Servlet

Page 29

MindTree Consulting Pvt Ltd Proprietary & Confidential

JSP resources
http://java.sun.com/products/jsp/

http://www.jguru.com/faq/index.jsp
http://archives.java.sun.com/archives/jspinterest.html

Page 30

MindTree Consulting Pvt Ltd Proprietary & Confidential

Imagination
Page 31

Action

Joy
MindTree Consulting Pvt Ltd Proprietary & Confidential