Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: I am unable to read radio button actions in JSP

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I am unable to read radio button actions in JSP

    I have a set of radio buttons.These radio buttons are associated with some questions for survey.I also have a Submit button.I want that as soon as I click the Submit button, the application should read the status of radio buttons.But I am unable to figure out a way of doing this.My code is shown bellow.I am also posting a snapshot of the screen that I am getting when I run this code(Just for little help in understanding).Please help me with providing code for doing this.

    pic1.JPG


    <%@ 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">
     
          <%@ page import="java.sql.*" %>
     
          <%@ page import="java.io.*" %>
           <%@ page import="javax.swing.*" %>
       <%@ page import="java.awt.*" %>
     
     
          <html>
     
          <head>                                                
     
          <title>display</title>
     
          </head>
     
          <body>
     
          <h2>Data</h2>
     
          <%
     
          try {
     
          Connection connection = null;
     
          Statement statement = null;
     
          ResultSet rs = null;
        System.out.println("loading class") ;
     
          Class.forName("com.ibm.db2.jcc.DB2Driver");
     
    System.out.println("after loading class"); 
     
          connection  = DriverManager.getConnection("jdbc:db2://localhost:50000/Simple","administrator","welcome2sa2");
      System.out.println("after conncetion class");
     
          System.out.println(connection.getClass());
     
     
          statement = connection.createStatement();
     
     
     
          String QueryString = "select * from question";
     
          rs = statement.executeQuery(QueryString);
     
          int i=1;
     
          %>
     
          <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
     
          <%
     
          while (rs.next()&&(i<=4)) {
     
          %>
           <TR>
            <TD><%=rs.getString(1)%></TD>
            <TD><%=rs.getString(2)%></TD>
     
            <TD><input type='radio' name="radio<%= i %>"><%=rs.getString(3)%></TD>
            <TD><input type='radio' name="radio<%= i %>"><%=rs.getString(4)%></TD>
            <TD><input type='radio' name="radio<%= i %>"><%=rs.getString(5)%></TD>
            <TD><input type='radio' name="radio<%= i %>"><%=rs.getString(6)%></TD>
     
        </TR>  
     
          <%i++; %>                                                      
     
          <%   }   %>
     
     
          <% 
          rs.close();
     
          statement.close();
     
          connection.close();
     
          } catch (Exception ex) {
     
          %>
     
     
          <font size="+3" color="red"></font>
     
         <%   
     
         out.println("Unable to connect to database."); 
     
               }
           %>
     
     
     
          </TABLE>
     
     
           <script type="text/javascript">
          function onsubmit()
          {
     
     
          document.write("Submitting");
     
          	return true;
          }
           </script>
     
     
     
     
          <form name="myform">
      <input type="submit" name="submit" value="Submit" align="middle" onclick="onsubmit();">
      </form>
     
     
     
          </body>
     
          </html>


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I am unable to read radio button actions in JSP

    There's quite a bit missing. What you need to do is to make an HTML form:
    Forms in HTML documents
    Bookmark that page and read it often.

    Your form element should contain your input controls. You need to re-arrange your code so that the table containing your radio controls is enclosed by the form element. Choose a 'method' for your form - 'get' (the default) can be handy while you're developing because when you submit the form, the data is 'sent' back to the server as URL parameters so you can just eyeball the URL your browser uses to check to see if everything is working ok. It might look ugly in production. 'post' is usually more semantically correct (something is usually updated on the server).

    Once you've changed your page so that it reliably sends the right data back to the server as URL parameters, you'll need to add some code to validate submitted form data before you build your page so that you can either update your database and (usually) redirect to a form-submitted-correctly page, or build the page with some indication of potential problems (user didn't complete all questions, for example).

    I don't know JSP, but it looks like you're using it in an old-school way in which there should be some way of getting hold of a 'request' object which in turn would have a method for returning the values of named parameters in the current URL.

    edit: this page looks as though it has everything you need:
    http://www.roseindia.net/jsp/RequestObjectInJSP.shtml
    I do something very similar to handle form data on my platform, but I usually use only one page / server-side entity to produce the original form and handle the input. That's a matter of taste, I guess.
    Last edited by Sean4u; August 16th, 2011 at 04:33 AM.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am unable to read radio button actions in JSP

    Quote Originally Posted by Sean4u View Post
    There's quite a bit missing. What you need to do is to make an HTML form:
    Forms in HTML documents
    Bookmark that page and read it often.

    Your form element should contain your input controls. You need to re-arrange your code so that the table containing your radio controls is enclosed by the form element. Choose a 'method' for your form - 'get' (the default) can be handy while you're developing because when you submit the form, the data is 'sent' back to the server as URL parameters so you can just eyeball the URL your browser uses to check to see if everything is working ok. It might look ugly in production. 'post' is usually more semantically correct (something is usually updated on the server).

    Once you've changed your page so that it reliably sends the right data back to the server as URL parameters, you'll need to add some code to validate submitted form data before you build your page so that you can either update your database and (usually) redirect to a form-submitted-correctly page, or build the page with some indication of potential problems (user didn't complete all questions, for example).

    I don't know JSP, but it looks like you're using it in an old-school way in which there should be some way of getting hold of a 'request' object which in turn would have a method for returning the values of named parameters in the current URL.

    edit: this page looks as though it has everything you need:
    Request Object In JSP
    I do something very similar to handle form data on my platform, but I usually use only one page / server-side entity to produce the original form and handle the input. That's a matter of taste, I guess.


    I will definitely keep the radio button in form syntax.But right now I m trying to configure the submit button.I am trying that as soon as submit is clicked it will transfer to a new file which I am taking as servlet.But in the program when I am clicking the submit it is only refreshing the existing page.My code for submit button is given below

          <form name="myform" action="Collect.java" method="post">
      <input type="submit" name="submit" value="Submit" align="middle" >
      </form>


    I am not understanding why it is not transferring to the servlet.I read somwhere that you need to create a web.xml file for matching.Is it required?What do we have to write in web.xml?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I am unable to read radio button actions in JSP

    This is why I mentioned 'get' and 'post' in my previous comment. When you 'post' a form from a web browser, you send a request to the bare URL specified in the FORM's ACTION attribute, but with HTTP request content:
    HTTP/1.1: HTTP Message
    Your form controls will be encoded and sent as parts of the HTTP request body.
    If you use 'get' as your form method, you'll see your controls' data added to the URL as parameters ('query'). Try that and let us know how you get on.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I am unable to read radio button actions in JSP

    One other thing I notice from your previous post is the fragment of Java code. If you have a FORM element with only a submit button in it, nothing (except perhaps the name of the submit button) will be sent to the server because the form is empty. What you should have is
          <form name="myform" action="Collect.java" method="get">
    <table>
    <tr><!-- radio buttons for one question --></tr>
    <tr><!-- radio buttons for another question --></tr>
    </table>
      <input type="submit" name="submit" value="Submit" align="middle" >
      </form>
    ... like that

  6. The Following User Says Thank You to Sean4u For This Useful Post:

    abhiM (August 17th, 2011)

  7. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am unable to read radio button actions in JSP

    Quote Originally Posted by Sean4u View Post
    One other thing I notice from your previous post is the fragment of Java code. If you have a FORM element with only a submit button in it, nothing (except perhaps the name of the submit button) will be sent to the server because the form is empty. What you should have is
          <form name="myform" action="Collect.java" method="get">
    <table>
    <tr><!-- radio buttons for one question --></tr>
    <tr><!-- radio buttons for another question --></tr>
    </table>
      <input type="submit" name="submit" value="Submit" align="middle" >
      </form>
    ... like that







    Thanks! Now its working fine.It is going to the servlet page on clicking the submit button.Thanks once again

Similar Threads

  1. radio button with spinner to create an answer in a textarea box
    By dekon in forum Java Theory & Questions
    Replies: 7
    Last Post: August 12th, 2011, 01:36 AM
  2. Java - Button Actions on Form --Please help
    By Cami7 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 24th, 2011, 01:31 PM
  3. need help Coding RADIO BUTTON for REMEMBER ME
    By timosoft in forum Java Theory & Questions
    Replies: 3
    Last Post: February 4th, 2011, 05:19 PM
  4. Adding Radio Button Values
    By bazazu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 13th, 2010, 10:41 AM
  5. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM

Tags for this Thread