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("<<NO T working>>");
for (int i=0; i<a.length; i++){
.....
MY Question is how to get values of
rs.getString(10)+rs.getStr ing(6) here in my jsp code.
I think using Hash table but can u please write that code.
Thanks
I have a form:
<form method="post" ..>
while (rs.next()){
out.print("New File path:"+rs.getString(10)+rs
out.print("<br>");
if (rs.next()) {
out.print("New File path:"+rsgetString(10)+rs.
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("<<NO
for (int i=0; i<a.length; i++){
.....
MY Question is how to get values of
rs.getString(10)+rs.getStr
I think using Hash table but can u please write that code.
Thanks
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
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
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.ex e
-------------------------- -------
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
--------------------------
New File path:/abc/xyz/1/batch.exe
New File path:/abc/xyz/1/readme.txt
New File path:/abc/xyz/1/install.ex
--------------------------
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
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.ex e";
String path2="/abc/xyz/1/readme.t xt";
ArrayList path = new ArrayList();
path.add(path1);
path.add(path2);
request.setAttribute(path) ;
Is this what you are looking for ?
String path2="/abc/xyz/1/readme.t
ArrayList path = new ArrayList();
path.add(path1);
path.add(path2);
request.setAttribute(path)
Is this what you are looking for ?
ASKER
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 ?
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
>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
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
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
like this
ASKER
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.getOutputStr eam());
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]) ;
^
for (int i=0; i<a.length; i++){
//while (procResult.next()){
Process p = Runtime.getRuntime().exec(
PrintWriter pw = new PrintWriter(p.getOutputStr
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.getStrin g(10)+proc Result.get String(6)) ;
path.add(procResult.getStr ing(10)+pr ocResult.g etString(6 ));
if (procResult.next()) {
out.print("New File path:"+procResult.getStrin g(10)+proc Result.get String(6)) ;
path.add(procResult.getStr ing(10)+pr ocResult.g etString(6 ));
}
ArrayList path = new ArrayList();
while (rs.next()){
out.print("New File path:"+procResult.getStrin
path.add(procResult.getStr
if (procResult.next()) {
out.print("New File path:"+procResult.getStrin
path.add(procResult.getStr
}
ASKER
ArrayList path = new ArrayList();
while (rs.next()){
out.print("New File path:"+rs.getString(10)+rs .getString (6));
path.add(rs.getString(10)+ rs.getStri ng(6));
if (rs.next()) {
out.print("New File path:"+rs.getString(10)+rs .getString (6));
path.add(rs.getString(10)+ rs.getStri ng(6));
}
request.setAttribute("path ",path);
while (rs.next()){
out.print("New File path:"+rs.getString(10)+rs
path.add(rs.getString(10)+
if (rs.next()) {
out.print("New File path:"+rs.getString(10)+rs
path.add(rs.getString(10)+
}
request.setAttribute("path
ASKER
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
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?
Just check if you are reaching the correct destination?
ASKER
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 ???.
And When I submit the form it is going there and in between it is chaning to null somewhow dont know ???.
ASKER
ArrayList a = (ArrayList) request.getAttribute("path ");
System.out.println("In Final Page: "+a);
I'm gettting: "In Final Page: NULL"
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 ");
try this
ArrayList a = (ArrayList) request.getParameter("path
ASKER
ArrayList a = (ArrayList) request.getParameter("path ");
Error: Inconvertible types; cannot cast 'java.lang.String' to 'java.Util.ArrayList'
Error: Inconvertible types; cannot cast 'java.lang.String' to 'java.Util.ArrayList'
ASKER
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....
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.
You didn't mention that you are redirecting your page.I was assuming that you are forwarding your page.
ASKER
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.
This doesn't say anyting about how you are moving to another page?Anyways you got yur problem resolved.
ASKER
Thanks for your time :o)
Good Luck :)
Author has acccepted ? Isn't it correct?
ASKER
yes
String[]a = request.getParameterValues
or
Enumeration enum = request.getParameterNames(
depending on whether you want the values or the names of the parameters.