jsp实验报告(2)

题目1:编写两个JSP页面input.jsp和result.jsp。input.jsp页面提交一个数字给result.jsp页面,result.jsp页面使用response对象做出动态响应。
input.jsp的具体要求:
input.jsp提供表单,用户在表单中输入一个数字,提交给result.jsp页面。
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页面。
input.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 'input.jsp' starting page</title>
  </head>
  
  <body>
    <form action="result.jsp" method="get" name="form">
        please input number:<input type="text" name="name" ><br><br>
        <input type="submit" name="submit" value="submit">
        
    </form>
  </body>
</html>

result.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 >
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'result.jsp' starting page</title>

  </head>
  
  <body>

   <%
       //接收提交得数字
       String str = (String)request.getParameter("name");
       
       try{
       if(str != null){
        //将接收到得数字由string类型转换为double类型
       double value = Double.parseDouble(str) ;
           if(value<0){
               out.print("The number you submitted is :"+ value);
               //将contentType属性值设置为text/plain
               response.setContentType("text/plain;charset=GB2312");
               out.print("The square of the number is :"+ value*value);
           }
           else if(value>=0 && value<100){
               out.print("The number you submitted is :"+ value);
               //将contentType得属性值设置为application/msword
               response.setContentType("application/msword;charset=GB2312");
               out.print("The cube of the number is :"+value*value*value);          
           }
           else if(value>100){           
                response.setStatus(404);            
               }
           }
         } catch(Exception e){
            response.sendRedirect("input.jsp");
        }
    %>
  </body>
</html>

题目2:猜数字游戏。本题包括4个JSP程序,inputGuess.jsp、resultGuess.jsp、small.jsp、large.jsp和success.jsp。
inputGuess.jsp的具体要求如下:
用户请求inputGuess.jsp时,随机分配给该用户一个1到100之间的数。该页面同时负责将这个数字存在用户的session对象中。该页面提供表单,用户可以使用该表单输入自己的猜测,并提交给resultGuess.jsp页面。
resultGuess.jsp的具体要求如下:
resultGuess.jsp页面负责判断inputGuess.jsp提交的猜测数字是否和用户的session对象中存放的那个数字相同,如果相同就将用户重定向到success.jsp;如果不相同就将用户重定向到large.jsp或small.jsp。
small.jsp和large.jsp的具体要求如下:
small.jsp和large.jsp页面提供表单,用户可以使用该表单继续输入自己的猜测,并提交给result.jsp页面。
success.jsp的具体要求如下:
success.jsp页面负责负责显示用户成功的消息,并负责输出用户session对象中的数据。
inputGuess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'inputGuess.jsp' starting page</title>
    <script type="text/javascript">
    //设置一个js函数来进行判定输入得数是否合法合规
                window.onload = function(){
                //获取a
				 var a = document.getElementById("a");
				 //为a绑定一个单击响应函数,判断输入是否合规
                  a.onclick = function(){
                  if(document.form.name.value == "" || isNaN(document.form.name.value)){
                    alert("必须输入一个数字");
                    document.form.name.focus();
                    return false;
                    }
                };       
			};        
        
    </script>    
  </head>
  
  <body>
  <h1>随机生成一个1-100之间得数字</h1>
  <%
      int number = (int)(Math.random()*100)+1;
      //建立一个session变量记录次数
      session.setAttribute("count", new Integer(0));
      //建立保存生成数字得session变量
      session.setAttribute("save",new Integer(number));
   %>
   <form action="resultGuess.jsp" method="post" name="form">
       <p>请输入你认为正确得答案</p>
       <input type="text" name="name" >
       <input type="submit" id="a" name="submit" value="进行验证" >
   
   </form>
   <p>正确答案:<cite><%=number %></cite></p>
   
  </body>
</html>

small.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>My JSP 'large.jsp' starting page</title>
  </head>  
  <body>
    <form action="resultGuess.jsp" method="post" name="form">
        <h1>猜得太小啦~ ~ ~</h1>
        <br><br>
        
        <input type="text" name = "name">
        <input type="submit" name="submit" value="再次猜测">
    </form>
  </body>
</html>

large.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>My JSP 'large.jsp' starting page</title>
  </head>  
  <body>
    <form action="resultGuess.jsp" method="post" name="form">
        <h1>猜得太大啦~ ~ ~</h1>
        <br><br>
        
        <input type="text" name = "name">
        <input type="submit" name="submit" value="再次猜测">
    </form>
  </body>
</html>

resultGuess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>My JSP 'resultGuess.jsp' starting page</title>
  </head>
  
  <body>
    <%
        String str = request.getParameter("name");
        if(str != null){
            int guessNumber = -1;
            try{
                guessNumber = Integer.parseInt(str);
                Integer integer = (Integer)session.getAttribute("save");
                int number_real = integer.intValue();
                //进行判断
                if(guessNumber == number_real){
                    int counts = ((Integer)session.getAttribute("count")).intValue();
                    counts+=1;
                    session.setAttribute("count",new Integer(counts));
                    response.sendRedirect("sucess.jsp"); 
                }
                else if(guessNumber < number_real){
                    int counts = ((Integer)session.getAttribute("count")).intValue();
                    counts+=1;
                    session.setAttribute("count",new Integer(counts));
                    response.sendRedirect("small.jsp");
                }
                else if(guessNumber > number_real){
                    int counts = ((Integer)session.getAttribute("count")).intValue();
                    counts+=1;
                    session.setAttribute("count",new Integer(counts));
                    response.sendRedirect("large.jsp");
                }
            
            }catch(Exception e){
                response.sendRedirect("inputGuess.jsp");
            }
        }
     %>
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'sucess.jsp' starting page</title>
  </head>
  <body>
      <%
          int count = (Integer)session.getAttribute("count");
          int num = (Integer)session.getAttribute("save");
       %>
       <h1>恭喜!!!回答正确,赏黄金万两,美人三千!!!</h1>
       <br>
       <p>共猜测<%=count %>次,答案为:<%=num %>,继续加油</p>
       <%
           session.invalidate();
        %>       
        <a href="inputGuess.jsp">再来一局</a>
    </body>
</html>

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页