实验一
1.实验目的
(1)掌握怎样设置Web服务目录、怎样访问Web服务目录下的JSP页面。
(2)掌握怎样在JSP页面中使用成员变量、怎样使用Java程序片、Java表达式。
2.实验内容
1. 输出所有1到1000内的完数。(perfect.jsp)
一个数如果恰好等于除它本身外的因子之和,这个数就称为"完数",也叫“完美数”。例如6=1+2+3.(6的因子是1,2,3),因此,6 就是一个完数。
2. 输出乘法口诀表。(multi.jsp)
3. 编写两个JSP 页面,名字分别为inputName.jsp和people.jsp。
3.实验要求
1)在硬盘分区D下新建一个目录,名字为:student,将student设置为Web服务目录,并为该Web服务目录指定名字为practice的虚拟目录。在tomcat的安装目录中的\conf\Catalina\localhost下新建一个xml文件,通过文件来设置新的web服务目录。必须在浏览器的地址栏中输入:Tomcat服务器的IP地址和端口号,并通过虚拟目录practice访问Web服务目录student下的JSP页面。
2)启动Tomcat服务器(如果已经启动,必须关闭Tomcat服务器,并重新启动)。
3)用文本编辑器ultraedit编写JSP页,并保存到Web服务目录student中。
4)用浏览器访问Web服务目录student中的JSP页面。
5)inputName.jsp的具体要求
该页面有一个表单,用户通过该表单输入自己的姓名并提交给people.jsp页面。
6)people.jsp的具体要求
(1) JSP页面有名字为person、类型是StringBuffer以及名字是count,类型为int的成员变量。
(2) JSP有public void judge ()方法。该方法负责创建person对象,当count的值是0 时,judge ()方法创建person对象。
(3) JSP有public void addPerson(String p)的方法,该方法将参数p指定的字符串尾加到操作成员变量person,同时将count作自增运算。
(4) JSP页面在程序片中获取inputName.jsp页面提交的姓名,然后调用judge ()创建person对象、调用addPerson方法将用户的姓名尾加到成员变量person。
(5) 如果inputName.jsp页面没有提交姓名,或姓名含有的字符个数大于10,就使用<jsp:forward page="要转向的页面" />标记将将用户转到inputName.jsp页面。
(6) 通过Java表达式输出person和count的值。
7)前两个实验任选一个实现即可,第3个实验为必做题。
8)写出xml文件的代码以及jsp文件的源代码、运行结果(截图)。
4. 实验代码
Practice xml文件代码:<Context path="/practice" docBase="D:\student” debug="0" reloadable="true" />
实验2代码:
<%@page contentType="text/html;charset=GB2312"%>
<html>
<head>
<title>乘法口诀表</title>
</head>
<table border="1">
<%for(int i=1;i<10;i++){%>
<tr>
<%for (int j=1;j<=i;j++){ %>
<td><%=j%> *
<%=i%> =
<%=i*j%> </td>
<%} %>
</tr>
<%}%>
</table>
</html>
效果截图:
实验3:
inputName.jsp 代码:
<%@ page contentType="text/html;Charset=GB2312" %>
<html>
<body bgcolor=yellow>
<FONT size=3>
<FORM action="people.jsp" method=get name=form> 请输入姓名:
<INPUT type="text" name="name">
<INPUT type="submit" value="提交" name=submit >
</FORM>
</body>
</html>
people.jsp代码:
<%@ page contentType="text/html;Charset=GB2312" %>
<%!
int count;
StringBuffer person;
public void judge()
{
if(count==0)
person=new StringBuffer();
}
public void addPerson(String p)
{
person.append(" "+p); count++;
}
%>
<%
String textContent=request.getParameter("name");
if(textContent.length()>10||textContent.length()==0)
{%>
<jsp:forward page="inputName.jsp" />
<%}
judge();
addPerson(textContent);
%>
共有<%=count %>个人浏览过该页面
<br>
他们的名字是: <%=person %>。
运行结果截图:
实验二
1.实验目的
(1)掌握怎样在JSP页面中使用page指令设置contentType的值;使用include指令在JSP页面中静态插入一个文件的内容。
(2)掌握怎样在JSP页面中使用include标记动态加载文件;使用forward实现页面的转向。
2.实验内容
(1)编写三个JSP 页面:first.jsp 、second.jsp和third.jsp。另外,要求用“记事本”编写一个txt文件hello.txt。hello.txt的每行有若干个英文单词,单词之间用空格分隔,每行之间用“<BR>”分隔,如下所示:
hello.txt
package apple void back public <BR>
private throw class hello welcome
(2)编写四个JSP 页面:one.jsp 、two.jsp、three.jsp和error.jsp。one.jsp 、two.jsp和three.jsp页面都含有一个一个导航条,以便让用户方便地单击超链接访问这三个页面,要求这三个页面通过使用include动作标记动态加载导航条文件:head.txt。导航条文件head.txt的内容如下所示:
head.txt
<%@ page contentType="text/html;charset=GB2312" %>
<table cellSpacing="1" cellPadding="1" width="60%" align="center" border="0" >
<tr valign="bottom">
<td><A href="one.jsp"><font size=3>one.jsp页面</font></A></td>
<td><A href="two.jsp"><font size=3>two.jsp页面</font></A></td>
<td><A href="three.jsp"><font size=3>three.jsp页面</font></A></td>
</tr>
</Font>
</table>
3.实验要求
(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文件。
(4)one.jsp的具体要求
要求one.jsp页面有一个表单,用户使用该表单可以输入一个1至100之间的整数,并提交给当前页面;如果输入的整数在50至100之间(不包括50)就转向three.jsp,如果在1至50之间就转向two.jsp;如果输入不符合要求就转向error.jsp。要求forward标记在实现页面转向时,使用param子标记将整数传递到转向的two.jsp或three.jsp页面,将有关输入错误传递到转向的error.jsp页面
(5) two.jsp、three.jsp和error.jsp的具体要求
要求two.jsp和three.jsp能输出one.jsp传递过来的值,并显示一幅图像,该图像的宽和高刚好是one.jsp页面传递过来的值。error页面能显示有关错误信息,和一幅图像。
(6)写出jsp文件的源代码、运行结果(截图)。
4.代码及效果截图
first.jsp代码:
<%@ page contentType="text/plain;charset=GB2312" %>
<HTML>
<BODY>
<FONT color=purple>
<%@ include file="hello.txt"%>
</FONT>
</BODY>
</HTML>
效果截图:
second.jsp代码:
<%@ page contentType="application/vnd.ms-powerpoint;charset=GB2312" %>
<HTML>
<BODY>
<%@ include file="hello.txt"%>
</BODY>
</HTML>
效果截图;
图4 contentType的值是application/vnd.ms-powerpoint
third.jsp代码:
<%@ page contentType="application/msword;charset=GB2312" %>
<HTML>
<BODY>
<%@ include file="hello.txt"%>
</BODY>
</HTML>
效果截图:
one.jsp代码:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body bgcolor=yellow >
<%@ include file="head.txt"%>
<br>请输入1~100的整数:
<form action="" method=post name=form>
<input type="text" name="number" >
<br>
<input type="submit" value="提交" name="submit" >
</form>
<%
String number=request.getParameter("number");
if(number==null)
{
number="0";
}
int n=Integer.parseInt(number);
if(n<=50&&n>=1)
{%>
<jsp:forward page="two.jsp">
<jsp:param name="number" value="<%=n%>"/>
</jsp:forward>
<%}
else if(n>50&&n<=100)
{%>
<jsp:forward page="three.jsp">
<jsp:param name="number" value="<%=n%>"/>
</jsp:forward>
<%}
else if(n>100)
{%>
<jsp:forward page="error.jsp">
<jsp:param name="number" value="<%=n%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>
效果截图;
two.jsp代码:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body bgcolor=yellow>
<font size=3>
这是two.jsp页面。
<br>
<%
String s=request.getParameter("number");
out.println("传递过来的值是:"+s);
%>
<br>
<img src=two.jpg width=<%=s%> height=<%=s%>></img>
</font>
</body>
</html>
效果截图;
three.jsp代码;
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body bgcolor=yellow>
<font size=3>
这是three.jsp页面。
<br>
<%
String s=request.getParameter("number");
out.println("传递过来的值是:"+s);
%>
<br><img src=two.jpg width=<%=s%> height=<%=s%>></img>
</font>
</body>
</html>
效果截图:
error.jsp代码:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body bgcolor=yellow>
<font size=3>
这是error.jsp页面。
<br>
<%
String e=request.getParameter("number");
out.println("传递过来的值是:"+e);
%>
<br><img src=two.jpg width=100 height=100></img>
</font>
</body>
</html>
、效果截图:
实验三
1.实验目的
(1)掌握怎样在JSP中使用内置对象request,以及使用response对象动态响应用户的请求。
(2)掌握怎样使用session对象存储和用户有关的数据。
2.实验内容
(1)编写两个JSP页面input.jsp和result.jsp。input.jsp页面提交一个数字给result.jsp页面,result.jsp页面使用response对象做出动态响应。
(2)编写四个JSP 页面letter.jsp、guess.jsp、small.jsp、large.jsp,实现猜字母游戏。
3.实验要求
(1)input.jsp的具体要求
input.jsp提供表单,用户在表单中输入一个数字,提交给result.jsp页面。
(2)result.jsp的具体要求
result.jsp页面首先使用request对象获得input.jsp页面提交的数字,然后根据数字的大小作出不同的响应。如果数字小与0,response对象的调用setContentType(String s)方法将contentType属性的值设置为text/plain,同时输出数字的平方;如果数字大于等于0并且小于100,response对象的调用setContentType(String s)方法将contentType属性的值设置为application/msword,同时输出数字的立方;如果数字大于等于100,response对象调用setStatus(int n)方法将状态行的内容设置为404;如果用户在input.jsp页面输入了非数字,response对象调用sendRedirect(URL url)方法将用户的重定向到input.jsp页面。
(3)letter.jsp的具体要求
将要猜测的a到z之间的字母放到一个数组中,随机分配给用户。该页面同时负责将这个字母存在用户的session对象中。该页面提供表单,用户可以使用该表单输入自己的猜测,并提交给guess.jsp页面。
(4)guess.jsp的具体要求
判断letter.jsp提交的猜测是否和用户的session对象中存放的那个字母相同,如果相同就输出用户session对象中的数据(猜测的次数及猜测的字母);如果不相同就将用户重定向到large.jsp或small.jsp。
(5)写出jsp文件的源代码、运行结果(截图)。
4.JSP页面效果示例
input.jsp代码:
<%@ page contentType="text/html;Charset=GB2312" %>
<html>
<body> 请输入数字或字符串:
<form action="result.jsp" method=post name=form>
<input type="text" name="number">
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
效果截图:
result.jsp代码为:
<%@ page contentType="text/html;Charset=GB2312" %>
<html>
<body>
获取文本框提交的信息:
<% String number=request.getParameter("number");
if(number==null)
{
number="0";
}
try{
Double n=Double.parseDouble(number);
if(n<0)
{
response.setContentType("text/html;charset=GB2312");
out.println("输入的数字"+n+"的平方是:"+n*n);
}
else if(n<100)
{
response.setContentType("application/msword;Charset=GB2312");
out.println("输入的数字"+n+"的立方是:"+n*n*n);
}
else
{
response.setStatus(404);
}}
catch(Exception e)
{
response.sendRedirect("input.jsp");
}
%>
</body>
</html>
效果截图为:
letter.jsp代码:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
随机分给了您一个a到z之间的字母,请您猜测:
<% char a[]=new char[30];
int i,n=0;
for(i=97;i<=122;i++)
{a[n++]=(char)(i);}
char letter=a[(int)(Math.random()*26)];
session.setAttribute("count",new Integer(0));
session.setAttribute("save",new Character(letter));
%>
<br>
输入您的所猜的字母:
<form action="guess.jsp" method="get" name=form>
<input type="text" name="letter" >
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
效果截图:
guess.jsp代码;
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
<% String str=request.getParameter("letter");
if(str==null||str=="")
{ response.sendRedirect("letter.jsp");
}
else
{
char guessLetter = str.charAt(0);
if(guessLetter>'z'||guessLetter<'a')
{%>
<a href="letter.jsp">请输入一个小写字母</a>
<%}
else
{
Character a=(Character)session.getAttribute("save");
char realLetter=a.charValue();
if(guessLetter==realLetter)
{ int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
out.println("您猜对了!!,该字母就是:"+realLetter+"!您一共猜了"+n+"次!"); %>
<a href="letter.jsp">再玩儿一次</a><%
}
else if(guessLetter>realLetter)
{ int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count",new Integer(n));
response.sendRedirect("large.jsp");
}
else if(guessLetter<realLetter)
{ int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count",new Integer(n));
response.sendRedirect("small.jsp");
}
}
}
%>
</body>
</html>
Small.jsp代码:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body bgcolor=yellow>
所猜的数比实际的数小,请再猜:
<form action="guess.jsp" method="post" name=form>
<input type="text" name="letter" >
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
效果截图:
效果截图:
Lager.jsp代码
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
所猜的数比实际的数大,请再猜:
<form action="guess.jsp" method="get" name=form >
<input type="text" name="letter" >
<input TYPE="submit" value="提交" name="submit">
</form>
</body>
</html>
效果截图:
实验四
1.实验目的
(1)掌握使用有效范围是request的bean显示汽车的基本信息。
(2)掌握使用有效范围是session的bean显示汽车的基本信息。
(3)对不同有效范围的bean进行对比。
2.实验内容
(1)编写一个JSP页面:inputAndShow.jsp和一个名字为car的Javabean,其中car由Car.class类负责创建。
(2)编写两个JSP页面:inputCar.jsp和showCar.jsp。bean可使用(1)中的Car.java。
3.实验要求
(1) inputAndShow.jsp的具体要求
inputAndShow.jsp页面提供一个表单。其中表单允许用户输入汽车的牌号、名称和生产日期,该表单将用户输入的信息提交给当前页面,当前页面调用名字为car的bean,并使用表单提交的数据设置car的有关属性的值,然后显示car的各个属性的值。
(2) Car.java的具体要求
编写的Car.java应当有刻画汽车号码、名称和生产日期的属性,并提供相应的getXxx和setXxx方法,来获取和修改这些属性的值。Car.java的包名是practice。
(3) inputCar.jsp的具体要求
inputCar.jsp页面提供一个表单。其中表单允许用户输入汽车的牌号、名称和生产日期,该表单将用户输入的信息提交给当前页面,当前页面调用名字为car的bean,并使用表单提交的数据设置car的有关属性的值。要求在inputCar.jsp提供一个超链接,以便用户单击这个超链接访问showCar.jsp页面。。
(4) showCar.jsp的具体要求
showCar.jsp调用名字为car的bean,并显示该bean的各个属性的值。
(5) 写出jsp文件的源代码、运行结果(截图),以及JSP实验课的心得体会。
4.JSP页面效果示例
Car.java代码;
package practice;
public class Car
{ String carName,carNumber="",date="";
StringBuffer carWatch=new StringBuffer();
public void setDate(String d)
{ date=d; }
public String getDate()
{ return date; }
public void setCarName(String n)
{ carName=n; }
public String getCarName()
{ return carName; }
public void setCarNumber(String s)
{ carNumber=s; }
public String getCarNumber()
{ return carNumber; }
public StringBuffer getCarWatch()
{ if(carName!=null&&carName.length()>0&&date.length()>0&&carNumber!=null&&carNumber.length()>0)
{
carWatch.append("<tr>");
carWatch.append("<td>");
carWatch.append(carNumber);
carWatch.append("</td>");
carWatch.append("<td>");
carWatch.append(carName);
carWatch.append("</td>");
carWatch.append("<td>");
carWatch.append(""+date);
carWatch.append("</td>");
carWatch.append("</tr>");
}
return carWatch;
}
}
inputAndShow.jsp代码;
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="practice.*"%>
<jsp:useBean id="list" class="practice.Car" scope="request"/>
<jsp:setProperty name="list" property="carName" param="carName" />
<jsp:setProperty name="list" property="carNumber" param="carNumber" />
<jsp:setProperty name="list" property="date" param="date" />
<html>
<body>
<form action="" method=post name=form>
汽车牌号:<input type="text" name="carNumber"> <br>
汽车名称:<input type="text" name="carName"> <br>
汽车生产日期:<input type="text" name="date">
<input type="submit" value="提交" name="submit">
</form>
<table border=1 >
<tr><th>汽车牌号</th><th>汽车名称</th><th>汽车生产日期</th><tr>
<jsp:getProperty name="list" property="carWatch"/>
</table>
效果截图:
inputCar.jsp代码:
<%@ page contentType="text/html;Charset=GB2312" %>
<html>
<body bgcolor=yellow>
<form action="showCar.jsp" method=post name=form>
汽车牌号:<input type="text" name="carNumber"> <br>
汽车名称:<input type="text" name="carName"> <br>
汽车生产日期:<input type="text" name="date">
<input type="submit" value="提交" name="submit">
</form>
<a href="showCar.jsp">访问showCar.jsp,查看有关信息</a>
</body>
</html>
效果截图:
showCar.jsp代码;
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="practice.*"%>
<%
String carName=request.getParameter("carName");
String carNumber=request.getParameter("carNumber");
String date=request.getParameter("date");
%>
<jsp:useBean id="list" class="practice.Car" scope="session"/>
<jsp:setProperty name="list" property="carName" param="carName" />
<jsp:setProperty name="list" property="carNumber" param="carNumber" />
<jsp:setProperty name="list" property="date" param="date" />
<html>
<body>
<table border=1 >
<tr>
<th>汽车牌号</th>
<th>汽车名称</th>
<th>汽车生产日期</th>
<tr>
<jsp:getProperty name="list" property="carWatch"/>
</table>
</body>
</html>
效果截图: