Solved

Servlets - Radio button check uncheck validation?

Posted on 2000-05-16
3
546 Views
Last Modified: 2013-11-24
Hello friends over this media! ,

I am stuck here with a servlet in which i am trying to generate a online quiz . I genearated radio buttons dynamically depending on the data from the database.i.e., there's a table which has 5 columns namely, question, choice1,choice2,  choice3,choice4. I am able to generate the buttons but am not able to read the value of the checked radio button. Though the default button is checked it's still not printing the value. I  have the HTML code inside this servlet itself. I have'nt written it separately because i have to generate radio buttons depending on the number of rows in the database. Please help me out....:-(! Here 's the code attached!

/***** part of dopost method**********/
/*Am using a MsqlJava package to connect and retrieve data from the
database. ***/
/***qans is the table ******/
public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {    

String row1[],row2[],row3[],row4[],row5[];

MsqlResult result1=msql.Query("select question from qans");
MsqlResult result2=msql.Query("select answer1 from qans");
MsqlResult result3=msql.Query("select answer2 from qans");
MsqlResult result4=msql.Query("select answer3 from qans");
MsqlResult result5=msql.Query("select answer4 from qans");

/***Just checking for one row fetched from the database*****/
if ((row1 = result1.FetchRow()) != null && (row2 = result2.FetchRow()) != null && (row3 = result3.FetchRow()) != null && (row4 = result4.FetchRow()) != null && (row5 = result5.FetchRow()) != null)
            {
            for(i=0; i < row1.length; i++)
            for(j=0; j < row2.length; j++)
            for(k=0; k < row3.length; k++)
            for(l=0; l < row4.length; l++)
            for(m=0; m < row5.length; m++)

toClient.println("<font size=3 color=navy>" + row1[i] + "<BR><BR><INPUT TYPE=RADIO NAME=" + a[inc] + " VALUE=" + row2[j] + " checked >" + row2[j] + "<BR><INPUT TYPE=RADIO NAME=" + a[inc] + " VALUE=" + row3[k] + ">" + row3[k] + "<BR><INPUT TYPE=RADIO NAME=" + a[inc] + " VALUE=" + row4[l] + ">" + row4[l] + "<BR><INPUT TYPE=RADIO NAME=" + a[inc] + " VALUE=" + row5[m] + ">" + row5[m] +"</font><br><br>");

      String value = request.getParameter("a[inc]");
                    toClient.println(value);
/**********Prints a null value when run**************/
            inc++;
            }
      
}
0
Comment
Question by:smithan
  • 2
3 Comments
 
LVL 5

Expert Comment

by:sgoms
Comment Utility
Seperate your logic..
/**GenerateServlet.java**/
have the servlet generate the form dynamically
in the form action method call a servlet called 'ProcessDataServlet' in that process ur answers.

GenerateHTMLServlet
->
<FORM NAME=Quiz ACTION="/servlet/ProcessDataServlet">
....

->
/**ProcessDataServlet.java**/
public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {    
  String ans=request.getParameter("Radio button name");

}


you cannot have the checking in the same method 'cos once the HTML response is sent to the browser the html page has to contact the servlet again for the data to be processed.
-sgoms
0
 

Author Comment

by:smithan
Comment Utility
thanks a lot sgoms! It works now!
0
 
LVL 5

Accepted Solution

by:
sgoms earned 100 total points
Comment Utility
kewl..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now