-1

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

例如,当我们首先要求它显示单选按钮的 2,5 个问题的“on”值时。第二次显示“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);
  }

}
4

1 回答 1

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方法

于 2013-04-24T18:02:39.783 回答