This question was tricky to answer. First, looks like you want to pass the variables declared in scriptlet to your JavaScript using EL. To accomplish this, you should:
- Set the variable in scriptlet as pageContext attribute or request attribute as explained here: How to evaluate a scriptlet variable in EL?
- Use JSTL
<c:out>
to send the variable from EL to your JavaScript function.
- Following GauravSharma's answer suggestion, add a
return p;
at the end of your JavaScript function.
Your code should look like this (at least works for me using Tomcat 7 and prints 2 in the navigator):
<script type="text/javascript">
function chk(d, e) {
var x = d.split('/');
var y = e.split('/');
var a = new Date(x[0], x[1] - 1, x[2]);
var b = new Date(y[0], y[1] - 1, y[2]);
var c = (b - a);
var p = c / (1000 * 60 * 60 * 24);
return p;
}
</script>
<%
String b = "2013/07/12";
String c = "2013/07/14";
pageContext.setAttribute("b", b);
pageContext.setAttribute("c", c);
%>
<script>
var myVar = chk('<c:out value="${b}" />', '<c:out value="${c}" />');
</script>
<body>
<%
String st = "<script>document.writeln(myVar)</script>";
out.println("value=" + st);
%>
</body>
As said in my comment on your question, this looks like an exercise for practicing about scriptlets, EL, JSTL and JavaScript integration. This kind of code is not meant to be used in a live production system NEVER. Scriptlets usage is discouraged since long time. Refer to: How to avoid Java code in JSP files?. Also, show this to your teacher, professor or whoever is teaching you about Java web development.