Using Expression language under javascript?

13,355

Solution 1

When a JSP page is called, the following happens, in this order:

  • Server checks to see if the .jsp has already been compiled and whether or not it has changed since it was last compiled.
  • Server runs the jsp through the Jasper compiler, which interprets the jsp into Java code, anything that is not Java (CSS, HTML, JavaScript, etc) is placed in a String.
  • The Java code is compiled and executed.
  • The results are placed in the response and sent to the user.

So, your statement: ${sessionScope.custId} is executed before the the HTML is sent to the user, and the input of selectCustomers() function is already set to before calling it.


For more info have a look at my another post JSP inside ListItems onclick

How to verify it?

Right click in the browser and look at the view source.


Try below sample code that might help you.

Enclose ${...} inside the single quotes.

<c:set var="custId" value="1234" scope="session" />

Before :
<c:out value="${sessionScope.custId}"></c:out>

<input type="checkbox" name="vehicle" value="Bike"
    onclick="javascript:selectCustomers('${sessionScope.custId}');">

<c:set var="custId" value="4321" scope="session" />

After:
<c:out value="${sessionScope.custId}"></c:out>

View Source code: (Right click in browser to view it)

Before : 1234

<input type="checkbox" name="vehicle" value="Bike"
    onclick="javascript:selectCustomers('1234');">  

After: 4321

Solution 2

Try this:

<input type="hidden" id="custId" name="custId" value="${sessionScope.custId}">

<input type="checkbox" name="vehicle" value="Bike" onclick="javascript:selectCustomers();">
function selectCustomers(){
   var custId = document.getElementById('custId').value;
}
Share:
13,355
user3198603
Author by

user3198603

Updated on June 04, 2022

Comments

  • user3198603
    user3198603 almost 2 years

    I have below CheckBox in JSP file

    <input type="checkbox" name="vehicle" value="Bike"
        onclick="javascript:selectCustomers(${sessionScope.custId});">
    

    Getting the following error:

    org.apache.jasper.JasperException: customer.jsp(1419,33) According to TLD or attribute directive in tag file,
            attribute onclick does not accept any expressions

    Can we not use expression language in JavaScript (in my case under onClick() Event)?