140 likes | 151 Views
Learn how to control the structure of generated servlets in JSP using the page directive and import attribute. Import classes, customize superclass, set content type, and more.
E N D
Chapter 12 Controlling the Structure of Generated Servlets: The JSP page Directive
Chapter 12 • There are three main types of directives: page, include, and taglib. • The page directive control the structure of the servlet by importing classes, customizing the servlet superclass, setting the content type, and the like • include insert a file into the JSP page at the time the JSP file is translated into a servlet • taglib, defines custom markup tags
The import Attribute • The import attribute of the page directive specifies the packages that should be imported by the servlet into which the JSP page gets translated • <%@ page import="java.util.*" %> • Listing 12.1 ImportAttribute.jsp
The contentType and pageEncoding Attributes • The contentType attribute sets the Content-Type response header • <%@ page contentType="application/vnd.ms-excel" %> • Conditionally Generating Excel Spreadsheets (Section 3)
The session Attribute • The session attribute controls whether the page participates in HTTP sessions. • <%@ page session="true" %> • <%@ page session="false" %>
The buffer and autoFlush Attributes • The buffer attribute specifies the size of the buffer used by the out variable . • Use of this attribute takes one of two forms: • <%@ page buffer="sizekb" %> • <%@ page buffer="none" %>
The buffer and autoFlush Attributes • The autoFlush attribute controls whether the output buffer should be automatically flushed when it is full . • two forms. • <%@ page autoFlush="true" %> • <%@ page autoFlush="false" %>
The errorPage and isErrorPage Attributes • The errorPage attribute specifies a JSP page that should process any exceptions • <%@ page errorPage="Relative URL" %>
The errorPage and isErrorPage Attributes • The isErrorPage attribute indicates whether or not the current page can act as the error page for another JSP page. • Use of isErrorPage takes one of the following two forms: • <%@ page isErrorPage="true" %> • <%@ page isErrorPage="false" %>
Practice Exercises Exercise 1 Create an JSP page allow user enter full name. Display First name, last name, middle name. Use: java.util.StringTokenizer
Exercise 2 <% response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition","attachment;filename=" + classname + ".xls"); %> Sample export to excel file
Create sample list of students in a class. Create link to export the list of students in excel file format.
Exercise 3 • Use <%@ page errorPage="errormessage.jsp" %> <%@ page isErrorPage="true" %> Design generalErrorPage of your website