parsing query string parameter causes server to hang

I

inetquestion

I am trying to add a check in my web app to look for a query string
parameter and produce different output depending on the value of
param1 if it exists. With the code below it works fine if there is no
query string parameters, but hangs if there are any present. I'm new
to Java, so any help would be appreciated.



Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String[] debug_array = request.getParameterValues("param1");
debug = (String) debug_array[0];
}


-Inet
 
M

Manish Pandit

I am trying to add a check in my web app to look for a query string
parameter and produce different output depending on the value of
param1 if it exists. With the code below it works fine if there is no
query string parameters, but hangs if there are any present. I'm new
to Java, so any help would be appreciated.

Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String[] debug_array = request.getParameterValues("param1");
debug = (String) debug_array[0];

}

-Inet

That is because e.hasMoreElements() is returning true all the time.
You need to increment the counter by using e.nextElement( ) and use
that to get the parameter values.

while(e.hasMoreElements()){
String[] debugArray = request.getParameterValues((String)
e.nextElement());
debug = (String) debug_array[0];

}

-cheers,
Manish
 
O

Owen Jacobson

I am trying to add a check in my web app to look for a query string
parameter and produce different output depending on the value of
param1 if it exists. With the code below it works fine if there is no
query string parameters, but hangs if there are any present. I'm new
to Java, so any help would be appreciated.

Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String[] debug_array = request.getParameterValues("param1");
debug = (String) debug_array[0];

}

-Inet

You never step the enumeration forward, so e.hasMoreElements will
always return the same value. See the javadocs for
java.util.Enumeration for details.
 
J

Joe Attardi

inetquestion said:
Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String[] debug_array = request.getParameterValues("param1");
debug = (String) debug_array[0];
}

Manish and Owen already answered your question, but just to add my
$0.02. Since debug_array is a String[], you don't have to cast
debug_array[0] to a String.
 
M

Manish Pandit

Manish and Owen already answered your question, but just to add my
$0.02. Since debug_array is a String[], you don't have to cast
debug_array[0] to a String.

Ah! Good catch :)

-cheers,
Manish
 
D

Daniel Pitts

Joe said:
inetquestion said:
Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String[] debug_array = request.getParameterValues("param1");
debug = (String) debug_array[0];
}

Manish and Owen already answered your question, but just to add my
$0.02. Since debug_array is a String[], you don't have to cast
debug_array[0] to a String.
Now, for my 2 cents...
If you are using getParameterValues("param1"), don't use
getParameterNames at all..., actually, since you only use the first one,
try: replacing that entire snippet with:

debug = request.getPramaterValue("param1");
 
D

Daniel Pitts

Daniel said:
If you are using getParameterValues("param1"), don't use
getParameterNames at all..., actually, since you only use the first one,
try: replacing that entire snippet with:

debug = request.getPramaterValue("param1");

Or, if you're like me and don't use spell check,
debug = request.getParameterValue("param1");
 
J

Joe Attardi

Daniel said:
Or, if you're like me and don't use spell check,
debug = request.getParameterValue("param1");
Isn't it request.getParameter("param1") for a single parameter?
 
D

Daniel Pitts

Joe said:
Isn't it request.getParameter("param1") for a single parameter?
Probably, but the point was made:
Read the Javadoc and choose the best method :)
 
I

inetquestion

Cool! thanks for your assistance.

I made the changes you pointed out and its working perfectly. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top