将所选单选按钮的值从一个jsp传递到另一个jsp

时间:2018-01-05 13:22:41

标签: jsp

我试图将所选单选按钮的值从一个jsp页面传递到另一个jsp页面,但我的代码无效。 以下是我的代码: -

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style></style>
<% 
        int i=1;
        String url = "jdbc:mysql://127.0.0.1:3306/Quiz";
        String username1="root";
        String password1="root";
        PreparedStatement st = null;
        ResultSet results = null;

         int iQuestionID=3;
            try {

                Class.forName("com.mysql.jdbc.Driver");
                Connection con= DriverManager.getConnection(url,username1,password1);
                String sql="SELECT  Questions.Question, Questions.option1, Questions.option2,Questions.option3, Questions.option4,Questions.correctanswer FROM Questions order by RAND() limit ?";
                st = (PreparedStatement) con.prepareStatement(sql);
                 st.setInt(1,iQuestionID);
                results = st.executeQuery(); 

                while (results.next()) {
    String r1, r2, r3, r4, r5,r6;
    r1 = results.getString(1);
    r2 = results.getString(2);
    r3 = results.getString(3);
    r4 = results.getString(4);
    r5 = results.getString(5);
    r6 = results.getString(6);
%>

<br/>
<center>


<form name="form1"  method="post" action="Results.jsp" > 
<table border="1" width="500px" bgcolor="white" cellspacing="0" cellpadding="0">
<tr>
<td width="100%"> 



<h2 align="center"><font color="red">Online Quiz Application</font></h2>

<b>Select Correct Answer</b>
<table border="0" width="500px" cellspacing="2" cellpadding="4">
<tr>

<td width="50%"> Question:</td>
<td><input type="hidden" name="correctAns<%= i %>" value="<%= results.getString(6) %>" /></td>
</tr>
<tr>
<td><%= results.getString(1) %></td></tr>
<tr>
<td>

1: <input type="radio" name="a<%= i %>"  id="a<%= i %>" value="<%= results.getString(2) %>" /></td>
<td><%= results.getString(2) %></td></tr> 
<tr>
<td>
2: <input type="radio" name="a<%= i %>" id="a<%= i %>" value="<%= results.getString(3) %>" /></td>
<td><%= results.getString(3) %></td></tr>

<tr>
<td>
3: <input type="radio" name="a<%= i %>" id="a<%= i %>" value="<%= results.getString(4) %>" /></td>
<td><%= results.getString(4) %> </td></tr>

<tr>
<td>
4: <input type="radio" name="a<%= i %>" id="a<%= i %>" value="<%= results.getString(5) %>" /> </td>
<td> <%= results.getString(5) %> </td></tr>
</table>
</td>
</tr>

</table>


<br/>
            <% if (i==iQuestionID)
                        {
                    %>
    <INPUT type = "submit" name="addSubmit" value = "Press to Submit">
    <% } %>

</form>
</center>
<%

i++;
} 
results.close();
con.close();

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


    }finally {     
    } %>



</body>
</html>

然后在结果JSP中我有

<%
String id[]= new String [5];
for (int i=1;i<5; i++)
{
    id[i]=request.getParameter("a"+i);
    out.println(id[i]);
}

%>

但是在Results.jsp中,我得到的是空值。

我甚至尝试使用servlets代码。我能够显示测验的问题,但我无法获取所选单选按钮的值。

任何帮助纠正上述jsp代码,以便我可以获取所选单选按钮的值将非常感激。

由于

2 个答案:

答案 0 :(得分:0)

在Result jsp代码中尝试以下操作。 你将得到正确的无线电值。

<%
String id[]= new String [5];
for (int i=1;i<5; i++)
{
   if(request.getParameter("a"+i)!=null){
    id[i]=request.getParameter("a"+i);
    out.println(id[i]);
  }
}

%>

答案 1 :(得分:0)

希望这对您和其他人有用:

我将通过单选按钮和下拉按钮的示例来展示它,因为这两个按钮都用于从列表中选择单个响应: 文件:index.jsp

<form action="./StudentServlet" method="post">
    <div class="form-label-group">
        <label for="studentCourses">Student Course Group</label>
        <select id="studentCourses" name="studentCourses">
            <%for (int i = 1; i <= 6; i++) {%>
                <option value= "1ITF<%=i%>">1ITF<%=i%></option>
            <%}%>
        </select>
    </div>
    <div class="form-label-group">
        <label for="studentPreference">Preference at the moment:</label>
        <%String[] chosenArray = {"APP", "BIT", "EMDEV", "INFRA", "ANDR"};
      for (int j = 0; j < chosenArray.length; j++) {%>
        <p><input type="radio" name="studentPreference" value="<%=chosenArray[j]%>" id="<%=chosenArray[j]%>"/>
            <label for="<%=chosenArray[j]%>"><%=chosenArray[j]%></label> 
        </p>
      <%}%>
      <input type="submit" name="studentFormSubmit" id="studentFormSubmit">
    </div>
</form>

文件:StudentServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String studentCourse = request.getParameter("studentCourses");
    String studentPreference = request.getParameter("studentPreference");

    Student coursePreference= new Student(studentCourse, studentPreference);

    if (request.getParameter("studentFormSubmit") != null) {
        request.setAttribute("coursePreference", coursePreference);
        request.getRequestDispatcher("DisplayResult.jsp").forward(request, response);
    } 
}

并在文件DisplayResult.jsp

<h1>Course Details</h1>
<div>
    <%
    Student coursePreference= = (Student) request.getAttribute("coursePreference");
    %>

    <p>Student Course: <%=coursePreference.getStudentCourse()%></p>
    <p>Student Preference: <%=coursePreference.getStudentPreference()%></p>
</div>
相关问题