Solved

Servlets - Radio button check uncheck validation?

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

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 61
egit plugin on eclipse 8 63
factorial example 4 40
servlet example 17 32
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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