Java实验指导与习题解析 } response.setContentType(\ PrintWriter out=response.getWriter(); out.println(\ out.println(\ out.println(\
out.println(\ out.println(\ } }
80
图5.3 GetCookie程序的运行结果
三. 思考题
1. 简述会话管理的常用方法有哪些? 2. 如何使用HttpSession对象共享数据?
3.6 实验六 JSP基本语法与页面指令
一. 实验目的
1. 熟练掌握JSP的声明、表达式、小脚本和注释的使用; 2. 理解JSP页面(page)指令和动作的语法格式; 3. 理解JSP页面的生命周期。
4. 熟练掌握page指令的下面的属性:import、session、errorPage、isErrorPage、contentType、pageEncoding。
5. 了解page指令的下面的属性:language、extends、buffer、autoFlush、info。
二. 实验内容
任务1:手工建立一个Java Web应用程序
本实验要求直接在Tomcat服务器中完成。首先在Tomcat的webapps目录中建立一个名为helloweb的应用程序,结构如下图所示:
在WEB-INF目录中建立一个名为web.xml的文件,该文件是部署描述文件。可从ROOT目录中复制该文件。
下面实验的JSP页面都建立在helloweb目录中,该目录是Web应用程序的上下文根目录。要求用记事本创建JSP页面。
任务2:输入并执行下面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; %>,页面能否正常执行,它与上面页面有什么区别?
任务3:修改并执行下面的expression.jsp页面
<%@ page language=\<%!
int anInt = 3;
boolean aBool = true;
Integer anIntObj = new Integer(3); Float aFloatObj = new Float(8.6); String str = \StringBuffer sBuff = new StringBuffer(); public char getChar(){ return 'A'; 81 第 3 章 Java Web实验指导 Java实验指导与习题解析 } %> <%= 500 %> <%= anInt*3.5/100-500 %> <%= aBool %> <%= false %> <%= !false %> <%= getChar() %>
<%= Math.random() %> <%= aVector %> <%= aFloatObj %>
<%= aFloatObj.floatValue() %> <%= aFloatObj.toString() %> <%= aBool; %>
<%= int i = 20 %>
<%= sBuff.setLength(12); %>
82 你能找出其中的错误吗?你可试着执行该页面,看会出现什么结果?将其中错误修改过来,最后输出下面结果:
图6.1 expression.jsp的运行结果
任务4:运行下面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(\} } %>
任务5:输入并运行下面的import_test.jsp页面
<%@ page import=\
<% 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=\83 第 3 章 Java Web实验指导 Java实验指导与习题解析 84 重新访问页面,中文显示正常。这说明可以使用page指令的contentType属性指定页面输出使用的字符编码。默认情况下,JSP页面使用的字符集是iso-8859-1编码,如使用汉字编码应指定为gb2312或gbk。 任务6: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=\
请求不能被处理:<%=exception.getMessage()%>
请重试!
用下面的URL访问hello.jsp页面,就会产生下面结果:
http://localhost:8080/helloweb/hello.jsp
图6.3 errorHandler.jsp页面的运行结果
这说明没有提供name参数,hello.jsp页面中抛出RuntimeException异常,所以调用错误页面。