Monday, June 21, 2010

Accessing a string variable which is in java file from javascript via jsp page

In java file:
Random random = new Random();
String ranLong = String.valueOf(random.nextLong());
session.setAttribute("hiddenvalue", ranLong);// this will sets the ranLong value in to the hiddenvalue
//ranLong variable contains some 10 digit number

In jsp file:
<%
String hiddenvalue1 = (String) session.getAttribute("hiddenvalue");
%>
now send this 'hiddenvalue1' to javascript
<body onload="setValue('<%= hiddenvalue%>')">

In javascript file:
function setValue(val){
document.getElementById("tf_CheckValue").value = val;
}

No comments:

Post a Comment