JSP第七次 作业

1.教材P78-79 例4-9

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

  </head>
  
  <body>
     <%
    double price=98.78;
    %>
   商品编号 A1001,价格 8765
   <a href="receive .jsp?id=A1001&price=8765">购买</a><br>
   商品编号 A1002,价格 <%=price %>
   <a href="receive .jsp?id=A1002&price=<%=price %>">购买</a><br>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    
    <title></title>
  </head>
  
  <body>
    <p style="font-family: :宋体;font-size: 36;color:blue">
     <% 
     request.setCharacterEncoding("utf-8");
     String id=request.getParameter("id");
       String price=request.getParameter("price");
     %>
     <b>商品编号:<%=id %><br>
                 商品价格:<%=price %>
    </p>
  </body>
</html>

 

 2.教材P97 实验2

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

  </head>
  
  <body>
     <form action="counter.jsp" method="post">
   输入运算数,选择运输符号:<br>
   <input type="text" name="num1" size="5"/>
   <select name="sel">
   <option selected="selected" value="+">加</option>
   <option value="-">减</option>
    <option value="*">乘</option>
     <option value="/">除</option>
   </select>
   <input type="text" name="num2" size="5"/><br>
   <input type="submit" name="submit" value="提交"/>
   </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  <title></title>
  </head>
  
  <body>
    <%
        request.setCharacterEncoding("utf-8");
        String num1 = request.getParameter("num1");
        String num2 = request.getParameter("num2");
        String sel = request.getParameter("sel");
        if (num1 == null || num1.length() == 0) {
            response.sendRedirect("input.jsp");
            return;
        } else if (num2 == null || num2.length() == 0) {
            response.sendRedirect("input.jsp");
            return;
        }
        try {
            double n1 = Double.parseDouble(num1);
            double n2 = Double.parseDouble(num2);
            double s = 0;
            if (sel.equals("+"))
                s = n1 + n2;
            else if (sel.equals("-"))
                s = n1 - n2;
            else if (sel.equals("*"))
                s = n1 * n2;
            else if (sel.equals("/"))
                s = n1 / n2;
            out.print(n1 + "" + sel + "" + n2 + "=" + s);
        } catch (Exception e) {
            out.print("输入错误");
        }
    %>
  </body>
</html>

 

 3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title></title>
  </head>
  
  <body>
  <%
       char shu[]={'0','1','2','3','4','5','6','7','8','9'};
       String ma="";
       for(int i=0;i<4;i++){
       ma=ma+shu[(int)(Math.random()*10)];
       }
   %>
   <form action="2.jsp" method="post" name="a">
           请输入账号:    <input name="name" type="text"/><br/>
           请输入密码:    <input name="password" type="password"/><br/>
           请输入验证码:<input name="number" type="text"/>
           <input type="text" readonly="readonly" name="yzm" value="<%=ma%>"/>
           <input type="hidden" name="yzm" value="<%=ma%>"/><br/>
           <input type="button" value="登录" onclick="login()"/>
      <script type="text/javascript">
         function login(){
            if(a.name.value==""){
               alert("账号不能为空!");
               return;
            }
             if(a.password.value==""){
               alert("密码不能为空!");
               return;
            }
            if(a.number.value==""){
               alert("验证码不能为空!");
               return;
            }
            a.submit();
         }
      </script>
   </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>2.jsp</title>
  </head>

  <body>
      <% 
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String number=request.getParameter("number");
        String yzm=request.getParameter("yzm");
        if(name.equals(password)&&number.equals(yzm)){
           request.getRequestDispatcher("3.jsp").forward(request, response);
        }else{
           request.getRequestDispatcher("4.jsp").forward(request, response); 
        }
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>3.jsp</title>
  </head>
  <body>
     <h3>登陆成功!</h3>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>4.jsp</title>
  </head>
  <body>
   <h3>登陆失败!</h3>
  </body>
</html>

 

 4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title></title>
  </head>
  
  <body>
  <%
       char shu[]={'0','1','2','3','4','5','6','7','8','9'};
       String ma="";
       for(int i=0;i<4;i++){
       ma=ma+shu[(int)(Math.random()*10)];
       }
   %>
   <form action="2.jsp" method="post" name="a">
           请输入账号:    <input name="name" type="text"/><br/>
           请输入密码:    <input name="password" type="password"/><br/>
           请输入验证码:<input name="number" type="text"/>
           <input type="text" readonly="readonly" name="yzm" value="<%=ma%>"/>
           <input type="hidden" name="yzm" value="<%=ma%>"/><br/>   
            是否注册会员: <input type="radio" name="huiyuan" value="yes"><input type="radio" name="huiyuan" value="no">否<br> 
        <input type="button" value="登录" onclick="login()"/>
      <script type="text/javascript">
         function login(){
            if(a.name.value==""){
               alert("账号不能为空!");
               return;
            }
             if(a.password.value==""){
               alert("密码不能为空!");
               return;
            }
            if(a.number.value==""){
               alert("验证码不能为空!");
               return;
            }
            a.submit();
         }
      </script>
   </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>2.jsp</title>
  </head>

  <body>
      <% 
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String number=request.getParameter("number");
        String yzm=request.getParameter("yzm");
        if(name.equals(password)&&number.equals(yzm)){
           request.getRequestDispatcher("3.jsp").forward(request, response);
        }else{
           request.getRequestDispatcher("4.jsp").forward(request, response); 
        }
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title>3.jsp</title>
</head>
<body>
    <h3>登录成功</h3>
    <%
    request.setCharacterEncoding("utf-8");
        String huiyuan = request.getParameter("huiyuan");
        if(huiyuan.equals("yes")){
            out.print("欢迎您注册为会员");
        }        
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>4.jsp</title>
  </head>
  <body>
   <h3>登陆失败!</h3>
  </body>
</html>

 

 5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。

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

  </head>
  
  <body>
<form  name="form" action="counter.jsp" method="post">
      输入数字N:<input type="text" name="number"><br>
    <input type="submit" value="提交" >    
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  <title></title>
  </head>
  
  <body>
    <%
    String number=request.getParameter("number");
    double n=Double.parseDouble(number);
    for(int i=0;i<n;i++){
    out.println("欢迎"+"<br>");
    }
     %>
  </body>
</html>

 

 6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title></title>
</head>
<body>
  <form action="2.jsp" method="post" name="form">
        <table>
            <tr>
                <td>账号</td>
                <td><input type="text" name="zh" />
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password" />
                </td>
            </tr>
            <tr>
                <td><input type="button" value="登录" onclick="login()" />
                </td>
            </tr>
        </table>
    </form>
 <script>
        function login() {
            if (form.zh.value == "") {
                alert("用户名不能为空!");
                form.zh.focus();
                return;
            }
            if (form.password.value == "") {
                alert('密码不能为空!');
                form.password.focus();
                return;
            }
            if(form.zh.value==form.password.value){
            form.submit();
            }else{
            alert('登陆失败');
            form.password.focus();
            return;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title></title>
</head>
<body>
<%
String zh=request.getParameter("zh");
 %>
    <form action="3.jsp" method="post">
        <input type="text" name="name" /> 
        <input type="submit" value="提交" />
        <input type="hidden" name="zh" value="<%=zh %>"/>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML >
<html>
  <head>
    <title></title>
  </head>
  <body>
  <%
  request.setCharacterEncoding("utf-8");
  String zh=request.getParameter("zh");
  String name=request.getParameter("name");
  out.print("用户姓名:"+name+"<br>"+"用户账号:"+zh);
  %>
  </body>
</html>

 

 

posted @ 2022-04-17 15:02  热血青年小刘  阅读(16)  评论(0编辑  收藏  举报