How to access a java object in javascript from JSP? [duplicate]

I am having a dropdown box in JSP, listing java object (accessing the object through MVC controller addAttribute). Now, on selection of an option from the dropdown box, I would like to display the selected employee's other details (example - ${employee.employeeCV} , ${employee.employeeName}) in a div. I have a javascript function for that (displayCV()). But not sure how to do this.


<c:forEach items="${employees}" var="employee">
  <option value="${}" onclick="displayCV();">



function displayCV() {
    var valueSelected = $('#employeeList').val();
    var div = $('#candidateDiv');


Could anybody help me with this please?



You can't access Java classes directly from JavaScript, you have to use some kind of web service communication between the JavaScript (client) and Java (server), you can make use of onchange event which will send a request to the server to return XML/JSON which you can parse to get the data (I see you're using jQuery, it has parseJSON method already) and update the corresponding node in the DOM.

Another easier though not multi user friendly (because it can't detect updates) is to "convert" the Java object to JavaScript and update the data using that object (still using onchange). Something like:

// this is JavaScript code written in the JSP
var employees = {
  <c:forEach items="${employees}" var="employee">
  "${}": {

now when JSP parse this, it would generate, for instance:

var employees = {
  "1": {
  "2": {

Meta of what LeleDumbo said here already.

First, you must understand that JSP is a server-side view technology, whereas JavaScript typically runs on the client (browser).

Now, how you solve the problem in hand. So, you can make an AJAX request from your JavaScript, which will fetch you the data in JSON/XML format. Then, you can present that data in the browser using JavaScript.

Further reading:


Call the function on onchange event of select instead of onclick of oprions. and use


to get selected value.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.