Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

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

Avatar of bloodredsun
bloodredsun
Flag of Australia image

use
String[]a = request.getParameterValues() ;
or
Enumeration enum = request.getParameterNames() ;
depending on whether you want the values or the names of the parameters.
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

Avatar of princehyderabad
princehyderabad

ASKER

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
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 ?
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 ?



>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




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]);
                                                                                                 ^
ASKER CERTIFIED SOLUTION
Avatar of koppcha
koppcha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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));                            
                                }

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);

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

Are you sure you are forwarding it correctly?
Just check if you are reaching the correct destination?
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 ???.
ArrayList a = (ArrayList) request.getAttribute("path");
                            System.out.println("In Final Page: "+a);

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

Error: Inconvertible types; cannot cast 'java.lang.String' to 'java.Util.ArrayList'
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....
>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.
Oops sorry you missunderstood. In my 1st mesg. I clearly wrote the code. And it was <form ...
>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.
Thanks for your time :o)
Good Luck :)
Author has acccepted ? Isn't it correct?
yes