learner
learner

Reputation: 99

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

This is the 1st page. I'm facing problem with the radio button value being NULL on the submission handling page.

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

This is the 2nd page. I'm unable to get the selected value here. Am I using request.getParameter() wrong?.

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

Upvotes: 0

Views: 73131

Answers (4)

Ali Safari
Ali Safari

Reputation: 1775

My answer may be of use for others:

I will show it with an example for both radio and drop-down buttons as both are used to select a singular response from a list: File: 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>

File: 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);
    } 
}

And in file 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>

Upvotes: 0

Santhosh
Santhosh

Reputation: 8197

i guess its because you have not specified the value for name attribute within quotes ,

Name is getting displayed because you have it inside quotes,

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

So try this,

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>

Hope it helps !!

Upvotes: 0

Harsh
Harsh

Reputation: 1695

It is working fine with me on eclipse. Modified the code a little for simplicity.

If not routing data to some other page then In in this you should have the name of your JSP file which your data should get displayed.

Here is the modified code.

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>

problem could be with you routing as you are sending your form first to logincheck.jsp then to your desired jsp.

Upvotes: 1

user1134181
user1134181

Reputation:

<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      

Fetching request values:

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

To determine which button is selected, use request.getParameter("myradio")

For values processing:

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

Upvotes: 1

Related Questions