如何在另一个jsp页面上获取所选的单选按钮值?

时间:2014-02-13 05:36:43

标签: java jsp

这是第1页。我在提交处理页面上的单选按钮值为NULL时遇到问题。

<html>
  <head>
    <title>LoginForm</title>
  </head>
<body >

<font size="20"><marquee behavior="alternate">Spice Digital</marquee></font>
  <h1>Login Page</h1>
    <h2>Signup Details</h2>
    <form action="LoginCheck.jsp" method="post">
      <br/>Username:<input type="text" name="username">
      <br/>Password:<input type="password" name="password">

      <br/>select your gender:
      <select name=dropdown>
        <option name=one value=one> Male </option>
        <option name=two value=two> Female </option>
      </select>

      <br/>select your department:
      <input type="radio" name=myradio value="1"/>MCA
      <input type="radio" name=myradio value="2"/>B.Tech
      <input type="radio" name=myradio value="3"/>Other

      <br/> select your choices:
      <input type="checkbox" name=mybox value="1"/>java
      <input type="checkbox" name=mybox value="2"/>c++
      <input type="checkbox" name=mybox value="3"/>c
      <input type="checkbox" name=mybox value="4"/>sql

      <br/>enter you comments here:
      <textarea name=mytextarea cols=20 rows=5>
      </textarea>

      <br/><input type="submit" value="Login">
    </form>
  </body>
</html>

这是第2页。我无法在此处获取所选值。我使用request.getParameter()错了吗?

<html>
  <head>
    <title>JSP Page</title>
  </head>
<body BACKGROUND="C:\Documents and Settings\temp-      00940\workspace\login\WebContent\background.jpg">      
    <br/><br/><br/><br/><br/>
    <form action="LoginForm.jsp"  method="get">
      <font size="20"><marquee>Spice Digital </marquee></font>
        <h2>

        <%          
        String a=session.getAttribute("username").toString();
        out.println("Welcome "+a+"..!!");
        %>
        <br>
        <%
        String b=request.getParameter("myradio");
        out.println("Your department is "+b);
        %>
        <br/>
        <%
        String c=request.getParameter("mybox");
        out.println("Your choice is "+c);
        %>
        </h2>
        <br/>
        <a href="http://www.Google.com">Click here to google search</a>
        <br/>
        <br/><br/>
        <br/><br/><br/>
     <a href="Logout.jsp">LogOut</a>
   </form>
  </body>
</html>

4 个答案:

答案 0 :(得分:1)

<html>
<head>
<title>Example</title>
</head>
<body>

<form name="form-name" action="LoginCheck.jsp" method="post">

     <input type="radio" name="myradio" value="1" checked="checked"/>MCA
     <input type="radio" name="myradio" value="2"/>B.Tech
     <input type="radio" name="myradio" value="3"/>Other

    <input name="goto" type="submit" value="Login"> 
</form>

</body>
</html      

获取请求值:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
    String myRadio= request.getParameter("myradio");
%>

要确定选择了哪个按钮,请使用request.getParameter("myradio")

对于价值处理:

if ("1".equals(myRadio)) {
   // processing here       
}

答案 1 :(得分:1)

我在日食上工作得很好。 为简单起见,修改了一些代码。

如果没有将数据路由到其他页面,那么在这里你应该有你的JSP文件的名称,你的数据应该显示出来。

以下是修改后的代码。

<强>的index.jsp

<html>
<head>
    <title>LoginForm</title>
</head>
<body >

<font size="20"><marquee behavior="alternate">Spice Digital</marquee></font>
    <h1>Login Page</h1>
        <h2>Signup Details</h2>
        <form action="new.jsp" method="post">
        <br/>Username:<input type="text" name="username">
        <br/>Password:<input type="password" name="password">

        <br/>select your gender:
        <select name=dropdown>
        <option name=one value=one> Male </option>
        <option name=two value=two> Female </option>
        </select>

        <br/>select your department:
        <input type="radio" name=myradio value="1"/>MCA
        <input type="radio" name=myradio value="2"/>B.Tech
        <input type="radio" name=myradio value="3"/>Other

        <br/> select your choices:
        <input type="checkbox" name=mybox value="1"/>java
        <input type="checkbox" name=mybox value="2"/>c++
        <input type="checkbox" name=mybox value="3"/>c
        <input type="checkbox" name=mybox value="4"/>sql

        <br/>enter you comments here:
        <textarea name=mytextarea cols=20 rows=5>
        </textarea>


        <br/><input type="submit" value="Login">
        </form>
</body>
</html>

<强> new.jsp

<html>
<head>
    <title>JSP Page</title>
</head>
<body BACKGROUND="C:\Documents and Settings\temp-00940\workspace\login\WebContent\background.jpg">      
    <br/><br/><br/><br/><br/>
    <form action="LoginForm.jsp"  method="get">
      <font size="20"><marquee>Spice Digital </marquee></font>
        <h2>

        <%          
       // String a=session.getAttribute("username").toString();
      //  out.println("Welcome "+a+"..!!");
        %>
        <br>
        <%
        String b=request.getParameter("myradio");
        out.println("Your department is "+b);
        %>
        <br/>
        <%
        String c=request.getParameter("mybox");
        out.println("Your choice is "+c);
        %>
        </h2>
        <br/>
        <a href="http://www.Google.com">Click here to google search</a>
        <br/>
        <br/><br/>
        <br/><br/><br/>
    <a href="Logout.jsp">LogOut</a>
 </form>
</body>
</html>

问题可能在于路由,因为您首先将表单发送到logincheck.jsp然后发送到您想要的jsp。

答案 2 :(得分:0)

我猜是因为你没有在引号中指定name属性的值,

名称正在显示,因为您将其放在引号内

 <br/>Username:<input type="text" name="username">

所以试试这个,

select your gender:
        <select name="dropdown">
        <option name="one" value="one> Male </option>
        <option name="two" value="two"> Female </option>
        </select>

        <br/>select your department:
        <input type="radio" name="myradio" value="1"/>MCA
        <input type="radio" name="myradio" value="2"/>B.Tech
        <input type="radio" name="myradio" value="3"/>Other

        <br/> select your choices:
        <input type="checkbox" name="mybox" value="1"/>java
        <input type="checkbox" name="mybox"value="2"/>c++
        <input type="checkbox" name="mybox"value="3"/>c
        <input type="checkbox" name="mybox"value="4"/>sql

        <br/>enter you comments here:
        <textarea name="mytextarea" cols=20 rows=5>
        </textarea>

希望它有所帮助!!

答案 3 :(得分: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>