–JSP page指令 –JSP taglib指令 –JSP include指令
############################################
? JSP page指令 – page指令的用途 ? 为根据JSP页面生成的servlet指定高层的信息 – 能够控制 ? 导入哪些类 ?该servlet扩展哪个类 ? 产生哪种MIME类型 ? 如何处理多线程 ? servlet是否共享会话 ? 输出缓冲区的大小的行为 ? 由哪个页面来处理意外的错误 – import属性 ? 格式 – <%@ page import="package.class" %> – <%@ page import="package.class1,...,package.classN" %> ? 目的 –在servlet定义的顶部生成导入语句 ? 注意 – 尽管JSP页面几乎可以放在服务器的任何位置,但由JSP使用的类必须放在常规的servlet目录中 ? 例如: – …/WEB-INF/classes或 – …/WEB-INF/classes/directoryMatchingPackage
? 示例(代码) –<%@ page import="java.util.*,com.amaker.*" %>
TestImport.java
User.java
TestImport.jsp
测试
看一下转换的Servlet文件哈~
TestImport_jsp.java
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import com.michael.jsp.*;
public final class TestImport_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType( "text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext( this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write( "\r\n");
out.write( "\r\n");
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <base href=\""); out.print(basePath); out.write("\">\r\n"); out.write(" \r\n"); out.write(" <title>My JSP 'TestImport.jsp' starting page</title>\r\n"); out.write(" \r\n"); out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n"); out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n"); out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n"); out.write("\t<!--\r\n"); out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n"); out.write("\t-->\r\n"); out.write("\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" <h1>Test Import!</h1>\r\n"); out.write(" "); List list = new ArrayList(); User u = new User(); u.setId(1); u.setName("michael"); list.add(u); for(int i=0;i<list.size();i++){ User u2 = (User) list.get(i); out.println(u2.getId()); out.println(u2.getName()); } out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import com.michael.jsp.*;
public final class TestImport_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType( "text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext( this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write( "\r\n");
out.write( "\r\n");
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <base href=\""); out.print(basePath); out.write("\">\r\n"); out.write(" \r\n"); out.write(" <title>My JSP 'TestImport.jsp' starting page</title>\r\n"); out.write(" \r\n"); out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n"); out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n"); out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n"); out.write("\t<!--\r\n"); out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n"); out.write("\t-->\r\n"); out.write("\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" <h1>Test Import!</h1>\r\n"); out.write(" "); List list = new ArrayList(); User u = new User(); u.setId(1); u.setName("michael"); list.add(u); for(int i=0;i<list.size();i++){ User u2 = (User) list.get(i); out.println(u2.getId()); out.println(u2.getName()); } out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
–contentType和pageEncoding属性 ? 格式 –<%@ page contentType="MIME-Type" %> –<%@ page contentType="MIME-Type;charset=Character-Set" %> –<%@ page pageEncoding="Character-Set" %> ? 目的 –指定由JSP页面生成的servlet生成的页面的MIME类型 ? 注意 –属性值不能在请求期间计算得出 –常见MIME类型的表格,参见介绍响应报头的章节 ? 生成Excel电子表格 –<%@ page contentType="application/vnd.ms-excel" %>
默认ISO-8859-1字符编码不显示中文,如果要显示中文可以使用gbk或gb2312
看下效果:
我们也可以不使用PageEncoding,可以指定内容类型的同时指定字符编码
看下效果:
生成Excel电子表格
看下效果:
TestContentType_jsp.java
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class TestContentType_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType( "application/vnd.ms-excel;charset=gbk");
pageContext = _jspxFactory.getPageContext( this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write( "\r\n");
out.write( "\r\n");
out.write( "\r\n");
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; out.write("\r\n"); out.write("\r\n"); out.write('\r'); out.write('\n'); out.write(' '); out.println("tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B8:E8)"); out.println("Oranges\t77\t86\t93\t30\t=SUM(B9:E9)"); out.write("\r\n"); out.write("\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class TestContentType_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType( "application/vnd.ms-excel;charset=gbk");
pageContext = _jspxFactory.getPageContext( this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write( "\r\n");
out.write( "\r\n");
out.write( "\r\n");
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; out.write("\r\n"); out.write("\r\n"); out.write('\r'); out.write('\n'); out.write(' '); out.println("tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B8:E8)"); out.println("Oranges\t77\t86\t93\t30\t=SUM(B9:E9)"); out.write("\r\n"); out.write("\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
–session属性 ?格式 –<%@ page session="true" %> <%-- Default --%> –<%@ page session="false" %> ?目的 –指定页面是不是会话的一部分 ?注意 –默认地,它是属于会话 –如果网站的流量较大,可以节省服务器端的内存 –要想真正起到作用,所有的相关网页都必须这样做
– errorPage属性 ? 格式 – <%@ page errorPage="Relative URL" %> ? 目的 – 指定一个JSP页面,抛出的任何异常如果未在当前页面内被捕获,则由该页面进行处理。
TestErrorPage.jsp
此程序错误为空指向异常
现在加一个错误转向页面
TestErrorPage.jsp
error.jsp
看下效果:
? 注意 – 指定的错误处理页面可以通过“exception”变量来访问所抛出的异常。 – web.xml文件允许我们指定适用于整个应用的错误页面,当任何确定的异常或确定的HTTP状态代码产生时,都会使用该页面来处理。 – errorPage属性仅用于指定特定页面的错误页面
web.xml
404.jsp
看下效果:
– isErrorPage属性 ? 格式 – <%@ page isErrorPage="true" %> – <%@ page isErrorPage="false" %> <%-- Default --%> ? 目的 – 标示当前页面是否可以作为其他JSP页面的错误页面。 ? 注意 – 名为exception的新预定义变量会被创建,并能够在错误页面中对其进行访问 – 只能将其作为紧急备份;明确地处理尽可能多的各种异常 – 不要忘记一定要检查查询数据中的值是否缺失或异常
下面指定被处理页面的错误类型
error.jsp
看下效果:
? JSP taglib指令 –格式 ? <%@ taglib uri=“” prefix=“”%> –uri:自定义标签的uri –prefix:标签前缀 –目的 ?在当前页面导入自定义标签 –示例 ?使用JSTL – <%@ taglib uri=" prefix="c" %>
TestTagLib.jsp
看下效果:
再看另一实例 TestTagLib.jsp
看下效果:
? JSP include指令 –格式 ? <%@ include file="Relative URL" %> –目的 ?为了在多个页面重用JSP内容,且需要JSP页面能够影响主页面
Head.jsp
footer.jsp
main.jsp
看下结果:
################################################