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>     
5
  • Where is fibSeq defined? Is it in scope? Commented Dec 10, 2014 at 22:54
  • 2
    It's a very bad idea to pass arguments to be displayed in the next view as part of the query string. I recommend you using forwarding instead. Commented Dec 10, 2014 at 22:54
  • fibSequence or fibSeq? Commented Dec 10, 2014 at 23:10
  • Oh I see the mistake now, should be fibSequence, fibSeq is the variable name I've used in the jsp page. Commented Dec 10, 2014 at 23:12
  • You should also read this stackoverflow.com/questions/10904911/… Commented Dec 10, 2014 at 23:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
Simply with out.print(...). out is a predefined variable in a jsp. Otherr are response, request, session,...
That is giving this output: [I@8ba95be
À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?
Seems to be printing the memory address not the integer values.
|
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] %>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.