教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

软件开发基础(.Java)实验指导书V2(7)

来源:网络收集 时间:2026-04-27
导读: int count = 0; String dbPath; public void jspInit(){ try{ dbPath = getServletContext().getRealPath(\FileInputStream fis = new FileInputStream(dbPath); DataInputStream dis = new DataInputStream(fis);
<%@ page language=\<%!

int count = 0; String dbPath;

public void jspInit(){ try{

dbPath = getServletContext().getRealPath(\FileInputStream fis = new FileInputStream(dbPath); DataInputStream dis = new DataInputStream(fis); count = dis.readInt(); dis.close(); }

catch(Exception e){

log(\} } %>

<%--下面是向浏览器输出的主要内容,

它将成为产生的_jspService()方法的一部分 --%>

<% count++; %>

Welcome! You are <%= count %>th visitor(s). <%!

public void jspDestroy(){ try{

FileOutputStream fos = new FileOutputStream(dbPath); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(count); dos.close(); }

catch(Exception e){

log(\} } %>

19

(四)输入并运行下面的import_test.jsp页面:

<%@ page import=\

page指令示例

<% Date date = new Date();

String s = DateFormat.getDateInstance().format(date);

String s2 = DateFormat.getDateInstance(DateFormat.FULL).format(date); %>

访问上述JSP页面,输出结果如下图所示:

图6.2 import_test.jsp页面的运行结果

可以看到页面中最后一行的中文显示为乱码,将下面代码加到JSP页面中: <%@ page contentType=\

重新访问页面,中文显示正常。这说明可以使用page指令的contentType属性指定页面输出使用的字符编码。默认情况下,JSP页面使用的字符集是iso-8859-1编码,如使用汉字编码应指定为gb2312或gbk。

(五) errorPage属性和isErrorPage属性的使用。

【步骤1】下面的hello.jsp页面执行时将抛出一个异常,它指定了错误处理页面为errorHandler.jsp。

<%@ page contentType=\

<%@ page errorPage=\ <%

String name = request.getParameter(\if (name==null){

throw new RuntimeException(\没有指定name 属性。\} %>

Hello, <%=name%>

【步骤2】下面的errorHandler.jsp是错误处理页面。

<%@ page contentType=\<%@ page isErrorPage=\

20

请求不能被处理:<%=exception.getMessage()%>
请重试!

用下面的URL访问hello.jsp页面,就会产生下面结果: http://localhost:8080/bookstore/hello.jsp

图6.3 errorHandler.jsp页面的运行结果

这说明没有提供name参数,hello.jsp页面中抛出RuntimeException异常,所以调用错误页面。

如果使用下面URL访问 hello.jsp页面,将产生下面正常的页面: http://localhost:8080/bookstore/hello.jsp?name=Mary

图6.4 hello.jsp页面的运行结果

注意:如果请求参数值使用汉字,仍然产生乱码,例如: http://localhost:8080/bookstore/hello.jsp?name=欧阳清风

这是因为请求参数默认使用iso-8859-1编码传递,如果要正确显示中文,应将请求参数编码转换为gb2312编码。

按下面方法修改hello.jsp页面,将请求参数值编码转换为汉字编码。 <%@ page contentType=\<%@ page errorPage=\ <%

String name = request.getParameter(\if (name==null){

throw new RuntimeException(\没有指定name 属性。\} else{

// 将字符串name的字符编码转换为汉字编码

name = new String(name.getBytes(\} %>

Hello, <%=name %>

21

(六)输入并执行下面JSP页面,文件名为counter.jsp

<%@ page language=\<%! int count = 0; %> <% count++; %>

Welcome! You are visitor number <%= count %> 【步骤1】该JSP页面包含哪些JSP语法元素。在浏览器中访问该页面,输出结果如何?多次刷新页面,结果如何?

【步骤2】打开counter.jsp转换后的源文件counter_jsp.java,对应的类文件在哪里?文件名是什么?

【步骤3】查看count变量是在哪里声明的?

【步骤4】将上面JSP页面中的<%! int count = 0; %>一行改为<% int count = 0; %>,页面能否正常执行,它与上面页面有什么区别?

(七)运行下面persistent_counter.jsp页面,体会如何实现持久的计数器的? <%@ page language=\<%!

int count = 0; String dbPath;

public void jspInit(){ try{

dbPath = getServletContext().getRealPath(\FileInputStream fis = new FileInputStream(dbPath); DataInputStream dis = new DataInputStream(fis); count = dis.readInt(); dis.close(); }

catch(Exception e){

log(\} } %>

<%--下面是向浏览器输出的主要内容,

它将成为产生的_jspService()方法的一部分 --%>

<% count++; %>

Welcome! You are <%= count %>th visitor(s). <%!

public void jspDestroy(){ try{

FileOutputStream fos = new FileOutputStream(dbPath); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(count); dos.close(); }

catch(Exception e){

log(\} } %>

22

(八)输入并运行下面的import_test.jsp页面:

<%@ page import=\

page指令示例

<% Date date = new Date();

String s = DateFormat.getDateInstance().format(date);

String s2 = DateFormat.getDateInstance(DateFormat.FULL).format(date); %>

访问上述JSP页面,输出结果如下图所示:

图6.2 import_test.jsp页面的运行结果

可以看到页面中最后一行的中文显示为乱码,将下面代码加到JSP页面中: <%@ page contentType=\

重新访问页面,中文显示正常。这说明可以使用page指令的contentType属性指定页面输出使用的字符编码。默认情况下,JSP页面使用的字符集是iso-8859-1编码,如使用汉字编码应指定为gb2312或gbk。

(九) errorPage属性和isErrorPage属性的使用。

【步骤1】下面的hello.jsp页面执行时将抛出一个异常,它指定了错误处理页面为errorHandler.jsp。

<%@ page contentType=\

<%@ page errorPage=\ <%

String name = request.getParameter(\if (name==null){

throw new RuntimeException(\没有指定name 属性。\} %>

Hello, <%=name%>

【步骤2】下面的errorHandler.jsp是错误处理页面。

<%@ page contentType=\<%@ page isErrorPage= …… 此处隐藏:1423字,全部文档内容请下载后查看。喜欢就下载吧 ……

软件开发基础(.Java)实验指导书V2(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/607979.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)