Download No Slide Title

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS6320 – JSP
L. Grewe
1
Java Server Pages








Servlets require you to write out entire page
delivered with print statements
JSP embedded in static html content
File extension .jsp
Not compiled
Deploy as part of webapp but, location of jsp files
variable (see deployment and your server
information)
Language of tags, can use Java
JSP run as servlets when envoked
Invokes special _jspService() method …do not
have the doGet (do*) methods.
2
How it works.
JSP engine on server receives request for a
.jsp page it:
1.
2.
3.
4.
Reads in the page, and transforms the contents to a
Servlet
Even the static HTML is converted to print
statements, printing to the output stream associated
with the JSP’s _jspService() method.
This translation is done the first time the page is
requested.
Then the server runs the resulting created Servlet
and returns the results to the client.
3
Elements of a Java Server Page

Expressions: <%= %>
• expression is evaluated and inserted into Servlet’s
output.

Scriptlets: <% %>
• This code is directly inserted into the produced
Servlet’s _jspService() method.
• This method is automatically called by the Servlet’s
service() method.

Comments: <%-- --%>
• User readable comments, not parsed by the JSP Compiler
• Difference between this an HTML comment is this is inserted
into the Servlet being produced by evaluating the .jsp file.4
Elements of a Java Server Page

Directives: <%@ %>
• Provide global information to the page
• Import statements
• Scripting language

Declarations: <%! %>
• For page-wide variable and method declaration
• The code is inserted into the body of the produced
Servlet, outside of any existing method (like
_jspService())
5
2 forms of tags
<%= your_expression %>
OR the XML compliant version
<jsp:expression>
your_expression
</jsp:expression>
6
JSP Expression Example
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<p> The time is now <%= new Date() %>.
</body>
</html>
7
JSP Scriptlets



JSP scriptlets are defined as block of Java
code embedded between a pair of
tags, <% and %>.
Example:
<%
java.util.Date d = new java.util.Date();
out.println(d);
%>
8
JSP Directives

General syntax:
 <%@ directive {attribute = "value"} %>

Possible values for directives are:
• Page - Information for the page

Include - Specifies the files whose
contents are to be included in the output
• e.g., <%@ include file="header.html" %>

Taglib
• The URI for a library of custom tags that may
be used in the page
9
Directive Examples
<%@ page language="java“ import =“java.util.*” %>
• Imports classes
<%@ include file="relative-URL" %>
• Includes files
<jsp:forward page="next.jsp">
Zero or more <jsp:param name="x" value="y"> tags
</jsp:forward>
• Forwards with parameters to another resource
See JSP documentation for more
10
Directive Examples (Contd.)
<%@ page session = "true | false" %>
• true indicates that session data is available to the
page
• By default, this is set to true
<%@ page buffer = "none | 16kb | sizekb" %>
• Determines the size of the output stream buffer
• Defaults to 8kb
• Use with autoFlush
<%@ page autoFlush = "true | false" %>
• When set to true, flushes the output buffer when it is
full, rather than raising an exception
11
JSP Declarations

Class and instance variables (of the
generated servlet class) may be specified
using the JSP Declaration tag:
<%! String name = “Web Applications";
int index = 10;
int count = 0; %>

Methods may also be specified:
<%!
private int getNextIndex()
{return index ++;} %>
12
JSP PreDefined Objects


When writing scriptlets and expressions,
the following objects (called implicit
objects) are available by default:
request
javax.servlet.http.HttpServletRequest

response
javax.servlet.http.HttpServletResponse




out javax.servlet.jsp.JspWriter
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception java.lang.Throwable
13
JSP Session Example
<html><head><title>
Visitor Count -- JSP Session </title>
</head> <body bgcolor="#FFFFFF">
<font face="Helvetica"> <h2>
Visitor Count </h2>
<p>
This JSP page demonstrates session management by
incrementing a counter each time a user accesses a
page.
<p>
<%!
private int totalHits = 0;
%>
14
JSP Session Example
<% session = request.getSession(true);
Integer ival =
(Integer)session.getValue("jspsession.counter");
if (ival == null)
{ ival = new Integer(1);}
else
{ival = new Integer(ival.intValue() + 1);}
session.putValue("jspsession.counter", ival);
%>
15
JSP Session Example
<center>
<font size=+2>
You have hit this page <%= ival %>
time<%= (ival.intValue() == 1) ? "" : "s" %>,
out of a total of <%= ++totalHits %> page
hit<%= (totalHits == 1) ? "" : "s" %>!
</font>
</center>
<p>
</font>
</body>
</html>
16
More…..

SEE the course website, and the
current JSP api for more details.
17