本题包括4个JSP程序,one.jsp、two.jsp、three.jsp、error.jsp。
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页面
two.jsp、three.jsp和error.jsp的具体要求如下:
要求two.jsp和three.jsp能输出one.jsp传递过来的值,并显示一幅图像,该图像的宽和高刚好是one.jsp页面传递过来的值。error页面能显示有关抛出的错误信息。(程序中使用的图片,可自行准备)。
JSP页面效果示例如下所示:
图E2-1 one.jsp运行效果
图E2-2 two.jsp运行效果
图E2-3 three.jsp运行效果
error.jsp(假如在one.jsp中输入‘a’,提交后,会跳入error页面显示如下:
图E2-4 error.jsp运行效果
我的代码:
我的代码:
我的代码:
1.JSP
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE html>
<html>
<head>
<title>4个网页</title>
</head>
<body>
<p>请输入1至100之间的整数:
<form action="1.jsp" method="get" name="form">
<input type="text" name="number">
<input type="submit" name="submit" value="提交">
</form>
<%
String str=request.getParameter("number");
if(str !=null){
try{
int num;
num = Integer.parseInt(str);
if(num>=1&&num<=50){
%>
<jsp:forward page="2.jsp">
<jsp:param name="str" value="<%=num %>"/>
</jsp:forward>
<%}
else if(num>50&&num<=100)
{
%><jsp:forward page="3.jsp">
<jsp:param name="str" value="<%=num %>"/>
</jsp:forward>
<% }
else if(num>100||num<=0)
{
%><jsp:forward page="4.jsp">
<jsp:param name="str" value="<%=num %>"/>
</jsp:forward>
<% }
}
catch(Exception e)
{
%><jsp:forward page="error.jsp">
<jsp:param name="mess" value="<%=e.toString()%>"/>
</jsp:forward>
<% }
}
%>
</body>
</html>
2.JSP:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor=blue>
<%
String s=request.getParameter("number");
%>
<h1>传递过来的数值:<cite><%out.println(s);%></cite></h1>
<h3>图片大小就是:<cite><%out.println(s);%></cite></h3>
<br><img src="C:\Users\HWP\Desktop\2\blue.jpg" width="<%=s%>" height="<%=s%>"</img>
</body>
</html>
3.JSP:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor=green>
<%
String s=request.getParameter("number");
%>
<h1>传递过来的数值:<cite><%out.println(s);%></cite></h1>
<h3>图片大小就是:<cite><%out.println(s);%></cite></h3>
<br><img src="C:\Users\HWP\Desktop\2\3.jpg" width="<%=s%>" height="<%=s%>"</img>
</body>
</html>
4.JSP:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<title>数值不在允许范围内!</title>
</head>
<body bgcolor=yellow>
<%
String s=request.getParameter("number");
%>
<h1>您输入的数值:<cite><%out.println(s);%></cite>不在允许范围内!<br>请重新输入···</h1>
<br><img src="C:\Users\HWP\Desktop\2\4.jpg"</img>
</body>
</html>
ERROR.JSP:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body bgcolor=red>
<%
String s=request.getParameter("number");
%>
<h3>您的输入: <cite><%out.println(s);%></cite></h3>
<br>
<h1>该输入不符合要求!请输入1~1000之间的数值</h1>
<br><img src="C:\Users\HWP\Desktop\2\err.jpg" width="<%=s%>" height="<%=s%>"</img>
</body>
</html>
测试:
1.jsp:
提交:0
提交:49
提交:50
提交:66
提交:100
提交:abc
提交超出范围的:-10
提交超出范围的:10000
OK!