?
Solved

loops and printouts on servlets

Posted on 2005-03-12
21
Medium Priority
?
267 Views
Last Modified: 2013-11-24
i know i can print out things to an applet... ie different images using a loop

for(int k = 0; k < 5; k ++)
{
  //g.drawImage(img1, int, int, this);
  //busyWait(PAUSE);
  //g.drawImage(img2, int, int, this),
  //busyWait(PAUSE);
}

but can this be done with other components and servlets

//**************use loop to print out radio buttons**************
                "<input type=\"radio\" name=\"color\" \n" +
                "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                "<input type=\"radio\" name=\"color\"\n" +
                "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                "<input type=\"radio\" name=\"color\"\n" +
                "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +
//**************use loop to print out radio buttons**************

for(int k = 0; k < 5; k ++)
{
  //print out radiobuttons
}
0
Comment
Question by:annie613
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13525674
Yes it can be. Just print in the usual way but in a loop
0
 

Author Comment

by:annie613
ID: 13525709
i get an error when i put the for loop in my code
i think it has to do with where i am placing it

"<tr><td>Select a color:</td>\n" +
"<td>\n" +
                //it doesnt like where i put this for...illegal start of expression
                for(int k = 0; k < 3; k++)
                {
                   //**************use loop to print out radio buttons**************
                  "<input type=\"radio\" name=\"color\" \n" +
                  "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +
                  //**************use loop to print out radio buttons**************

                }//end for
               
 "<tr><td>Select all the a number:</td>\n" +
 "<td>\n" +
0
 

Author Comment

by:annie613
ID: 13525710
and i know the code isnt correct yet
0
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 

Author Comment

by:annie613
ID: 13525729
i see what i did. because i placed bad code in the for loop it thinks its illegal...now to code the loop
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13525738
Yes it is in the wrong place. But before fixing that: how would this benefit you? The values would only get repeated. You need distinct ones. You could do

name=\"color + k + \"

etc.

but is that what you'd want?
0
 

Author Comment

by:annie613
ID: 13525743
name=\"color + k + \"
yeah that is what i am looking to do..
:) let me play around :)
0
 

Author Comment

by:annie613
ID: 13525853
i am just trying to get a simple loop to print out

but i am not sure where to put the loop

for(int k = 0; k < 3; k++)
{
        out.println("test");
        out.println(k);
}//end for

i thought inside my doGet but i think im placing it in the wrong place?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13525879
There's nothing wrong with that in itself, but of course i don't know the context. Before the loop followed this sort of thing:

"x" +

for (int i =

etc., which is clearly wrong. Make sure you have no String stuff before/after
0
 

Author Comment

by:annie613
ID: 13525903
its funny i can get the loop to work on one servlet but not another? let me try to exam the code a bit more *confused*
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13525931
btw, on a general point: you do realise that all this printing html from servlets is quite old hat and undesirable don't you? Although there are better ways, a small increment would take you to jsp which nonetheless would be a significant increment in ease of programming
0
 

Author Comment

by:annie613
ID: 13525964
i do understand that..i know it would be TOTALLY easier to use JSP seeing that one reason JSP was created was because of the challenges of using servlets to create forms like this..but i want to have an understanding of this before i move ahead...

im just confused on how to put the html into a java loop..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13526095
>>but i want to have an understanding of this before i move ahead...

I understand, that's probably a good strategy for you. Here's an example.

for(int i = 0;i < 5;i++) {
      out.println("<input type=\"radio\" name=\"color" + i + "\n value=\"Red\">Red");
}
0
 

Author Comment

by:annie613
ID: 13526100
.
.
.
"<tr><td>Select color:</td>\n" +
"<td>\n");//i forgot to close this
               
                //test for a simple for loop
                //IT WORKS
                for(int k = 0; k < 3; k++)
                {
                    out.println(k);
                }//end for

//needed the out.println
out.println("<input type=\"radio\" name=\"status\" \n" +
"value=\"Sponsor\"> Sponsor&nbsp;&nbsp; \n" +

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Member\"> Member&nbsp;&nbsp;\n" +

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Visitor\" checked> Visitor&nbsp;&nbsp;\n" +

"<tr><td>Select Number:</td>\n" +
"<td>\n" +
.
.
.

now will the html be like a string??? im not sure i understand how this is a String and could be printed

"<input type=\"radio\" name=\"status\"\n" +
"value=\"Member\"> Member&nbsp;&nbsp;\n" +
0
 

Author Comment

by:annie613
ID: 13526105
something like this maybe

String[] colors = new String[] { "Red", "Blue", "Green"};

for (int i=0; i<colors.length; i++)
{
    //"<input type=\"radio\" name=\"color\" value=\"COLORNAME\"> COLORNAME&nbsp;&nbsp; \n"

    out.print("<input type=\"radio\" name=\"color\" value=\"");
    out.print(colors[i]);
    out.println("\">" + colors[i] + "&nbsp;&nbsp; \n");

}

im still trying :)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 700 total points
ID: 13526110
But again, the above seems strange since you'd get 'Red' fives times. You probably need something more like

final String[] COLORS = { "Red", "Blue", "Green" };
for(int i = 0;i < COLORS.length;i++) {
      out.println("<input type=\"radio\" name=\"color" + i + "\n value=\"" + COLORS[i] + "\">" + COLORS[i]);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13526113
I think you beat me to it ;-)
0
 

Author Comment

by:annie613
ID: 13526158
i think your code is more readable and solid so i chose to work with that..
my only problem is now making sure i have a radio checked when the page displays

my orginal code i had Blue checked

                  "<input type=\"radio\" name=\"color\" \n" +
                  "value=\"Red\"> Red&nbsp;&nbsp; \n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Green\"> Green&nbsp;&nbsp;\n" +

                  "<input type=\"radio\" name=\"color\"\n" +
                  "value=\"Blue\" checked> Blue&nbsp;&nbsp;\n" +

so im trying for that and i should have it
0
 

Author Comment

by:annie613
ID: 13526175
thanks again for all your help! i am glad we were able to both come up with something similiar. :) im going to work on the checked now and try to find more example to learn from! :) cheers
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13526196
// The below outside the loop
String part1 = "<input type=\"radio\" name=\"color" + i + "\n value=\"";
String part2 = ">" + COLORS[i];

// The below in the loop
if ("Blue".equals(COLORS[i])) {
      System.out.println(part1 + " checked" + part2);
}
else {
      System.out.println(part1 + part2);
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13526200
8-)

>>// The below outside the loop

Ignore that ;-)
0
 

Author Comment

by:annie613
ID: 13526208
awesome!
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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