Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having a servlet in which I have stored an array within the session scope as follows,

session.setAttribute("pageNames",pageNames);

Now this servlet transfers the controll to an JSP page which calls the function 'getPage()' while loading as follows,

<body  id="qwer" onload="getPage('<%=(String[])session.getAttribute("pageNames")%>'>

The javascript code for 'getPage()' is as follows,

function getPage(pageNamesArray)
                {
                  var ele = document.getElementById('app');

                    for(var i=0;i<10;i++)
                    {
                      var imageLabel = document.createElement("label");
                        imageLabel.innerHTML = pageNamesArray[i];
                        ele.appendChild(imageLabel);
                    }
                }

I expected that I would be getting the name of the first 10 values from 'pageNames' array that I had stored in the session, but instead I'am getting following output,

[Ljava.lang.String;@17a8undefinedundefinedundefined

I tested the code in my servlet and found that the array 'pageNames' is filled with more than 40 values.

Can anyone suggest me what to do to print the values of the array pageNames from within the JSP page? Thanks in advance.

share|improve this question
 
JSP is a server side technology. It renders HTML which is sent as the body of an HTTP response. Javascript is a client side technology which typically executes scripts contained in HTML within a browser. –  Sotirios Delimanolis 10 hours ago
 
@SotiriosDelimanolis so how do I access the arrays from the jsp page? –  Saumil Soni 10 hours ago
1  
You would have to serialize the contents of the array into a format that can be expressed and parsed in javascript (possibly JSON). –  Sotirios Delimanolis 10 hours ago
add comment

1 Answer

Your approach is correct but that's not how you print the elements of an array. You have to loop through the array and output the components.

To keep this clean, you could create another javascript function like:

function getPageNames() {
  var pageNamesArray = [
  <c:forEach var="p" items="${sessionScope.pageNames}" varStatus="status">
    ${status.first ? '' : ','} "${p}"
  </c:forEach>
  ];

  return pageNamesArray;
}

then have:

<body  id="qwer" onload="getPage(getPageNames())">

What I'm doing above is create a Javascript array with the page names, with an output like:

function getPageNames() {
  var pageNamesArray = [
    "page1"
    ,"page2"
    ,"etc"
  ];

  return pageNamesArray;
}

I'm using JSTL for that, but you can also use a scriptlet. The idea is to loop through the array.

share|improve this answer
 
hey thanks for your answer but when I pasted your code nothing seems to be printed. I checked using 'alert()' that 'getPageNames()' and 'getPage()' function are never called. I wrote my getPage() function as follows, function getPage(pageNamesArray) {.......} –  Saumil Soni 3 hours ago
 
I commented all the code and only kept 'alert()' within both the functions and now the alert() is displayed. –  Saumil Soni 2 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.