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

Thread: I have a problem in my JSP code

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

    Default I have a problem in my JSP code

    I have this code and I am displaying contents from database in the browser.In database I have questions for survey with options as answers.For each question users have to select any one of the four options.I am using radio buttons for getting user response.I am showing the output that I am getting


    tab.JPG


    The problem is that I want that users should be able to select only one option out of the 4 options for a question but this code is making me able to select all the four options for a question.Conversely it is allowing me select only one option for a column while it should allow me to select multiple.Like if you look at the first column of the options(3rd column from left) than it allows me select only one option throughout the column.I want to have this on the row and not column.Please can anyone help me with this?

    <%@ 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.*" %>
     
          <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);
     
          %>
     
          <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
     
          <%
     
          while (rs.next()) {
     
          %>
           <TR>
            <TD><%=rs.getString(1)%></TD>
            <TD><%=rs.getString(2)%></TD>
     
            <TD><input type='radio'name='radio1'><%=rs.getString(3)%></TD>
            <TD><input type='radio'name='radio2'><%=rs.getString(4)%></TD>
            <TD><input type='radio'name='radio3'><%=rs.getString(5)%></TD>
            <TD><input type='radio'name='radio4'><%=rs.getString(6)%></TD>
     
        </TR>                                                           
     
          <%   }   %>
     
     
          <% 
          rs.close();
     
          statement.close();
     
          connection.close();
     
          } catch (Exception ex) {
     
          %>
     
     
          <font size="+3" color="red"></font>
     
         <%   
     
         out.println("Unable to connect to database."); 
     
               }
           %>
     
     
     
          </TABLE>
     
     
     
          </body>
     
          </html>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I have a problem in my JSP code

    Is this a question about what HTML code to generate to get Radio buttons?

  3. #3
    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 have a problem in my JSP code

    Your loop makes 4 radio controls on a table row, with names coded from 1 to 4. Then it makes another row, using the same names. Radio controls with the same name run through columns of your HTML, not through table rows. You would be able to see that clearly if you posted the HTML output to the User-agent.

    Is that a Java Server Page? Nasty. It's like a combination of the worst parts of Java and PHP.

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

    Default Re: I have a problem in my JSP code

    Quote Originally Posted by Sean4u View Post
    Your loop makes 4 radio controls on a table row, with names coded from 1 to 4. Then it makes another row, using the same names. Radio controls with the same name run through columns of your HTML, not through table rows. You would be able to see that clearly if you posted the HTML output to the User-agent.

    Is that a Java Server Page? Nasty. It's like a combination of the worst parts of Java and PHP.


    That even I know why the problem is coming.I want to know how to correct it.I want to allow users to select one option from each row and not column.If you could correct my code for doing that it would be great

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I have a problem in my JSP code

    Can you create the HTML code you want with an editor so you know what needs to be generated
    and then change the jsp code to generate that desired HTML.

  6. #6
    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 have a problem in my JSP code

    That even I know why the problem is coming
    I'm at a loss to understand why you haven't fixed it in that case.

    You've got this:
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>

    output a row at a time by a while loop and you want this:

    <tr>1 1 1 1</tr>
    <tr>2 2 2 2</tr>
    <tr>3 3 3 3</tr>
    <tr>4 4 4 4</tr>

    Have a go at editing that loop yourself to get the numbers in the right place, and if it still doesn't work, post your latest code again.

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

    Default Re: I have a problem in my JSP code

    Quote Originally Posted by Sean4u View Post
    I'm at a loss to understand why you haven't fixed it in that case.

    You've got this:
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>
    <tr>1 2 3 4</tr>

    output a row at a time by a while loop and you want this:

    <tr>1 1 1 1</tr>
    <tr>2 2 2 2</tr>
    <tr>3 3 3 3</tr>
    <tr>4 4 4 4</tr>

    Have a go at editing that loop yourself to get the numbers in the right place, and if it still doesn't work, post your latest code again.
    This is my latest code snippet.I am only posting the while loop because prob is in that only.I have taken a variable int i and I am using it to name the radio buttons.But now it is only allowing me to select one option throughout the grid(means in all rows and columns).I think it is naming all the radio buttons with the same name.And there is one more thing.The number of rows in the table are not fixed.Even though the image is showing only 4 rows but in reality they can be of any number.Right now I am trying for 4 rows only.When the prob is solved for 4 rows it can be converted to any number of rows by getting the number of rows from database and storing it in i.Can you correct the code.


     
     <%int i=0; %>
     
          <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+1)'><%=rs.getString(3)%></TD>
            <TD><input type='radio' name='radio(i+1)'><%=rs.getString(4)%></TD>
            <TD><input type='radio' name='radio(i+1)'><%=rs.getString(5)%></TD>
            <TD><input type='radio' name='radio(i+1)'><%=rs.getString(6)%></TD>
     
        </TR>  
     
          <%i++; %>                                                      
     
          <%   }   %>
    Last edited by abhiM; August 12th, 2011 at 02:03 AM.

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

    Default Re: I have a problem in my JSP code

    Quote Originally Posted by Norm View Post
    Can you create the HTML code you want with an editor so you know what needs to be generated
    and then change the jsp code to generate that desired HTML.

    I am trying to do that only but every time I am getting logical error.I am not able to figure out the correct way.My latest code is posted above.

  9. #9
    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 have a problem in my JSP code

    That's more like it, though you should have included your declaration of 'i' in that code fragment. What you should really be doing here is changing your JSP, loading the page in your browser to test it, and when it doesn't work the way you expect, you should be looking at the HTML source of the page to see what your JSP has output. View your HTML source in your browser now - it's usually right-click somewhere on the page and 'View Source' or similar. Select, copy and paste the whole <table></table> that contains your radio controls. Your current JSP code looks to me like it should give you the right output, so we need to double-check the HTML source to see why it isn't working.

Similar Threads

  1. I have a problem with my code I think
    By byrne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 12th, 2011, 04:18 AM
  2. problem in my code
    By dhruvguys in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 05:18 AM
  3. What is the Problem With This Code?
    By olufemi1712 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2011, 09:05 AM
  4. Problem with code
    By lichad3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2011, 06:40 PM
  5. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM