How to set the JSTL variable value in javascript?

How to set the JSTL variable value in java script?


 function function1()


  var val1 = document.getElementById('userName').value;

  <c:set var="user" value=""/>  // how do i set val1 here?   


How do I set the 'user' variable (JSTL) value from 'val1' (Java script)?



It is not possible because they are executed in different environments (JSP at server side, JavaScript at client side). So they are not executed in the sequence you see in your code.

var val1 = document.getElementById('userName').value; 

<c:set var="user" value=""/>  // how do i set val1 here? 

Here JSTL code is executed at server side and the server sees the JavaScript/Html codes as simple texts. The generated contents from JSTL code (if any) will be rendered in the resulting HTML along with your other JavaScript/HTML codes. Now the browser renders HTML along with executing the Javascript codes. Now remember there is no JSTL code available for the browser.

Now for example,

<script type="text/javascript">
<c:set var="message" value="Hello"/> 
var message = '<c:out value="${message}"/>';

Now for the browser, this content is rendered,

<script type="text/javascript">
var message = 'Hello';

Hope this helps.


one more approach to use.

first, define the following somewhere on the page:

<div id="valueHolderId">${someValue}</div>

then in JS, just do something similar to

var someValue = $('#valueHolderId').html();

it works great for the cases when all scripts are inside .js files and obviously there is no jstl available


Just don't. Don't write code with code. Write a JSON object or a var somewhere but for the love of a sensible HTTP divide, don't write JavaScript functions or methods hardcoded with vars/properties provided by JSTL. Generating JSON is cool. It ends there or your UI dev hates you.

Imagine if you had to dig into JavaScript to find something that was setting parameters in the middle of a class that originated on the client-side. It's awful. Pass data back and forth. Handle the data. But don't try to generate actual code.


This variable can be set using value="${val1}" inside c:set if you have used jquery in your system.


As an answer I say No. You can only get values from jstl to javascript. But u can display the user name using javascript itself. Best ways are here. To display user name, if u have html like

<div id="uName"></div>

You can display user name as follows.

var val1 = document.getElementById('userName').value;
document.getElementById('uName').innerHTML = val1;

To get data from jstl to your javascript :

var userName = '<c:out value="${user}"/>';

here ${user} is the data you get as response(from backend).

Asigning number/array length

var arrayLength = <c:out value="${details.size()}"/>;


function advanced(){
    var values = new Array();
    <c:if test="${empty details.users}">
       values.push("No user found"); 

    <c:if test="${!empty details.users}">
        <c:forEach var="user" items="${details.users}" varStatus="stat">
    alert("values[0] "+values[0]);


You can save the whole jstl object as a Javascript object by converting the whole object to json. It is possible by Jackson in java.

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil{
   public static String toJsonString(Object obj){
      ObjectMapper objectMapper = ...; // jackson object mapper
      return objectMapper.writeValueAsString(obj);


<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="" 


      <function-signature>java.lang.String toJsonString(java.lang.Object)</function-signature>





<%@ taglib prefix="uf" uri="http://www.your.url/util-functions" %> 

   var myJavaScriptObject = JSON.parse('${uf:toJsonString(myJstlObject)}');
<script ...
   var someJsVar = "<c:out value='${someJstLVarFromBackend}'/>";


This works even if you dont have a hidden/non-hidden input field set somewhere in the jsp.


You have to use the normal string concatenation but you have to make sure the value is a Valid XML string, you will find a good practice to write XML in this source, or if you like you can use an API in javascript to write XML as helma for example.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.