我正在开发servlet和jsp的在线考试系统

时间:2013-04-24 17:35:22

标签: servlets

我正在开发servlet中的在线考试应用程序。当我用getParamter( - )方法读取单选按钮值时,它显示/返回“on”是某些参数名称的参数值。它会随机改变它们的值。

例如,首先我们要求它显示2,5个单选按钮问题的“on”值。 第二次显示“开启”7,4个单选按钮问题。

请验证我的代码并告诉我我的错误

代码

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ExamSrv extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    int j = 1;
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");
    Connection con = null;
    ResultSet resultSet = null;
    Statement st = null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:accdsn");
      st = con.createStatement();
      resultSet = st.executeQuery("select qno,question,option1,option2,option3,option4 from Level1 where qno<=10");
      pw.println("<form action=ansurl name=ans method=get>");
      while (resultSet.next()) {

        int k = resultSet.getInt(1);

        pw.println(k + ").");
        pw.println("<b><font size=3>" + resultSet.getString(2) + "</font></b><br><br>");
        pw.println("<input type=radio name=opt" + j + " value=1 unchecked/>a)<b>" + resultSet.getString(3) +
                   "</b> &nbsp;&nbsp;");
        pw.println("<input type=radio name=opt" + j + " value =2 />b)<b>" + resultSet.getString(4) +
                   "</b> &nbsp;&nbsp;");
        pw.println("<input type=radio name=opt" + j + " vaule=3 />c)<b>" + resultSet.getString(5) +
                   "</b> &nbsp;&nbsp;");
        pw.println("<input type=radio name=opt" + j + " value=4 />d)<b>" + resultSet.getString(6) +
                   "</b><br><br><br> ");

        j++;


      }
      pw.println("<input type=submit name=submit value=submit>");
      pw.println("</form>");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    doGet(req, res);
  }
}

但是我将表单方法修改为“POST”,它在浏览器上显示不支持发布..

阅读时

public class AnsSrv extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    int j = 1, cnt = 0, fal = 0;
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");
    Connection con = null;
    ResultSet resultSet = null;
    Statement st = null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:accdsn");
      st = con.createStatement();
      resultSet = st.executeQuery("select answer from Level1 where qno<=10");

      while (resultSet.next()) {
        if (req.getParameter("opt" + j).equals(resultSet.getString(1))
          {
           pw.println("opt"+j+"<br>"); //here it shows "on " value as on browser window
          cnt++;
        else
          fal++;
        j++;
      }

      pw.println("your marks are:" + (cnt - fal));

    } catch (Exception e) {
      e.printStackTrace();
    }


  }

  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    doGet(req, res);
  }

}

1 个答案:

答案 0 :(得分:1)

while(resultSet.next())
{
           if(req.getParameter("opt"+j).equals(resultSet.getString(1)))
                 cnt++;
              else
              {            // block only if you wont to execute both the statements.
                fal++;
                j++;
              }


}
<=>在Java ==中,它不比较字符串的值。它比较了对象的引用。 所以使用equals方法