Solved

Servlets - Radio button check uncheck validation?

Posted on 2000-05-16
3
560 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
ID: 2815017
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
ID: 2823174
thanks a lot sgoms! It works now!
0
 
LVL 5

Accepted Solution

by:
sgoms earned 100 total points
ID: 2823241
kewl..
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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