aspose file tools*
The moose likes Servlets and the fly likes how to get a string of parameters from a http servlet and pass it to stored procedure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get a string of parameters from a http servlet and pass it to stored procedure" Watch "how to get a string of parameters from a http servlet and pass it to stored procedure" New topic
Author

how to get a string of parameters from a http servlet and pass it to stored procedure

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
thanks for your email last time. I got one last question.
I am getting a QueryString from a web page to this SErvlet, different querySTrings will
be generated depending on the different user responses in the webpage. My job in this
servlet is to get the list of parameters and pass them to stored procedures.,
i won't be sure of the parameter names, so i am getting parameters names and values like
this in the Servlet.
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String values[] = req.getParameterValues(name);

if (values != null) {
for (int i=0; i<values.length; i++) {

out.println(name + " = " + values[i] + "<BR>" );

}
}
}
I am able to print parameter names and parameter values. But what i want is to collect
the parameter values and pass them to a Stored procedure.
If i collect them in while loop, then i am unable to use them outside the while loop.
all i need is a string of parameter values will comma separated. and pass them to stored
procedure.
I am calling stored procedure like this
try{
CallableStatement cs = dbconn.prepareCall("{Call " + proc_name + "(?,?)}" );
}
catch(SQLException e)
{
System.out.println("GOT SOME SQLEXCEPTION" + e.getMessage());
}

cs.execute;
My question.
1. i am unable to collect the parameter values in a STring.
2. how to pass them to stored PRocedure
can u suggest me something

Bhasker Reddy
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you said: "If i collect them in while loop, then i am unable to use them outside the while loop."

That's only if you declare your variable inside the loop! Variables have block scope. Declare your array outside the while loop, populate inside the loop, and it's available 'outside' the loop.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I've been thinking more about this.

If you're using a stored proc, then you *must* know something of the nature of your request. The stored proc is expecting a certain number of parameters, so even if you don't know parameter names, you must know their number, right?

If this is form processing, how would you handle a textfield left blank? This is not sent on the query string. What about checkboxes? If there are 10 of them, there could be from zero to 10 entries for that particular parameter name. Here is an example of what i did.. but it assumes that the parameters are well known.
I used 10 in the 2D array constructor, because even though it compiles without one, it makes nasty error messages when trying to run. Something about a signature on the method, and that it might not be compatible with array of arrays or objects.

The request i used, looks like this:
?fName=Mike&lName=Curwen&id=1&id=2&id=3&lastOne=99
and the output is:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get a string of parameters from a http servlet and pass it to stored procedure