jsp 教程(二)

简介:
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,默认为true
    • buffer="none or number[kb]" 指定输出流缓存的大小,默认为8 kb
    • autoflush="true or false" 决定输出流的缓冲区是否要自动刷新。默认为true
    • isThreadSafe="true or false" 默认为 true,表明此 jsp 页面可以处理来自多个线程的同步的请求。不建议使用 false。默认为 true
    • errorPage="error-page-URL" 表面如果发生异常错误,网页会被重定向一个URL 页面。
    • isErrorPage="true or false" 如果此页面被用作处理异常的页面,则设置为 true。默认为false。
    • contentType="content-type" 表示将在生成Servlet 中使用的MIME类型和可选字符编码。默认为 text/html
    • info="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[i];
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 第一次访问的输出如下:

image

    import.jsp 第二次访问的输出如下:

image

二、 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注释 --%>
2008 2009 2010 2011 2012 2013(expected)
100 150 134 156 140 140

    在 IE 的运行效果图如下:

   首先会弹出如下对话框:

image

   接着就是

image

    格式化表格的第二种方式,就是使用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>

    输出效果图:

image

   当在地址栏里输出参数”format = excel”,则可以excel 表格。image ,运行效果图如下:

image

三、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: <%= exception %>.
        this problem occurred in the following place:
 
  

    <pre>
    <% exception.printStackTrace(new PrintWriter(out)); %>
    
</body>
</html>

    如果正确输出参数的值,可以得到如下页面:

image

   参数输入错误时,将会得到如下页面。

image


本文转自peiquan 51CTO博客,原文链接:http://blog.51cto.com/peiquan/1313663


相关文章
|
11月前
|
Java
IDEA版SpringBoot全教程 04 整合JSP
IDEA版SpringBoot全教程 04 整合JSP
|
安全 Java 应用服务中间件
某教程学习笔记(一):18、JSP漏洞
某教程学习笔记(一):18、JSP漏洞
92 0
某教程学习笔记(一):18、JSP漏洞
|
前端开发 Java Maven
SpringBoot2.x系列教程09--SpringBoot中支持jsp页面
前言 在上一章节中,壹哥 跟大家讲过现在项目比较流行的开发模式,稍微大点的项目一般都是采用前后端分离的开发模式。如果我们的项目采用的是前后端不分离的模式,在SpringBoot中推荐我们使用Themeleaf模板来作为页面。 但是咱们都知道,Spring MVC中是支持JSP的,但是在Spring Boot中,其实不建议我们使用JSP。因为SpringBoot自带的嵌入式servlet容器对jsp的解析有使用限制,而且jsp的本质是Servlet,每个页面的加载都需要先进行编译,所以jsp的效率相对于html或Themeleaf都比较低。 但是如果你非要在Spring Boot中使用jsp
310 0
|
Java 数据安全/隐私保护
JSP web应用程序开发教程实验二
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396229 ...
1055 0
|
Java
JSP web应用程序开发教程实验一
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396232 ...
1021 0
|
XML Java 数据格式