Part 2 JSP 基本语法练习
一、实验目的
1. 理解 JSP 程序结构
2. 掌握 JSP 各种变量的声明方式。
3. 掌握在页面中如何组织 JSP 程序。
4. 掌握 JSP 指令的应用。
5. 掌握 JSP 动作指令的应用。
6. 掌握 JSP 中的中文字符处理。
二、实验要求
- 编写简单的计数器程序。
- 编写 JSP 的 include 指令的程序。
- 编写 JSP 动态指令 forward 的程序。
- 编写 JSP 中文字符处理程序。
三、实验内容
(一) 简单的计数器程序
1.建立一个新的 WEB 工程,再建立一个 count.jsp,源代码如下。
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%!int counter=0;
synchronized void counterFunction()
{
counter++;
}
%>
<%counterFunction();%>
网站计数器<br>
您是第<%=counter%>位访问者
</body>
</html>
2.部署并浏览该页面。
(二) JSP 的 include 指令的程序
1.在建立的 WEB 工程中建立 include.jsp 文件,top.txt 文件和 bottom.txt 文件。源代
码如下:
include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ include file="top.txt"%>
<p align="center">
<%out.println("此书出版了,快来看看吧!还有更多的好书等着您呢!");%>
</p>
<%@ include file="bottom.txt"%>
</body>
</html>
top.txt
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>《JSP 从入门到实战》网站</title>
</head>
<body>
<p align="center"><b><font size="5">《JSP 从入门到实战》网站</font></b></p>
</body>
</html>
bottom.txt
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>《JSP 从入门到实战》网站</title>
</head>
<body>
<hr>
<p align="center">
<font size="3">@电子工业出版社版权所有 2004-2008</font><br>
<font size="3">制作人:邓子云</font><br>
<font size="3">联系方式:dengziyun@126.com</font><br>
</body>
</html>
部署并浏览该页面。
思考:如果将 top.txt 和 bottom.txt 改为 bottom.jsp 和 top.jsp,上述程序会出错吗? 会
错了如何改正? <%@ include file="xx.xx"%>
更改为相应后缀名。
(三) JSP 动态指令 forward 的程序
1.在建立的 WEB 工程中建立 forward.jsp 文件。源代码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<form name="form1" action="forward.jsp" method="post" >
程序示例链接:
<select name="goaddress" onchange="javascript:form1.submit()">
<option value="novalue"></option>
<option value="1">实例 13</option>
<option value="2">实例 14</option>
</select></form>
<% // forward 应用示例 Java 程序片
String s=null;
s=request.getParameter("goaddress");
if(s!=null)
{ switch(s.charAt(0))
{ case '1':
%>
<jsp:forward page="count.jsp"></jsp:forward>
<%
break;
case '2':
%>
<jsp:forward page="include.jsp "></jsp:forward>
<%
break;
default:
out.println("您没有选择。");
}
}
else
out.println("您没有选择。");
%>
</body>
</html>
2.部署并浏览该页面。
(四) JSP 中文字符处理程序
1.在建立的 WEB 工程中建立 convert.jsp 文件。源代码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e){
return s;
}
}
%>
<html>
<body>
<form name="form1" action=" convert.jsp " method="post">
请输入您的姓名:
<input type="text" name="username">
<input type="submit" name="submit" value="提交">
</form><br>
<%
if(request.getParameter("username")==null)
out.println("您没有输入姓名。");
else
out.println("您输入的姓名为:"+codeToString(request.getParameter("username")));
%>
</body>
</html>
3.部署并浏览该页面。