簡體   English   中英

如何在另一個 jsp 頁面上獲取選定的單選按鈕值?

[英]How can I get the selected radio button value on another jsp page?

這是第一頁。 我在提交處理頁面上遇到了單選按鈕值為 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>
<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       
}

它在 eclipse 上對我來說很好用。 為簡單起見,稍微修改了代碼。

如果沒有將數據路由到其他頁面,那么在此您應該有您的數據應該顯示的 JSP 文件的名稱。

這是修改后的代碼。

索引.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>

新建.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。

我猜是因為您沒有在引號中指定 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>

希望能幫助到你 !!

我的回答可能對其他人有用:

我將用一個單選按鈕和下拉按鈕的例子來展示它,因為它們都用於從列表中選擇一個單一的響應:文件: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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM