JavaServer Pages

Download Report

Transcript JavaServer Pages

JSP Fundamentals
Elements of a JSP
Using Beans with JSP
Integrating Servlets and JSP
Overview
JavaServerPages are similar to HTML files, but provide the ability to display
dynamic content with Web pages. A JavaServer Page (JSP) is an HTML web page
that contains embedded Java code.
sample.jsp
HTML
The HTML displays the static portion of the
page (text and graphics).
The Java code is used to generate dynamic
content for the page (JDBC, RMI).
Java Code
HTML
HTML
A JSP separates user interfaces from content generation. This allows the
separation of web page design from component development.
Overview
When a JSP is requested by a user, the web server executes the Java code to
create the dynamic portion of the page.
sample.jsp
HTML
Java Code
request
request
HTML
HTML
HTML
Web Browser
HTML
response
|||||
Web Server
response
HTML
HTML
Overview
sample.jsp
<html>
<body>
The date is <%= new java.util.Date( ) %>
</body>
</html>
Java code to create a Date object
and include it in the page
How is the JSP file processed?
• Client sends HttpRequest using URL of JSP
• JSP returns an HTML page
sample.jsp
request
request
Web Browser
response
|||||
Web Server
response
<%@ import=“java.util.* %>
<html>
<body>
The date is <%= new Date( ) %>
</body>
</html>
<html>
<body>
The date is Tue Jun 06 16:03:37 EDT 2000
</body>
</html>
How is the JSP file processed?
Web Server
Web Browser
JSP File
JSP Parser
Java Source
Web Page
JSP Servlet
Java Compiler
request
(HTML)
response
Elements of a JSP
JSPs are composed of standard HTML tags and JSP
tags. The JSP tags are categorized as follows:
Expressions – code fragment who results are placed into the page
Scriplets – blocks of Java code embedded in the page
Declarations – page-wide variable and method declarations
Directives – global information about the page
JSP -- Expressions
• Expressions
are variables or constants that are inserted into the
data returned by the web server.
• Syntax
<%= expression %>
• The expression is evaluated, converted to a string, and the
result is inserted into the page
• Examples
<%= new Date( ) %>
<%= client.getFirstName( ) %>
JSP -- Expressions
• JSP defines several predefined variables which may be used in
expressions and scriplets. Here are a few:
request – the HttpServletRequest
response – the HttpServletResponse
session – the HttpSession associated with the session
out – the PrintWriter used to send output to the client
~~ ~~ ~~
request
request
~~ ~~~
out
~~~~
response
response
|||||
JSP
JSP -- Scriplets
• Scriptlets are blocks of Java code embedded within the JSP
page
• Syntax
<% java code %>
• Example
<%
int i;
for(i = 0; i < 10; i ++ )
out.println( i );
%>
JSP -- Scriplets
Example
Scriplets are often used to conditionally construct parts of the HTML
page.
scriptlet2.jsp
<html>
<body>
<%
String answer = request.getParameter("ans");
if( answer.equals(“texas“) ) {
%>
<p>Your answer is correct!</p>
<% } else { %>
<p>Sorry, your answer is incorrect.</p>
<% } %>
</body>
</html>
JSP -- Scriplets
This form will send the data in
the textfield to scriptlet2.jsp.
question.htm
<html>
<body>
<form method="POST" action="scriptlet2.jsp">
<p>Where is San Antonio located? <input type="text" name="ans" size="20"> </p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>
scriptlet2.jsp
<html>
<body>
<%
String answer = request.getParameter("ans");
request
ans = texas
if( answer.equals(“texas“) ) {
%>
<p>Your answer is correct!</p>
<% } else { %>
<p>Sorry, your answer is incorrect.</p>
<% } %>
</body>
</html>
<html>
<body>
response
<p>Your answer is correct!</p>
</body>
</html>
scriptlet2.jsp
<html>
<body>
<%
String answer = request.getParameter("ans");
request
if( answer.equals(“texas“) ) {
ans = nevada
%>
<p>Your answer is correct!</p>
<% } else { %>
<p>Sorry, your answer is incorrect.</p>
<% } %>
</body>
</html>
<html>
<body>
response
<p>Sorry, your answer is incorrect.</p>
</body>
</html>
JSP -- Declarations
• A declaration block defines Java variables and methods for
subsequent use in expressions or scriptlets. The declared variable or
method may be used throughout the page.
• Syntax
<%! declaration %>
• Example
<%!
int cout = 0;
private void increment ( ) {
count++;
}
%>
JSP -- Declarations
Example
Here is a JSP that
calculates the factorial
of a number retrieved
from a form.
factcalc.jsp
<html>
<body>
<%!
public int fact( int x ) {
if( x == 1 ) return 1;
else return x * fact( x - 1 );
}
%>
<p align="center">Factorial Calculator</p>
<%
String numberString = request.getParameter("number");
int number = Integer.parseInt(numberString);
out.println(number + " ! = " + fact(number));
%>
<p><a href="factorial.htm">Try another one</a></p>
</body>
</html>
declaration
JSP -- Declarations
This form will send the data in
the textfield to factcal.jsp.
factorial.htm
<html>
<body>
<p>Factorial Calculator</p>
<form method="POST" action="factcalc.jsp">
<p>Enter a number : <input type="text" name="number" size="13"></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>
factcalc.jsp
<html>
<body>
<%!
public int fact( int x ) {
if( x == 1 ) return 1;
request
else return x * fact( x - 1 );
number = 4
}
%>
<p align="center">Factorial Calculator</p>
<%
String numberString = request.getParameter("number");
int number = Integer.parseInt(numberString);
out.println(number + " ! = " + fact(number));
%>
<p><a href="factorial.htm">Try another one</a></p>
</body>
</html>
<html>
response
<body>
<p align="center">Factorial Calculator</p>
4 ! = 24
<p><a href="factorial.htm">Try another one</a></p>
</body>
</html>
JSP -- Declarations
Example
counter.jsp
<%! int count = 0; %>
<html>
<body>
<% count = count + 1; %>
<p> This page has been accessed <%= count %> times.</p>
</body>
</html>
Here is a JSP that
counts the number of
times a page is
accessed.
JSP -- Directives
A directive is a global definition that dictates how the code is a JSP
file will be processed.
Directives are used to set page-level instructions, insert data from
external files, and specify custom tag libraries
Directives are placed at the beginning of the JSP file???.
Syntax:
<@ directive directive_attr_name = value @>
There are three types of directives : page, include, taglib
JSP -- Directives
page directive
The page directive defines page dependent attributes:
<%@ page language = “java” %>
-- Identifies the scripting language used in the JSP file
<%@ page errorpage = “/app/info.html” @>
-- Returns the defined page if an error occurs in the JSP
<%@ page import = “java.util.*, java.text.*”
-- Defines packages that that will be used by the embedded scripts
Other page directives are: extends, session, buffer, autoFlush, isThreadSafe,
info, isErrorPage, contentType
JSP -- Directives
include directive
The include directive allows text to be inserted into the page:
<@ include = “copyright.html” @>
-- Inserts the contents of copyright.html into the JSP
Scripting language is java
sample2.jsp
<%@ page language=“java” %>
<%@ page import = java.util.*;” @>
<html>
<body>
The date is <%= Date( ) %>
<%@ include file = “copyright.htm”>
</body>
</html>
Import the package java.util
Insert the file copyright.htm
copyright.htm
<html>
<body>
<p>Copyright 2000 Allen</p>
</body>
</html
>