软件工程个人作业——四则运算

<%@ 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>主页</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>
    <form id="form1" name="form1" method="post" action="servlet/YunServlet?method=add">
  <p>
    <label for="name">要做的题数</label>
    <input type="text" name="num" id="num" />
  </p>
      
     <p>
    <input type="submit" name="tijiao" id="tijiao" value="提交" />
  </p>
</form>
  </body>
</html>
首页
<%@ page language="java" import="java.util.*,java.sql.*" 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>做题页</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>
   <form id="form1" name="form1" method="post" action="MyJsp1.jsp">
  <%
      dao.DBUtil db=new dao.DBUtil();
      Connection conn =db.getConn();
        Statement stmt = conn.createStatement();//从数据库里面取东西对比    
        ResultSet rs=stmt.executeQuery("select * from yunsuan");
        int x=0;
        while(rs.next())
        {
            x++;
            int num1=rs.getInt("num1");
            int num2=rs.getInt("num2");
            String symbol=rs.getString("symbol");
   %>
   <p>
           <label for="name"><%=num1+symbol+num2 %>=</label>
        <input type="text" name="<%=x %>" id="<%=x %>" />
           </p>
   <%
           }
    %>
    <p>
    <input type="submit" name="tijiao" id="tijiao" value="提交" />
    </p>
  </body>
</html>
出题页
<%@ page language="java" import="java.util.*,java.sql.*" 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>答案页</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>
  <% 
          dao.DBUtil db=new dao.DBUtil();
          Connection conn =db.getConn();
        Statement stmt = conn.createStatement();//从数据库里面取东西对比    
        ResultSet rs=stmt.executeQuery("select * from yunsuan");
        int x=0;
        while(rs.next())
        {
            x++;
            String y=String.valueOf(x);
            int num1=rs.getInt("num1");
            int num2=rs.getInt("num2");
            String symbol=rs.getString("symbol");
            int find=rs.getInt("find");
            if(request.getParameter(y)!=null)
            {
                int a=Integer.parseInt(request.getParameter(y));
   %>
   <p>
           <label for="name"><%=num1+symbol+num2 %>=</label>
        <table width="75" border="1">
          <tr>
        <td><%=a %></td>
          </tr>
        </table>
        <%
            if(find==a)
            {
         %>
         回答正确
         <%
             }
             else
             {
          %>
          回答错误,正确答案为<%=find %>
          <%
              }
           %>
           </p>
   <%
           }
           else
           {
               out.print("出错");
           }
           
           }
           
    %>
    <p>
  </body>
</html>
答案页
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Random;
import java.util.Scanner;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Dao;
import user.users;

public class YunServlet extends HttpServlet {
    /**
         * Constructor of the object.
         */
    public YunServlet() {
        super();
    }

    /**
         * Destruction of the servlet. <br>
         */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }

    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        dao.DBUtil db=new dao.DBUtil();
        Connection conn =db.getConn();
        Dao open=new Dao();
        int num=Integer.parseInt(request.getParameter("num"));
        //HttpSession session=request.getSession(); 
        //session.setAttribute("num", num);
            Random r=new Random();
            for(int i=0;i<num;i++)
            {
                users u=new users();
                int r1=r.nextInt(100)+1;
                int r2=r.nextInt(100)+1;
                int r3=r.nextInt(4)+1;
                switch(r3)
                {
                    case 1:
                    {
                        int x=r1+r2;
                        while(x>100&&x>0)
                        {
                            r1=r.nextInt(100)+1;
                             r2=r.nextInt(100)+1;
                             x=r1+r2;
                        }
                        u.num1=r1;
                        u.num2=r2;
                        u.symbol="+";
                        u.find=x;
                        
                        try {
                                open.add(conn, u);
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                        break;
                    }
                    case 2:
                    {
                        int x=r1-r2;
                        while(x>100&&x>0)
                        {
                            r1=r.nextInt(100)+1;
                             r2=r.nextInt(100)+1;
                             x=r1-r2;
                        }
                        u.num1=r1;
                        u.num2=r2;
                        u.symbol="-";
                        u.find=x;
                        try {
                                open.add(conn, u);
                        } catch (SQLException e) {
                    // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                    }
                    case 3:
                    {
                        int x=r1*r2;
                        while(x>100&&x>0)
                        {
                            r1=r.nextInt(100)+1;
                             r2=r.nextInt(100)+1;
                             x=r1*r2;
                        }
                        u.num1=r1;
                        u.num2=r2;
                        u.symbol="*";
                        u.find=x;
                        try {
                            open.add(conn, u);
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                    }
                    case 4:
                    {
                        int x=r1/r2;
                        int root=r1%r2;
                        while(x>100&&root==0&&x>0)
                        {
                            r1=r.nextInt(100)+1;
                             r2=r.nextInt(100)+1;
                             x=r1/r2;
                             root=r1%r2;
                        }
                        u.num1=r1;
                        u.num2=r2;
                        u.symbol="/";
                        u.find=x;
                        try {
                            open.add(conn, u);
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
            request.getRequestDispatcher("../MyJsp.jsp").forward(request,response);
        }


    /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
    public void init() throws ServletException {
        // Put your code here
    }
}
数据写入

以上是四则运算的源码

设计思想:

通过网页输入做题数,和时间,提交到下一个界面。servlet类生成数据存入数据库。做题页提交后到下一个页面显示进行判断对错。

原文地址:https://www.cnblogs.com/aishanyishi/p/7954329.html