今天完成了javaweb的出题系统,比较简陋
jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加信息</title> <script> var result0=new Array(6); var result=new Array(6); var t=new Array(6); var num=0; function refreshPage() { location.reload(); } function operator(){ var m=Math.floor(Math.random()*4); switch(m){ case 0: return " + ";break; case 1: return " - ";break; case 2: return " * ";break; case 3: return " / ";break; default: return " + ";break; } } function makeTimu() { num++; var a = Math.floor(Math.random() * (100 - 1) + 1); var b = Math.floor(Math.random() * (100 - 1) + 1); var c = Math.floor(Math.random() * (100 - 1) + 1); var d = Math.floor(Math.random() * (100 - 1) + 1); var n = Math.floor(Math.random() * 3 + 2); var q; var p; var x; switch (n) { case 2: document.write(a); q=operator();document.write(q); document.write(b); t[num]=a+q+b; toString(t[num]); result[num]= eval(t[num]); break; default: case 3: document.write(a); q=operator();document.write(q); document.write(b); p=operator();document.write(p); document.write(c); t[num]=a+q+b+p+c; toString(t[num]); result[num]=eval(t[num]); break; case 4: document.write(a); q=operator();document.write(q); document.write(b); p=operator();document.write(p); document.write(c); x=operator();document.write(x); document.write(d); t[num]=a+q+b+p+c+x+d; toString(t[num]); result[num]=eval(t[num]); break; } console.log(t[num]); console.log(result[num]); } function charge(result){ for(var i=1;i<=5;i++){ if(result!=result[i]){ } } } function showWrong(){ result0[1] = document.getElementById("result1"); result0[2] = document.getElementById("result2"); result0[3] = document.getElementById("result3"); result0[4] = document.getElementById("result4"); result0[5] = document.getElementById("result5"); for(let i=1;i<=5;i++){ if(result0[i]!=result[i]){ document.write(t[i]); document.write("="); document.write(result[i]+"<br>"); } } } function submitButtonClick() { result0[1] = document.getElementById("result1"); result0[2] = document.getElementById("result2"); result0[3] = document.getElementById("result3"); result0[4] = document.getElementById("result4"); result0[5] = document.getElementById("result5"); // function creatProblemObject(expr,res) { // return { // expression: expr, // result: res // } // } // let problemList = []; // for (let i = 1; i <= 5; i++) { // problemList.push(creatProblemObject(t[i],result[i])); // } let xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST","addInfo",false); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); for (let i = 1; i <= 5; i++ ) { t[i] = t[i].replace("+","十"); } xmlHttp.send("expression1="+t[1] + "&expression2="+t[2] + "&expression3="+t[3] + "&expression4="+t[4] + "&expression5="+t[5] + "&result1="+result[1] + "&result2="+result[2] + "&result3="+result[3] + "&result4=" + result[4]+ "&result5="+result[5]); //xmlHttp.send("problemSetJson=" + JSON.stringify(problemList)); for (let i = 1; i <= 5; i++ ) { t[i] = t[i].replace("+","十"); } for(let i=1;i<=5;i++){ if(result0[i]!=result[i]){ let xmlHttp1 = new XMLHttpRequest(); xmlHttp1.open("POST","addInfoW",false); xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp1.send("expression="+t[i]+"&result="+result[i]); } } alert("提交成功"); } function timer(){ let timerContent = document.getElementById("timer"); let timeleft = 120; let timr = setInterval(function (){ timerContent.innerText = timeleft; timeleft--; if(timeleft<0){ clearInterval(timr); alert("时间到"); } },1000); } </script> </head> <body> <script> document.write(1+'、'); makeTimu(); </script> <label for="result1">=</label> <input type="text" id="result1" name="result1" required> <br> <script> document.write(2+'、'); makeTimu(); </script> <label for="result2">=</label> <input type="text" id="result2" name="result2" required> <br> <script> document.write(3+'、'); makeTimu(); </script> <label for="result3">=</label> <input type="text" id="result3" name="result3" required> <br> <script> document.write(4+'、'); makeTimu(); </script> <label for="result4">=</label> <input type="text" id="result4" name="result4" required> <br> <script> document.write(5+'、'); makeTimu(); </script> <label for="result5">=</label> <input type="text" id="result5" name="result5" required> <br> 剩余时间:<p id="timer"></p> <button onclick="submitButtonClick()">提交</button> <button onclick="refreshPage()">刷新</button> <button onclick="timer()">开始答题</button> <button onclick="showWrong()">查看错题</button> </body> </html> addWrongServl package com.example.demo; import com.google.gson.Gson; import java.io.IOException; import java.lang.reflect.Type; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/addInfoW") public class AddWrongServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Connection connection = null; try { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/class"; String user = "root"; String password = "123567"; Class.forName(driver); connection = DriverManager.getConnection(url, user, password); if (connection != null) { System.out.println("数据库连接成功!"); } else { System.out.println("数据库连接失败!"); } // Gson gson = new Gson(); // String problemSetJson = request.getParameter("problemSetJson"); // System.out.println(problemSetJson); // List<Map<String,String>> problemSet = new ArrayList<>(); // problemSet = gson.fromJson(problemSetJson, ((ArrayList<Map<String, String>>) problemSet).getClass()); // // for (Map<String,String> problem:problemSet) { // System.out.println(problem.getClass()); // System.out.println(problem); // } String insertSQL = "INSERT INTO wrong (expression,result) VALUES (?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertSQL); System.out.println(getClass()); String expression = request.getParameter("expression" ); String result = request.getParameter("result" ); System.out.println(expression); System.out.println(result); preparedStatement.setString(1, expression); preparedStatement.setString(2, result); preparedStatement.executeUpdate(); preparedStatement.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } AddInfoServlet package com.example.demo; import com.google.gson.Gson; import java.io.IOException; import java.lang.reflect.Type; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/addInfo") public class AddInfoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Connection connection = null; try { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/class"; String user = "root"; String password = "123567"; Class.forName(driver); connection = DriverManager.getConnection(url, user, password); if (connection != null) { System.out.println("数据库连接成功!"); } else { System.out.println("数据库连接失败!"); } final int n = 5; // Gson gson = new Gson(); // String problemSetJson = request.getParameter("problemSetJson"); // System.out.println(problemSetJson); // List<Map<String,String>> problemSet = new ArrayList<>(); // problemSet = gson.fromJson(problemSetJson, ((ArrayList<Map<String, String>>) problemSet).getClass()); // // for (Map<String,String> problem:problemSet) { // System.out.println(problem.getClass()); // System.out.println(problem); // } String insertSQL = "INSERT INTO timu (expression,result) VALUES (?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertSQL); System.out.println(getClass()); for (int i =1; i<= n; i++) { String expression = request.getParameter("expression" + Integer.toString(i)); String result = request.getParameter("result" + Integer.toString(i)); System.out.println(expression); System.out.println(result); preparedStatement.setString(1, expression); preparedStatement.setString(2, result); preparedStatement.executeUpdate(); } System.out.println(n + " 行数据已插入。"); preparedStatement.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
标签:Web,java,write,10.18,num,出题,println,import,document From: https://www.cnblogs.com/xuxingkai/p/17773423.html