jsp实验报告(一)

  • 编写一个JSP程序,计算1!+2!+3!+4!+5!,并显示出结果。要求先声明计算阶乘的方法,再调用该方法,最后在页面上输出结果。
    进阶要求,通过表单提交一个正整数,然后计算它的阶乘和。例如:输入3,就计算1!+2!+3!
<%@ page contentType="text/html; charset=utf-8"%>

<html>
  <head>
  </head>
  <body>
      <h3>请输入1-10的整数,计算阶乘和</h3>
      <form action="1.jsp" method="get" name="form">
            <input type="text" name="number">
            <input type="submit" name="submit" value="提交">
      </form>     
        <%!public int count(int num){
            int s=1;
            if(num==1){
                 return 1;
               }
            else{
                for(int i=2;i<=num;i++){
                       for(int j=2;j<=i;j++){
                            s=s+s*j;
                          }
                  }
          }
          return s;
       }
      //返回输入数字
      int N(int n){
           return n;
      }
      %>
      <%
          String str = request.getParameter("number");
          if(str != null){
              try{
		             int num;
		             num= Integer.parseInt(str);
%>
     <p>你输入n的数值:<cite><%=N(num)%> </cite></p>
>>>
     <p>阶乘之和的结果:<cite><%=count(num)%></cite></p>
<% }	
    catch(NumberFormatException e){
		e.printStackTrace();
	    }
}
%>
       
  </body>
</html>

进阶要求得代码如下:
2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>静态</title>
</head>
<body bgcolor=salmon>
<a href="2.jsp"%></a>
<br><cite>请输入11000之间的整数:</cite><br><br>
<form action="2.jsp" method="get" name="form">
<input type="text" name="number">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

2.jsp

<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE html>
<html>
<head>
<title>求完数!!!</title>
</head>
<body bgcolor=bluegreen>
<p>请输入11000之间的整数:
<form action="2.jsp" method="get" name="form">
<input type="text" name="number">
<input type="submit" name="submit" value="提交">
</form>

<%!int Num(int num)     //判断是否是完数
    {
	int s=0;
    for(int i=1;i<num;i++) 
    	if(num%i==0)  	
    		s=s+i;
    if(s==num)
    	return 1;
    if(s!=num)
    	return 0;
    return 0;
}
//判断输入是否是在1-1000之间
int N(int i)
{
	if(i>0&&i<1000)
        return 1;
	else
		return 0;
}
%>
<%
String str=request.getParameter("number");
if(str !=null){
	try{
		int num;
		
		num= Integer.parseInt(str);
		if(N(num)==1){			%>
			<h3>您所输入的数:<cite><%out.println(num);%></cite>[1,1000]的范围里面!</h3>
			<%			}
		else{			%>
			<h2>您输入的数:<%out.println(num);%>不在[1,1000]的范围里面!</h2>
			<%			}
%>
<p> </p>
<%

if(Num(num)==1){
%>
	<p>这个数是完数!</p>
<%
}
else{
	%>
<p color="red">这个数不是完数!</p>
<%}
}
	catch(NumberFormatException e){
		e.printStackTrace();
		out.println("出现错误");
	}
}
%>
</body>
</html>

  • 在JSP页面中静态包含文件。要求程序包含两个文件,主文件静态包含一个能够计算1到1000内的完数的页面。(如果一个正整数刚好等于它的真因子之和,这样的正整数为完数, 例如,6=1+2+3,因此6就是一个完数。)

3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">   
    <title>My JSP '3.jsp' starting page</title>  
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->	

  </head>
  <body>
      <p>请输入一个数字</p>
      <form action="#" method="post" name="form">
          <input type="text" name="number">
          <br>
          <input type="submit" name="submit" value="计算">
      </form>
     
  </body>
</html>

  • 动态包含页面并传递数据。要求程序包含两个文件,主文件加载次文件,并将随机产生的50~100之间的数据传递给它,并且在页面上显示两个信息:该数据和这个数据的平方根。
    MyJsp3_1.jsp
<%@page language="java" contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
     <title>练习3</title>
</head>

<body>
   
    <%@page import="java.util.Random" %>
    <%
        Random random = new Random();%>
        <%-- 导入随机数--%><% 
        double Num = random.nextInt(50)+50;
    
     %>
     <h3>主页面产生的随机数如下:
     <%
         out.println(Num);
      %></h3>
      <jsp:include page="MyJsp3_2.jsp" flush="true">
      <jsp:param value="<%=Num %>" name="Num"/>
      </jsp:include>
      
      <%
          double num;
          String str=request.getParameter("Num");
          if(str != null)
                  try{
                      
                  }catch(NumberFormatException e){
                      e.printStackTrace();
                  }
       %>

</body>
</html>

MyJsp3_2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp3_2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%@page import="java.lang.Math" %>
    <%
        double num;num=0;
        String str2 = request.getParameter("Num");
        num = Double.parseDouble(str2);
        if(str2 != null){
            try{
             %><h3>主页面传过来的随机数如下:
             <%
                 out.println(" "+str2+" ");%></h3>
             <%
             }
             
                 catch(NumberFormatException e){e.printStackTrace();}
              %>    
        <% }%>
        <h1><%=num %>的平方根:<%=Math.sqrt(num) %>
        <br>被调用的页面</h1>

    
  </body>
</html>

  • 本题包括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页面能显示有关抛出的错误信息。(程序中使用的图片,可自行准备)
  • MyJsp4_1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp4_1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <!-- 表单页面 -->
    <p>输入1-100的整数并传递给下一个页面</p>
    <form action="MyJsp4_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="MyJsp4_2.jsp">
                    <jsp:param name="str" value="<%=num %>"/>
                    </jsp:forward>
                    
                    
                    <% }
                    else if(num>50 && num<=100){%>
                        <jsp:forward page="MyJsp4_3.jsp">
                        <jsp:param name="str" value="<%=num %>"/>
                        </jsp:forward>
                    <%}
                    else{%>
                        <jsp:forward page="MyJsp4_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>
    
  • MyJsp4_2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>2</title>


  </head>
  
  <body>
    <%
        String 	str2 = request.getParameter("number");
        
     %>
     <p>图像传过来的值为:<cite><%out.println(str2); %></cite></p>
     <p>图片规格:<cite><%out.println(str2); %></cite></p>
     <br><br>
     
     <img src="C:\Users\Pictures\Camera Roll\1.jpg" width="<%=str2%> height="<%=str2 %>"></img>
    
  </body>
</html>

  • MyJsp4_3.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp4_3.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
        String 	str2 = request.getParameter("number");
        
     %>
     <p>图像传过来的值为:<cite><%out.println(str2); %></cite></p>
     <p>图片规格:<cite><%out.println(str2); %></cite></p>
     <br><br>
     
     <img src="C:\Users\Pictures\Camera Roll\IMG_20180828_125707.jpg" width="<%=str2%> height="<%=str2 %>"></img>
    
  </body>
  </html>


  • MyJsp4_4.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp4_4.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
        String str4 = request.getParameter("number");
     %>   
     <h3>输入的数:<%out.println(str4); %>不在1-100范围内,重新输入</h3>
      <img src="C:\Users\Pictures\Camera Roll\IMG_20180828_135515.jpg" ></img>
  </body>
 </html>

  • error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <% 
        String str5 = request.getParameter("number");
        
     %>
     <h1>你所输入的<cite><%out.println(str5); %></cite>不符合要求,请重新输入1-100之内的数</h1>
  </body>
</html>

发布了21 篇原创文章 · 获赞 43 · 访问量 5012

猜你喜欢

转载自blog.csdn.net/weixin_42878211/article/details/104809029