• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

How to get Array in JSP

hello,

I have a form:
<form method="post" ..>

while (rs.next()){
                                out.print("New File path:"+rs.getString(10)+rs.getString(6));
                                out.print("<br>");
                                           if (rs.next()) {
                                              out.print("New File path:"+rsgetString(10)+rs.getString(6));
                                               out.print("<br>");                                              
                                }
                            }
</form>

I'm using above JSP code to display the data. Now I hv to send this data thru form to a JSP page.  in JSP page how should I set of getParameter
=====JSP c0de====

String[] a = request.getParameter("<<NOT working>>");
for (int i=0; i<a.length; i++){
.....

MY Question is how to get values of
rs.getString(10)+rs.getString(6) here in my jsp code.
I think using Hash table but can u please write that code.

Thanks

0
princehyderabad
Asked:
princehyderabad
  • 13
  • 10
1 Solution
 
bloodredsunCommented:
use
String[]a = request.getParameterValues() ;
or
Enumeration enum = request.getParameterNames() ;
depending on whether you want the values or the names of the parameters.
0
 
koppchaCommented:
you use request.setAttribute() to set any thing to the equest obeject so that you can get it in the other JSP.
Build arraylist out of your parameters and send the arraylist using the request object.I am not clear about what you want to do so only i was not able to write the code but the above mentioned thing will work i guess

0
 
princehyderabadAuthor Commented:
JSP output:
--------------------------
New File path:/abc/xyz/1/batch.exe
New File path:/abc/xyz/1/readme.txt
New File path:/abc/xyz/1/install.exe
---------------------------------

I want to pass this:
/abc/xyz/1/batch.exe
/abc/xyz/1/readme.txt
/abc/xyz/1/install.exe
 or rather I can do
request.setAttribute("path", /abc/xyz/1/i)
request.setAttribute( ??????

how to pass:
batch.exe, readme.txt, install.exe

and how to accept at Server:
String[] a = getAttribute(?????

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
koppchaCommented:
String path1="/abc/xyz/1/batch.exe";
String path2="/abc/xyz/1/readme.txt";
ArrayList path = new ArrayList();
path.add(path1);
path.add(path2);

request.setAttribute(path);


Is this what you are looking for ?
0
 
princehyderabadAuthor Commented:
Yes thats what I'm looking.

Q1. But I dont know how many files so can you please also provide the code with for loop or still all files been added to path ArrayList.
Q2. how to getAttribute in Servlet ???
     String[] a = request.getAttribute("path");  IS THIS ENOUGH ?



0
 
koppchaCommented:
>But I dont know how many files so can you please also provide the code with for loop or still all files been added to path ArrayList.
     you can keep a for loop to iterate the files and keep path.add(filepath) in the loop since it is dynamically increasing you do not have to worrry about the size.

>how to getAttribute in Servlet ???      String[] a = request.getAttribute("path");  IS THIS ENOUGH ?
    Not really you have to cast correctly since it is an ArrayList you have to take it to arrayList variable only so
ArrayList a =(ArrayList) request.getAttribute("path") will do after getting the new arraylist iterate over it and get the path
like this




0
 
princehyderabadAuthor Commented:
ArrayList a =  (ArrayList) request.getAttribute("path");                    
                            for (int i=0; i<a.length; i++){
                            //while (procResult.next()){
                              Process p = Runtime.getRuntime().exec("abc");
            PrintWriter pw = new PrintWriter(p.getOutputStream());
             pw.println("crypto/"+a[i]);
...

1st  Error:
cannot resolve symbol symbol  : variable length
2nd Error:
array required, but java.util.ArrayList found   pw.println("crypto/"+a[i]);
                                                                                                 ^
0
 
koppchaCommented:
//Check the changes done...1> it is a.size() not a.length 2>not a[i] but a.get(i)...
ArrayList a = (ArrayList) request.getAttribute("path");
for (int i=0; i<a.size(); i++){
                            //while (procResult.next()){
                              Process p = Runtime.getRuntime().exec("abc");
            PrintWriter pw = new PrintWriter(p.getOutputStream());
             pw.println("crypto/"+a.get(i));
...
0
 
princehyderabadAuthor Commented:
I'm getting null value for my variable 'a' can u plz look am I setting is correcting here:

ArrayList path = new ArrayList();
                            while (rs.next()){
                              out.print("New File path:"+procResult.getString(10)+procResult.getString(6));
                               path.add(procResult.getString(10)+procResult.getString(6));
                                           if (procResult.next()) {
                                              out.print("New File path:"+procResult.getString(10)+procResult.getString(6));
                                               path.add(procResult.getString(10)+procResult.getString(6));                            
                                }

0
 
princehyderabadAuthor Commented:
ArrayList path = new ArrayList();
                            while (rs.next()){
                              out.print("New File path:"+rs.getString(10)+rs.getString(6));
                               path.add(rs.getString(10)+rs.getString(6));
                                           if (rs.next()) {
                                              out.print("New File path:"+rs.getString(10)+rs.getString(6));
                                               path.add(rs.getString(10)+rs.getString(6));                            
                                }
 request.setAttribute("path",path);

0
 
princehyderabadAuthor Commented:
Before sending to final page I;m checking is path has values or not.
System.out.println(path);
I'm getting the values printed. Seems like only while I'm getting Attribute its getting null why ?? dont know

0
 
koppchaCommented:
Are you sure you are forwarding it correctly?
Just check if you are reaching the correct destination?
0
 
princehyderabadAuthor Commented:
yes destination is correct.
And When I submit the form it is going there and in between it is chaning to null somewhow dont know ???.
0
 
princehyderabadAuthor Commented:
ArrayList a = (ArrayList) request.getAttribute("path");
                            System.out.println("In Final Page: "+a);

I'm gettting: "In Final Page: NULL"
0
 
koppchaCommented:
It should work no clue why it is not working
try this
ArrayList a = (ArrayList) request.getParameter("path");
0
 
princehyderabadAuthor Commented:
ArrayList a = (ArrayList) request.getParameter("path");

Error: Inconvertible types; cannot cast 'java.lang.String' to 'java.Util.ArrayList'
0
 
princehyderabadAuthor Commented:
This is the Answer KOPPCHA:

 obviously you will get null in both the cases...
you are setting up a attribute in the request.. but that request is being served to the browser..
( that how you are seeing the HTML in explorer)..
now when user hits submit on the HTML then it goes to 2.jsp and this time it is a new request which is coming from the browser to 2.jsp..
it will not find arraypath in this request..
request.setAttibute works only when it is the same request. i.e. you have done a forward to another jsp...
otherwise session is the only way to share a object between two jsps....
0
 
koppchaCommented:
>you have done a forward to another jsp...
          You didn't mention that you are redirecting your page.I was assuming that you are forwarding your page.
0
 
princehyderabadAuthor Commented:
Oops sorry you missunderstood. In my 1st mesg. I clearly wrote the code. And it was <form ...
0
 
koppchaCommented:
>I clearly wrote the code. And it was <form
      This doesn't say anyting about how you are moving to another page?Anyways you got yur problem resolved.
0
 
princehyderabadAuthor Commented:
Thanks for your time :o)
0
 
koppchaCommented:
Good Luck :)
0
 
koppchaCommented:
Author has acccepted ? Isn't it correct?
0
 
princehyderabadAuthor Commented:
yes
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now