您的位置:首页 > Web前端 > JavaScript

jsp 教程(二)

2013-10-23 01:53 183 查看
Technorati 标签: page,jsp     接着上一课程 jsp 教程(一),这一次将会谈到如何使用 jsp 的指令标签。jsp 的指令标签有 page、include、taglib。简单介绍如下:<%@ page options %> options 可以是一下一项或几项: language="java" 指定jsp container 要用什么语言来编译jsp 网页,默认值为 java;import="package-spec.*"  定义此jsp 页面可以使用哪些 java API,用逗号分隔列出一个或多个全称类名session="true or false" 只有在调试的时候才会设置为false,默认为truebuffer="none or number[kb]" 指定输出流缓存的大小,默认为8 kbautoflush="true or false" 决定输出流的缓冲区是否要自动刷新。默认为trueisThreadSafe="true or false" 默认为 true,表明此 jsp 页面可以处理来自多个线程的同步的请求。不建议使用 false。默认为 trueerrorPage="error-page-URL" 表面如果发生异常错误,网页会被重定向一个URL 页面。isErrorPage="true or false" 如果此页面被用作处理异常的页面,则设置为 true。默认为false。contentType="content-type" 表示将在生成Servlet 中使用的MIME类型和可选字符编码。默认为 text/htmlinfo="text" 表示此 jsp 页面的相关信息,可由 getServletInfo()方法返回。extends="name-of-super-class" 定义此jsp 页面产生的 Servlet是继承自哪一个父类,通常为HttpServlet)<%@ include file="file-URL" %> 将指定的文件包含到容器里<%@ taglib uri=".../glarf.tld" prefix="glarf" %>: 声明标签的使用    这一次主要介绍一下page 指令的相关属性,另外两个指令include和tablig将会在下一次详细说明。一、page import属性    属性 import 属性类似java类里import,不过page import 属性可以包好多个类,类名之间使用逗号分隔。其语法格式如下:
<%@ page import="package.class" %>
<%@ page import="package.class1,...,package.classN" %>
    例子如下:<%@ page import="java.util.*" %>    Demo:在这个demo 里包含了两个个自定义的类,都在com.tools包里,代码设计如下:import.jsp
<%@page import="java.util.Random"%>
<%@page import="org.apache.naming.java.javaURLContextFactory"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.tools.*,java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>the import attribute</h1>
<%!
private String randomID() {
Random random = new Random();
int num = random.nextInt(100000);
return "id" + num;
}
private final String NO_VALUE = "No value";
%>
<%
Cookie[] cookies = request.getCookies();
String oldID = ServletUtilities.getCookieValue(cookies, "userID", NO_VALUE);
String newID;
if(oldID.equals(NO_VALUE)){
newID = randomID();
}else{
newID = oldID;
}
ShortLivedCookie cookie = new ShortLivedCookie("userID",newID);
response.addCookie(cookie);
%>
this page was accessed at <%= new Date() %> with a userID cookie of <%= oldID %>
</body>
</html>
    ServletUtilities 和 ShortLivedCookie 代码设计如下,注意都在 com.tools包里,
package com.tools;
import javax.servlet.http.Cookie;
public class ServletUtilities {
public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
if (cookies == null) {
return defaultValue;
}
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies;
if (cookieName.equals(cookie.getName()))
return cookie.getValue();
}
return defaultValue;
}

public static Cookie getCookie(Cookie[] cookies, String cookieName) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return cookie;
}
return null;
}
}

//ShortLivedCookie 的代码
package com.tools;
import javax.servlet.http.Cookie;
public class ShortLivedCookie extends Cookie{

private static final long serialVersionUID = 1L;
public static final int SECOND_HALF_MINUTE = 30;
public ShortLivedCookie(String name, String value) {
super(name, value);
setMaxAge(SECOND_HALF_MINUTE);
}
}
    import.jsp 第一次访问的输出如下:    import.jsp 第二次访问的输出如下:二、 page contentType 属性    page contentType 属性石用来设置 response 的头字段 Content-Type的值,用来表示输出到客户端的文档的 MIME 类型。不同的ContentType 会影响客户端所看到的效果。一般来说,属性contentType 的使用有以下两种格式:<%@ page contentType="MIME-Type" %><%@ page contentType="MIME-Type; charset=Character-Set" %>    如:<%@ page contentType="text/html" %>    如上代码等同于 <% response.setContentType("text/html"); %>    附:Servlet 的默认 MIME 类型是 text/plain,jsp 默认类型是 text/html,默认的字符集是ISO-8859-1。    Demo1,:使用 contentType 属性产生纯文本文件。     Demo2:利用 contentType属性产生 excel 表格    将contentType 的属性值设为“application/vnd.ms-excel”,便可产生一个简易的 excel 表格。    格式化表格的方式有两种,一种方式是:将数据分成一行行的,列之间的使用 tab 分开(注意不是空格键)。如下是一个简单的例子(附:表格里的数据可以从数据里读取)。
<%@ page language="java" contentType="application/vnd.ms-excel; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%-- 注意列之间的元素要使用 tab 键来分开--%>
<%--另外注意一下,这里是使用jsp 注释,不要使用html注释 --%>
200820092010201120122013(expected)
100 150 134 156 140 140
    在 IE 的运行效果图如下:   首先会弹出如下对话框:   接着就是    格式化表格的第二种方式,就是使用html 表格属性控制,如果设置其对应 MIME 类型,则可输出 excel 表格,如果没有则输出html表格.    Demo3:其实和Demo2很类似。exportToExcel.jsp代码设计如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<H2>People information</H2>
<%  <!-- 获取用户输入的参数 -->
String format = request.getParameter("format");
if ((format != null) && (format.equals("excel"))) {
response.setContentType("application/vnd.ms-excel");
}
%>
<table border=1>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Age</th>
<tr>
<td>Mike</td>
<td>male</td>
<td>23</td>
<tr>
<td>Tom</td>
<td>male</td>
<td>24</td>
<tr>
<td>Rose</td>
<td>female</td>
<td>23</td>
<tr>
<td>Kate</td>
<td>female</td>
<td>22</td>
</table>
</body>
</html>
    输出效果图:   当在地址栏里输出参数”format = excel”,则可以excel 表格。 ,运行效果图如下:三、page isThreadSafe 属性    属性 isThreadSafe 表明此 jsp 页面可以处理来自多个线程的同步的请求。一般是默认使用,如下:<%@ page isThreadSafe="true" %> <%!-- 默认--%><%@ page isThreadSafe="false" %>    因为不推荐将isThreadSafe 属性设置为 false,且这是与线程相关的知识体系,在此就不多做介绍了。相关知识可参考java 线程知识。四、其他    对于剩下来的其他属性,可以参考一开始给出来的总结。在此只是补充一下如何使用属性 isErrorPage 和 errorPage。    isErrorPage = ”true or false”,如果此页面被用作处理异常错误的页面,则为true。在这种情况下,页面可被指定为另一页面 page 指令元素中 errorPage 属性的取值。指定此属性为 true 时,便可以在此页面使用内置对象 exception。默认值为false。    errorPage = “error-page-url”,表示如果网页发生异常错误,网页会被重新指向一个 url 页面。错误页面必须在其page 指令元素中指定 isErrorPage = ”true“。    Demo1:下面设置一个计算速度的 computeSpeed.jsp,如果正确输入参数,便可得到正确的输出;否则输出异常页面。    computeSpeed.jsp代码如下:
<!-- 指定 errorPage 属性 -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8" errorPage="speedErrors.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>computing speed</h2>
<%-- 注意,如果输入的value不是数字,将会抛出异常 --%>
<%!
private double toDouble(String value) {
return (Double.valueOf(value).doubleValue());
}
%>
<%
double distance = toDouble(request.getParameter("distance"));
double time = toDouble(request.getParameter("time"));
double speed = distance / time;
%>
Distance: <%= distance %> m
Time: <%= time %> s
Speed: <%= speed %> m/s.
</body>
</html>
    speedErrors.jsp代码如下:
<!-- 指定 isErrorPage 属性 -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true" import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>error computing speed</h2>
computeSpeed.jsp reported the following error: [i]<%= exception %>.
this problem occurred in the following place:
<pre>
<% exception.printStackTrace(new PrintWriter(out)); %>
</body>
</html>
    如果正确输出参数的值,可以得到如下页面:   参数输入错误时,将会得到如下页面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  标签 target style blank