Java Server Pages
Introduction • • • • •
What is JSP? My first JSP JSP Fundamentals JSP Directives and APIs JSP, Java Beans and Tags
What is JSP •
A simplified way to generate dynamic web content
Simpler then Servlet Editing tool friendly Can be used to separate the coding of presentation and business logic
cont… •
JSP is based on Java and Servlet technologies
An HTML page with embedded tags and Java Code At runtime the JSP page is translated into a Java Servlet The runtime is usually encapsulated in a special JSP-Runtime Servlet
JSP – A standard •
JSP is a Java Standard:
Defined by a group of companies led by JavaSoft Current version is 1.2, but previous versions are also in use (mainly 1.1) Ongoing effort to improve JSP
Why JSP? •
Because it is needed!
•
In fact, there are several JSP like technologies both in the Java (as well as non-Java) world
Servlet that generate HTML output hide the HTML inside Java
Makes it hard for an HTML expert (!= Java expert) to fix stuff Lock the content in Java Makes look and feel changes to the site problematic
cont… •
The ability to script with Java and JavaBeans makes generating dynamic content simpler
The competitors (ASP) provide an extremely !!! simple (and appealing) method for generating dynamic content
The JSP Syntax •
Inline Java code delimited by <% and %>
• •
Also printing of expressions as text by using <%= %>
Special tags to declare class wide variables and methods Special tags to use with JavaBeans
cont… • •
Special tags to expose JSP services JSP directives to specify
Interfaces implemented by the Servlet, classes it extends, packages to import etc
My first JSP
Hello World
Helloworld.jsp – Code <%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII" %> <TITLE>helloworld.jsp
<% out.print("This is a sample JSP file"); %>
Helloworld.jsp – Output
Helloworld.jsp – Observations •
At first, the JSP file looks like a regular HTML page
•
There is a new <%@ page … %> “Tag” that has nothing to do with HTML
•
HTML tags, html, body etc
This is a JSP directive
We have some Java code enclosed between <% and %>
This is a JSP scriptlet
JSP Fundamentals
JSP Fundamentals •
Adding Java to a plain HTML page.
•
Use JSP implicit variables.
•
To obtain information. To write data back to the user.
Defining class wide variables and methods.
•
Scriptlets
For initialization.
Commenting your scripts.
JSP Scriptlets •
Scriptlets let the developer place inline Java code inside the HTML
•
General syntax: <% Java code %> • <% i++; %>
Expression placement lets the developer place a Java expression (converted to a String) inline with the HTML
General syntax <%= expression %> • <%= new Date() %>
Conditional HTML •
Scriptlets can be used to implement conditional HTML <% if(variable ) { %>
variable is true
<% } else { %>
variable is false
<% } %>
Looping •
Scriptlets can be used to implement loops inside the HTML
<% for(int k = 0 ; k < 10 ; k++ ) { %>
variable’s value is <%= k %>
<% } %>
JSP Implicit Variables •
JSP has several implicit variables that can be used freely within the JSP page
Available without prior declaration and definition Provides all the services and APIs that are available for the servlet developer
cont… •
Some of the implicit variables are:
request – The HttpServletRequest object as arrived to the service method response – The HttpServletResponse object as arrived to the service method out – A JspWriter connected to the ServletOutputStream of response session – The HttpSession object associated with the current user-session
Class Wide Declarations •
Class wide can be used to define instance methods and variables in the page
These methods and variables end up defined in the class created from the JSP file
<%! int varname = 1; void jspInit(){ System.out.println("inside init"); } %>
cont… • •
Class wide declarations are enclosed within a <%! %> pair Example usage can be creating initialization code
Comments in JSP • •
There are two types of comments in JSP HTML comments that should arrive to the client's browser
•
These comments can contain JSP code and Scriptlets that are executed •
Comments to the JSP page itself
These comments and their content are not interpreted and will not arrive to the client • <%-- anything but a closing --%> ... --%>
The include Directive • •
A tool for inclusion of content inside the JSP page at translation time General syntax:
•
<%@ include file="relativeURLspec" %> File points to a URI that identifies the resource to be included into the JSP page
Very useful for sites with specific headers footers etc
Snoop.jsp •
Uses scriptlets to present the request headers and form parameters sent to the page
Employs scriptlets, implicit variables and comments
Snoop.jsp – Code <%@ page language="java" contentType="text/html” import="java.util.*" %> <TITLE>JSP snoop page
JSP Snoop page
…
Snoop.jsp – Code <% Enumeration e = request.getHeaderNames(); if(e != null && e.hasMoreElements()) { %>
Request headers
Header: | Value: |
<% while(e.hasMoreElements()) { String k = (String) e.nextElement(); %> <%= k %> | <%= request.getHeader(k) %> |
<% } %>
<% } %>
Snoop.jsp – Code <% e = request.getParameterNames(); if(e != null && e.hasMoreElements()) { %>
Request parameters
Parameter: | Value: |
<% while(e.hasMoreElements()) { String k = (String) e.nextElement(); String val = request.getParameter(k); %> <%= k %> | <%= val %> |
<% } %>
<% } %>
Snoop.jsp – Output
Directives and APIs
JSP Directives and APIs • •
JSP 1.0 provides directives and APIs to allow enhanced control over the page. Directives provides:
Declarative page control (caching for example). Defining Java related page attributes.
cont… •
The APIs provides:
Programmatic page control. Access to information that was supplied by using directives
JSP Directives • •
Directives begin with <%@ and ends with %> Directives have a type, attributes and values
•
<%@ type attribute="value" %>
For now the types are
page include • <%@ page attribute="value">
JSP Page Directives •
Page directives provides fine-grained control over the JSP Page
Buffering. Session Usage. Importing a Java class Programming language used in the page Required locale
<%@ page language="java" contentType="text/html; charset=WINDOWS-1255" pageEncoding="WINDOWS-1255" import="java.util.*" %>
JSP, Java Beans and Tags
JSP and JavaBeans •
JavaBeans is the component model used for the Java language:
•
Portable. Platform independent. Written in Java.
Beans are:
Java classes. Reusable software components. You can combine several beans to create a bigger whole.
cont... •
•
One of the main ideas in JSP is that developers can use JavaBeans to separate the HTML view from the Java implementation code. Developers integrate the JavaBeans into the JSP page using special tags and Scriptlets.
Bean Tags in JSP •
JSP introduces new tags to handle beans:
Attaching to a certain bean. • jsp:useBean Initializing a bean. Setting a bean attribute • jsp:setProperty Getting a bean attribue. • jsp:getProperty
Beanmyname.jsp •
Print a name as submitted by the user
•
Uses JavaBeans and JSP JavaBeans tags:
To parse the parameters sent Print the parameters
Beanmyname.jsp – The code <%@ page language="java" contentType="text/html” import="NameBean" %> <META http-equiv="Content-Type" content="text/html; charset=WINDOWS-1255"> <TITLE>beanmyname.jsp <jsp:useBean id="myusername" scope="page" class="NameBean"> <jsp:setProperty name="myusername" property="*" />
Beanmyname.jsp – The code
Print my name using JavaBeans
Your full name Is: <jsp:getProperty name="myusername" property="name" /> <jsp:getProperty name="myusername" property="family" />
Send Your Name
NameBean – The code public class NameBean implements java.io.Serializable { String nameProperty = null; String fnameProperty = null; public void setName(String name) { nameProperty = name; } public String getName() { return nameProperty; } public void setFamily(String name) { fnameProperty = name; } public String getFamily() { return fnameProperty; } }
Beanmyname.jsp – Output
Custom Tags
Objectives • • • •
Introduction Why custom tags? Basics of Tag Libraries Simple Custom Tag
Introduction • • •
•
A tag library is a collection of custom tags Tags within a tag library are written in Java A tag represents some functionality which is written in a form of a Java method as a part of a Java class A simple way to think of a custom tag is a User Defined Scriptlet
Cont… •
Custom tags are to replace embedded Java code within a JSP page
Why Custom Tags? • • •
Custom tags provide methods to cleanly separate logic from presentation Ease of use Portability
Thank You
Zubair Shaikh
[email protected] Presentation Version 1.2