0

I have achieved sending an integer variable to a jsp page using the following code:

resp.sendRedirect(("result.jsp?fibNum=" + fibNum));

But when I try the same to pass the array, int[] fibSequence I get the following passed to the address bar of the jsp page:

fibSequence

Does anyone have any advice on how I can output the array value passed over to the jsp page?`

This is how I sent the array across to the result jsp page within the doPost():

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub


        // read form fields
        String fibNum = req.getParameter("fibNum");


        try{
              //Get reference from server's registry
              Registry registry = LocateRegistry.getRegistry("127.0.0.1");

              //Lookup server object from server's registry
              IFibonacci fibonacci_proxy = (IFibonacci)registry.lookup("PowerObject");


              int fibMax = Integer.parseInt(fibNum);

             //Invoke server object's methods 
             //Get Fibonacci array.
             int[] fibSequence = fibonacci_proxy.fibonacciArrayTest(fibMax);


             for (int value : fibSequence) {
                System.out.println(value);
             }


            //System.out.println(Arrays.toString(fibSequence));


            }catch(NotBoundException nbe){
              nbe.printStackTrace();
            }catch(RemoteException re){
              re.printStackTrace();
            }

            //send input to the result page using a redirect
            //resp.sendRedirect(("result.jsp?fibNum=" + fibNum));
            resp.sendRedirect(("result.jsp?fibSequence=" + fibSequence));

          }

How I've tried to retrieve the array values on the jsp page and print them, but I'm getting a fibSequence cannot be resolved to a variable although this is the name of the array passed over:

<a href="home.jsp">Return to Main</a><br>
             <%String[] var_array=request.getParameterValues("fibSequence");%>
             <%System.out.print(""+fibSequence);%>
        </form>     
Brian Var
  • 6,029
  • 25
  • 114
  • 212

2 Answers2

1

Trust the compiler. fiBSeq ist not defined. You defined fibSequence. But passing that array as an argument will not work, because you will pass (int[]).toString() which is probably not what you want. You can serialize and encode it, if it is not too big. Or post it.

EDIT 1

int [] array = {1,2,3,4,5,6,7,8,9};
System.out.print(""+array);//<-- print [I@15db9742  or similar

EDIT 2

Encoding the array on the sender side

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String param = Arrays.toString(array);
param = param.substring(1, param.length()-1);//removing enclosing []
String encArray = URLEncoder.encode(param, "utf-8");
    
// Send encArray as parameter.
resp.sendRedirect(("result.jsp?fibSequence=" + encArray));

Decoding the array on the receiver side

String encArray = request.getParameterValues("fibSequence");
String decArray = URLDecoder.decode(encArray,"utf-8");
//Now you can parse the list into an Integer list
String [] var_array = decArray.split(",");

In jsp, put the code between <% ... %>. If you get some unresolved symbol errors you have to import the missing libraries.
Can be one or more of the following, simply copy the statements at the top of the page.

<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>

(maybe java.util is imported per default, I am not sure)

BUT ATTENTION

Be aware of not sending too much data in this manner! The size of an URL maybe is not unlimited. Also the data is visible in the URL, a 'nasty' user could simply copy and reproduce requests.
A better way to send data is using HTTP post.

Community
  • 1
  • 1
Gren
  • 1,850
  • 1
  • 11
  • 16
  • Please see edits to my question, the array is passed over but I'm not sure how to output it to the jsp page.I know in a java class you can just use a println or toString, but how can I print it within this type of page? – Brian Var Dec 10 '14 at 23:32
  • Simply with `out.print(...)`. `out` is a predefined variable in a jsp. Otherr are `response`, `request`, `session`,... – Gren Dec 10 '14 at 23:38
  • That is giving this output: `[I@8ba95be` – Brian Var Dec 10 '14 at 23:42
  • Àlso I'm trying to retrieve the fibSequence I passed over not create a new array. How can I pull the value from the redirect? – Brian Var Dec 10 '14 at 23:43
  • Seems to be printing the memory address not the integer values. – Brian Var Dec 10 '14 at 23:44
  • Ok, will post as little workaround for passing an array as URL parameter. See EDIT2 in a few minutes – Gren Dec 10 '14 at 23:59
  • Thanks, will this work for my the decoding part of the solution work on a jsp page? I pasted it in but it gets recognized as text..maybe if I put these tags `<%` around the code? – Brian Var Dec 12 '14 at 01:13
0

Here is the better answer to transfer array variable from servlet to jsp page:

In Servelet:
String arr[] = {"array1","array2"};
request.setAttribute("arr",arr);
RequestDispatcher dispatcher = request.getRequestDispatcher("yourpage.jsp");
dispatcher.forward(request,response);
In Jsp:
<% String str[] = (String[]) request.getAttribute("arr"); %>
<%= str[0]+""+str[1] %>
Bipin Maharjan
  • 423
  • 6
  • 13