HttpServletRequest setAttribute <-> getParameterValues question for arrays

A

Anke Barton

I have a database servlet that queries a list of names and it should
then forward this list to a JSP. My servlet uses the setAttribute
method:

visit.java

public void GetProviderList(HttpServletRequest request)
throws SQLException, ClassNotFoundException
{
int i = 0;
String prov_name[] = new String[100];
Statement cmd = con.createStatement();
// Get all providers -> prov_name
String command = "select name from providers ";
ResultSet rset = cmd.executeQuery(command);
while (rset.next()) {
prov_name = rset.getString(1);
i++;
}
// Send array prov_name
request.setAttribute("PROVIDERLIST", prov_name);
rset.close();
cmd.close();
}

Then, the JSP tries to read this string array:

<select name="PROVIDERS" size=1>
<%! String provider_name[] = new String[100]; %>
<% provider_name = request.getParameterValues("PROVIDERLIST"); %>
<%! String fullname = provider_name[0]; %>
<option value=<%= fullname%>><%= fullname %>
</select>

However, the getParameterValue returns a null list. I think that
there is something wrong with my setAttribute function, but I'm new to
Java and can't figure this out.

Thank you for any suggestion.

Anke
 
R

Ravi Shankar

There is alimitation to URL you can cretae (Max 256 chars). Hence why not
you put into session and retrive from session

Regards,
Ravi
 
J

John C. Bollinger

Anke said:
I have a database servlet that queries a list of names and it should
then forward this list to a JSP. My servlet uses the setAttribute
method:

visit.java

public void GetProviderList(HttpServletRequest request)
throws SQLException, ClassNotFoundException
{
int i = 0;
String prov_name[] = new String[100];
Statement cmd = con.createStatement();
// Get all providers -> prov_name
String command = "select name from providers ";
ResultSet rset = cmd.executeQuery(command);
while (rset.next()) {
prov_name = rset.getString(1);
i++;
}
// Send array prov_name
request.setAttribute("PROVIDERLIST", prov_name);
rset.close();
cmd.close();
}

Then, the JSP tries to read this string array:

<select name="PROVIDERS" size=1>
<%! String provider_name[] = new String[100]; %>
<% provider_name = request.getParameterValues("PROVIDERLIST"); %>
<%! String fullname = provider_name[0]; %>
<option value=<%= fullname%>><%= fullname %>
</select>

However, the getParameterValue returns a null list. I think that
there is something wrong with my setAttribute function, but I'm new to
Java and can't figure this out.

Thank you for any suggestion.


"Attributes" and "parameters" are not at all the same thing. Parameters
are what are parsed out of the query string, and/or out of the request
body in the case of a form POST. Attributes are Java objects attached
to particular pages, requests, sessions, or applications and referenced
by name. Parameters are passed from client to server in a request; they
are text-only. Attributes live only inside the application server, but
they can be literally any object.

Since you added attributes to the request with request.setAttribute(),
did you consider retrieving them with request.getAttribute()? Do please
recognize that since they are confined to the application server,
attributes attached to requests are lost in the process of an HTTP
redirect. If by "forward" you meant using a RequestDispatcher or
jsp:forward action to deliver the request elsewhere in the same
application server, then your attributes will survive the process.


John Bollinger
(e-mail address removed)
 
A

Anke Barton

John C. Bollinger said:
Anke said:
I have a database servlet that queries a list of names and it should
then forward this list to a JSP. My servlet uses the setAttribute
method:

visit.java

public void GetProviderList(HttpServletRequest request)
throws SQLException, ClassNotFoundException
{
int i = 0;
String prov_name[] = new String[100];
Statement cmd = con.createStatement();
// Get all providers -> prov_name
String command = "select name from providers ";
ResultSet rset = cmd.executeQuery(command);
while (rset.next()) {
prov_name = rset.getString(1);
i++;
}
// Send array prov_name
request.setAttribute("PROVIDERLIST", prov_name);
rset.close();
cmd.close();
}

Then, the JSP tries to read this string array:

<select name="PROVIDERS" size=1>
<%! String provider_name[] = new String[100]; %>
<% provider_name = request.getParameterValues("PROVIDERLIST"); %>
<%! String fullname = provider_name[0]; %>
<option value=<%= fullname%>><%= fullname %>
</select>

However, the getParameterValue returns a null list. I think that
there is something wrong with my setAttribute function, but I'm new to
Java and can't figure this out.

Thank you for any suggestion.


"Attributes" and "parameters" are not at all the same thing. Parameters
are what are parsed out of the query string, and/or out of the request
body in the case of a form POST. Attributes are Java objects attached
to particular pages, requests, sessions, or applications and referenced
by name. Parameters are passed from client to server in a request; they
are text-only. Attributes live only inside the application server, but
they can be literally any object.

Since you added attributes to the request with request.setAttribute(),
did you consider retrieving them with request.getAttribute()? Do please
recognize that since they are confined to the application server,
attributes attached to requests are lost in the process of an HTTP
redirect. If by "forward" you meant using a RequestDispatcher or
jsp:forward action to deliver the request elsewhere in the same
application server, then your attributes will survive the process.


John Bollinger
(e-mail address removed)




Thank you for your help. Yes, I found out that I need to use the
getAttribute function with the appropriate type casting.

So, instead of

<% provider_name = request.getParameterValues("PROVIDERLIST"); %>

I use
<% provider_name = (String[]) request.getAttribute("PROVIDERLIST");
%>

and it works.

Thank you for explaining the difference between parameter and
attribute.

Anke
 
Joined
Jan 10, 2008
Messages
1
Reaction score
0
Hey...i am working on a similar project.

Would you mind posting how you declared the "request" variable in JSP.

Greatly appreciate your help.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top