【问题标题】:Passing the value of selected radio buttons from one jsp to another将选定单选按钮的值从一个 jsp 传递到另一个
【发布时间】:2018-01-05 13:22:41
【问题描述】:

我正在尝试将选定单选按钮的值从一个 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>

然后在Results 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 中,我得到的是空值。

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

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

谢谢

【问题讨论】:

    标签: jsp


    【解决方案1】:

    尝试在 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]);
      }
    }
    
    %>
    

    【讨论】:

    • 不客气。你能把我的答案标记为绿色吗?
    【解决方案2】:

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

    我将通过单选按钮和下拉按钮的示例来展示它,因为两者都用于从列表中选择单个响应: 文件: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>
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2019-09-09
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多