JSP实验二Word下载.docx
《JSP实验二Word下载.docx》由会员分享,可在线阅读,更多相关《JSP实验二Word下载.docx(23页珍藏版)》请在冰豆网上搜索。
doublegetLength(doublea){
return2*a*pi;
%>
%
intx=100;
out.println("
调用getArea方法计算半径是"
+x+"
的园的面积"
);
doublearea=getArea(x);
out.println(area);
inty=50;
br>
调用getLength方法计算半径是"
+y+"
的园的周长"
doublelength=getArea(y);
out.println(length);
/body>
/html>
(2)运行结果:
(alt+printscreen)
(3)运行结果分析:
在程序片中定义了两个方法,分别计算面积和周长。
2.编写JSP页面,用户在该JSP页面输入E-mail地址并提交,在该页面判断用户输入的E-mail地址是否含有非法字符。
p>
请输入E-mail:
formaction="
"
method=getname=from>
inputtype="
text"
name="
client"
value="
请输入E-mail"
>
submit"
送出"
/form>
Stringstr=request.getParameter("
if(str!
=null){
intindex=str.indexOf("
@"
if(index==-1){
您的E-mail地址中没有@。
}
else{
intspace=str.indexOf("
if(space!
=-1){
您的E-mail地址含有非法的空格。
intstart=str.indexOf("
intend=str.lastIndexOf("
if(start!
=end){
您的E-mail地址中有两个以上的符号:
@。
+str);
您的E-mail地址书写正确。
在表单中输入邮箱,并根据获取的结果来判断输出提示。
3.编写JSP页面,用户在该JSP页面输入三角形的三条边长度并提交,在该页面输出三角形的面积。
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
gb2312"
请输入三角形三边a,b,c的长度:
/p>
method=postname=form>
a:
name=a>
b:
name=b>
c:
name=c>
name=submitvalue=送出>
%
Stringstr_a=request.getParameter("
a"
Stringstr_b=request.getParameter("
b"
Stringstr_c=request.getParameter("
c"
doublea,b,c;
if(str_a==null){
str_a="
0"
;
str_b="
str_c="
}
try{
a=Double.valueOf(str_a).doubleValue();
b=Double.valueOf(str_b).doubleValue();
c=Double.valueOf(str_c).doubleValue();
if(a+b>
c&
&
a+c>
b&
b+c>
a){
doublep=(a+b+c)/2;
doublearea=Math.sqrt(p*(p-a)*(p-b)*(p-c));
out.print("
+"
三角形的面积"
+area);
}else
{
你输入的三边不能构成一个三角形"
}catch(NumberFormatExceptione){
out.print("
请输入数字字符"
输入三个数,并判断是否可以构成三角形,计算结果。
4.编写两个JSP页面,名字分别为inputName.jsp和people.jsp。
1)inputName.jsp的具体要求
该页面有一个表单,用户通过该表单输入自己的姓名并提交给people.jsp页面。
2)people.jsp的具体要求
(1)JSP页面有名字为person、类型为StringBuffer以及名字是count、类型为int的成员变量。
(2)JSP页面有publicvoidjudge()方法。
该方法负责创建personList对象,当count的值是0时,judge()方法创建personList对象。
(3)JSP页面有publicvoidaddPerson(Stringp)的方法,该方法将参数p指定的字符串尾加到成员变量personList,同时将count作自增运算。
(4)JSP页面在程序中获取inputName.jsp页面提交的姓名,然后调用judge()创建personList对象,调用addPerson方法将用户的姓名尾加到成员变量personList。
(5)如果inputName.jsp页面没有提交姓名,或姓名含有的字符个数大于10,就使用<
jsp:
forwordpage="
要转向的页面"
/>
标记将用户转到inputName.jsp页面。
(6)通过Java表达式输出person和count的值。
(1)源代码:
!
--inputName.jsp-->
charset=GB2312"
people.jsp"
method=getname=form>
请输入姓名:
name"
value=送出name=sumbit>
—people.jsp-->
<
intcount=0;
StringBufferpersonList;
publicvoidjudge(){
if(count==0){
personList=newStringBuffer();
publicvoidaddPerson(Stringp){
personList.append(p);
}else{
personList.append("
"
+p);
count++;
}%>
Stringname=request.getParameter("
byte[]bb=name.getBytes("
iso-8859-1"
name=newString(bb);
if(name.length()>
10||name.length()==0){
%>
forwardpage="
inputName.jsp"
/>
}
judge();
addPerson(name);
目前一共有<
%=count%>
人浏览了该页面,他们的名字是:
%=personList%>
累计并顺序输入表单中的姓名。
5.编写三个JSP页面:
first.jsp、second.jsp和third.jsp。
另外,要求用“记事本”编写一个TXT文件hello.txt。
hello.txt的每行有若干个英文单词,这些单词之间用空格分隔,每行之间用“<
BR>
”分隔,如下所示:
hello.txt
packageapplevoidbackpublic
privatethrowclasshellowelcome
(1)first.jsp的具体要求
first.jsp使用page指令设置contentType属性的值是text/plain,使用include指令静态插入hello.txt文件。
(2)second.jsp的具体要求
second.jsp使用page指令设置contentType属性的值是application/vnd.ms-powerpoint,使用include指令静态插入hello.txt文件。
(3)third.jsp的具体要求
third.jsp使用page指令设置contentType属性的值是application/msword,使用include指令静态插入hello.txt文件。
text/plain;
h5>
%@includefile="
hello.text"
/h5>
application/vnd.ms-powerpoint;
application/msword;
打一个输出的是text文件,第二个是PPT文件,第三个是Word文件。
6.编写四个JSP页面:
one.jsp、two.jsp、three.jsp和error.jsp。
one.jsp、two.jsp和three.jsp页面都含有一个导航条,以便让用户方便地单击超链接访问这三个页面,要求这三个页面通过使用include动作标记动态加载导航条文件head.txt。
导航条文件head.txt的内容如下所示:
head.txt
%@pagecontentType="
charset=GB2312"
tablecellSpacing="
1"
cellPadding="
width="
60%"
align="
center"
border="
trvalign="
bottom"
<
td>
Ahref="
one.jsp"
FONTSize=3>
one.jsp<
/FONT>
/A>
/td>
two.jsp"
two.jsp<
three.jsp"
three.jsp<
/tr>
/table>
(1)one.jsp的具体要求
要求one.jsp页面有一个表单,用户使用该表单可以输入一个1至100之间的整数,并且提交给本页面;
如果输入的整数在50至100之间(不包括50)就转向three.jsp,如果在1至50之间就转向two.jsp;
如果输入不符合要求的整数,如120,就转向error.jsp。
要求forword标记在实现页面转向时,使用param子标记将整数传递到转向的two.jsp或three.jsp页面,将有关错误的信息传递到转向的error.jsp页面。
(2)two.jsp、three.jsp和error.jsp的具体要求
要求two.jsp和three.jsp能输出one.jsp传递过来的值,并显示一幅图像,该图像的宽和高刚好是one.jsp页面传递过来的值。
error.jsp页面能显示有关错误信息和一幅警告出错的图像。
Head.text:
tablecellSpacing="
fontsize=3>
one.jsp页面<
/font>
two.jsp页面<
three.jsp页面<
tr>
One.jsp:
head>
includepage="
head.text"
/head>
bodybgcolor=yellow>
请输入1到100之间的整数:
number"
%Stringnum=request.getParameter("
if(num==null)
num="
try{intn=Integer.parseInt(num);
if(n>
=1&
n<
=50){
paramname="
%=n%>
/jsp:
forward>
%}
elseif(n>
50&
=100){
%<
100)
error.jsp"
mess"
}
catch(Exceptione){
%=e.toString()%>
%}
Two.jsp:
fontsize=2color=blue>
thisistwo.jsp<
Strings=request.getParameter("
out.println("
传递过来的值是"
+s);
imgsrc="
a.jpg"
%=s%>
height="
/img>
Three.jsp:
fontsize=2color=red>
thisisthree.jsp<
b.jpg"
Error.jsp
fontsize=5color=red>
thisiserror.jsp<
fontsize=2>
error.jpg"
120"
a.jpg,b.jpg,c.jpg
判断输入的数据的范围,确定输出的内容和大小。
7.编写两个JSP页面:
main.jsp和lader.jsp,将两个JSP页面保存在同一Web服务目录中。
main.jsp使用include动作标记动态加载lader.jsp页面。
lader.jsp页面可以计算并显示梯形的面积。
当lader.jsp被加载时获取main.jsp页面中include动作标记的param子标记提供的梯形的上底、下底和高的值。
上底是:
10
下底是:
高是:
lader.jsp"
shangdi"
10"
param>
xiadi"
gao"
include>
fontsize=3color=blue>
计算梯形的面积:
S