Solved

Servlets - Radio button check uncheck validation?

Posted on 2000-05-16
3
547 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 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

12 Experts available now in Live!

Get 1:1 Help Now