May 14th, 2014
Lập trình ứng dụng
Mạng
Subtitle or catch phrase for the presentation
May 14th, 2014
Nhóm 2
Chaper 11
Controlling the Structure of Generated Servlets: The JSP page Directive
Agenda
• Understanding the purpose of the
page directive.
• Designating which classes are
imported.
• Specifying the MIME type of the
page.
• Generating Excel spreadsheets.
• Participating in sessions.
• Setting the size and behavior of the
output buffer.
• Designating pages to handle JSP
errors.
• Controlling threading behavior.
1. The Page Directive
Points
• In JSP, there are three main types of directives:
Page
Include
Taglib
• The third
page directive
second
directive,
directive,
lets
taglib,
you control
include,
defines
custom
letsthe
youmarkup
structure
insert a
of the
file
tags;
into
it’s
servlet
the
discussed
JSP
by page
at at
importing
the
great
time
length
the
classes,
JSP
in Volume
file is 2
customizing
translated
of
this book
into
thea servlet
superclass, setting the
content type, and the like.
1. The Page Directive
Purpose
• So the purpose of the page directive is effects the overall structure of the
servlet (or Give high-level information about the servlet ) that results from
the JSP Page.
2. Designating which classes are imported.
The import Attribute
• Format.
• – <%@ page import="package.class" %>
• – <%@ page import="package.class1,...,package.classN" %>
• Purpose.
• Generate import statements at top of servlet definition
•
Notes.
• Although the JSP pages go in the normal HTML directories of the
server, the classes you write that are used by JSP pages must be placed in the special Java-code dirs.
• Java-code directories – E.g.:
…/WEB-INF/classes or
…/WEB-INF/classes/directoryMatchingPackage
• Always put your utility classes in packages.
2.TheDesignating
which
classes
are
imported.
import Attribute Example
…
The import Attribute
<%@ page
import="java.util.*,coreservlets.*" %>
<%!
private String randomID() {
int num =
(int)(Math.random()*10000000.0);
return("id" + num);
}
private final String NO_VALUE = "
No
Value";
%>
<%
String oldID =
CookieUtilities.getCookieValue(request,
"userID",
NO_VALUE);
if (oldID.equals(NO_VALUE)) {
String newID = randomID();
Cookie cookie = new
LongLivedCookie("userID", newID);
response.addCookie(cookie);
}
%>
This page was accessed on <%= new
Date() %> with a userID
cookie of <%= oldID %>
2. Designating which classes are imported.
The import Attribute Example
The page uses three classes not in the standard JSP import list:
+ java.util.Date
+ coreservlets.CookieUtilities
+ coreservlets.LongLivedCookie.
So, to simplify references to these classes, the JSP page uses:
<%@ page import="java.util.*,coreservlets.*" %>
2. Designating which classes are imported.
The import Attribute Example Result
3. Specifying the MIME type of the page
• Format.
• <%@ page contentType="MIME-Type" %>
• <%@ page contentType="MIME-Type; charset=Character-Set" %>
• <%@ page pageEncoding="Character-Set" %>
•
Purpose.
• The contentType attribute sets the Content-Type response header, indicating the MIME type
of the document being sent to the client.
•
Notes.
• See section on response headers for table of the most common MIME types.
4. Generating Excel spreadsheets
First
Last
Email Address
Marty Hall
[email protected]
Larry
Brown
[email protected]
Steve
Balmer
[email protected]
Scott
McNealy
[email protected]
<%@ page contentType="application/vnd.ms-excel" %>
<%-- There are tabs, not spaces, between cols. --%>
4. Generating Excel spreadsheets
• Microsoft Excel can import tables that are represented in HTML with the
Table tag. This capability suggests a simple method of returning either
HTML or Excel content, depending on which the user prefers: just use an
HTML table and set the content type to application/vnd.ms-excel, only if the
user requests the results in Excel.
4. Generating Excel spreadsheets
<% boolean usingExcel = checkUserRequest(request); %>
<% if (usingExcel) { %>
<%@ page contentType="application/vnd.ms-excel" %>
<% } %>
Cause the Attribute values (contentType) cannot be computed at runtime so we
use scriptlets and the normal
<%
String format = request.getParameter("format");
if ((format != null) && (format.equals("excel"))) {
response.setContentType("application/vnd.ms-excel");
}
%>
4.
Generating
Excel
spreadsheets
Example
Comparing Apples and Oranges
<%
String format = request.getParameter("format");
if ((format != null) && (format.equals("excel"))) {
response.setContentType("application/vnd.ms-excel");
}
%>
| Apples | Oranges
|
---|
First Quarter | 2307 | 4706
|
---|
Second Quarter | 2982 | 5104
|
---|
Third Quarter | 3011 | 5220
|
---|
Fourth Quarter | 3055 | 5287
|
---|