Reading a Jsp Variable from JavaScript

Reading a JSP variable from JavaScript

alert("${variable}");

or

alert("<%=var%>");

or full example

<html> 
<head>
<script language="javascript">

function access(){
<% String str="Hello World"; %>
var s="<%=str%>";
alert(s);
}

</script>
</head>

<body onload="access()">
</body>

</html>

Note: sanitize the input before rendering it, it may open whole lot of XSS possibilities

Want to access JSP Variables in javascript

Use JSTL tag for accessing the JSP variable in JavaScript for example here is code snippet. Where seatList is your JSP variable.

<script>var list = []; <c:forEach items="${seatList}" var="seat"> var arr = []; arr.push("<c:out value="${seat.seatNo}" />"); list.push(arr); </c:forEach></script>

Access JSP variable in javascript

Your JavaScript is inside a .js file. The server will (exceptional circumstances aside) treat that as a static file. It will just serve it up to the client without processing.

If you want to run JSP code (such as <%=myVar%>) then put it in a JSP file.

How can i access javascript variables in JSP?

JavaScript variable is on client side, JSP variables is on server side, so you can't access javascript variables in JSP. But you can store needed data in hidden fields, set its value in client and get it on server over GET or POST.

Client side:

<script type="text/javascript">
var el = document.getElementById("data");
el.value = "Needed_value";
</script>
<form action="./Your_JSP.jsp" method="POST">
<input id="data" type="hidden" value="" />
<input type="submit" />
</form>

server side:

<%
if (request.getParameter("data") != null) { %>
Your value: <%=request.getParameter("data")%>
<%
}
%>

Accessing java variable from javascript on same jsp

This won't work since you're trying to use an undefined variable. The code is generated like this:

... = myVar;
//...
String myVar = "blabla";

Doesn't make sense, right? So, in order to make this work you should declare the variable before using it (as always):

<%
String myVar="blabla";
%>
<script type="text/javascript">
foo();
function foo() {
var value = "<%=myVar%>";
alert(value);
}
</script>

Still, usage of scriptlets is extremely discouraged. Assuming you're using JSTL and Expression Language (EL), this can be rewritten to:

<c:set name="myVar" value="blabla" />
<script type="text/javascript">
foo();
function foo() {
var value = "${myVar}";
alert(value);
}
</script>

If your variable has characters like " inside, then this approach will faile. You can escape the result by using <c:out> from JSTL:

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

More info:

  • How to avoid Java code in JSP files?

JSP : using java variable in Javascript

Just assign the value to your Javascript variable.

<script type="text/javascript">
var count = '<%= totalCount %>';
</script>

But using scriptlets been highly discouraged and shift to JSTL as soon as possible.

Read : How to avoid Java code in JSP files?

As others pointed out, do not create unnecessary hidden elements on DOM. If you really want to use this variable across files declare this variable on top. Even before script includes, so that it avail across all the files.

Example:

 <script type="text/javascript">
var count = '<%= totalCount %>';
</script>
<script type='text/javascript' src='js/blah.js'></script>
<script type='text/javascript' src='js/blahblah.js'></script>

How do I pass JavaScript values to Scriptlet in JSP?

Your javascript values are client-side, your scriptlet is running server-side. So if you want to use your javascript variables in a scriptlet, you will need to submit them.

To achieve this, either store them in input fields and submit a form, or perform an ajax request. I suggest you look into JQuery for this.



Related Topics



Leave a reply



Submit