1

I have a .jsp page in which I used on click JavaScript function for further validation and other processing stuff. Now I've created an array in jsp scriptlets and how do I pass into this JavaScript function that gets called when submit button is hit? I tried something like this:

var jsArray = <%=lineArray%>;

But that didn't work out.
In fact the function wasn't getting called after I the put above scriptlet. So how do I copy this java array into a JavaScript array?

2
  • 3
    Google something like AJAX and Client-Server principle. Commented Jun 19, 2015 at 11:36
  • Share your code snippet Commented Jun 19, 2015 at 11:49

2 Answers 2

2

Try something like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

var jsArray = [ 
  <c:forEach var="item" items="${lineArray}">
   <c:out value="${item}"/>,
  </c:forEach>
];

This will generate javascript array variable jsArray and put the Java lineArray values into it. If that's what you need to do.

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

2 Comments

What is prefix="c" about? And what is <c:? Is it created due to the prefix ="c" declaration?
You can use custom jsp tags from custom taglibs in .jsp. In this example I'm using standard JSTL taglib. See tutorialspoint.com/jsp/taglib_directive.htm or tutorialspoint.com/jsp/jsp_standard_tag_library.htm for more info.
0

Actually You can't Access Java object(server) directly Into "JavaScript"(client). You have to Create a service(Ajax call) and get the response Object from the server........

But you can do something like this.....

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 var jsArray = {
      <c:forEach items="${employees}" var="employee">
      "${employee.id}": {
        name:"${employee.employeeName}",
        cv:"${employee.employeeCV}",
      },
      </c:forEach>
    }

After Jsp parse....

var employees = {
  "1": {
    name:"foo",
    cv:"cv1",
  },
  "2": {
    name:"bar",
    cv:"cv2",
  },
}

Then you can modify js object as per your need...

Check Below link For further clarification....

How to access a java object in javascript from JSP?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.