proshantoS
proshantoS

Reputation: 55

Can not pass jsp value to javascript

I am working on a jsp application. I have a bean user. In javascript I have the user id. I want to send it to javascript. With the getter method of id - getId() I have tried the following thing but it not working -

<script type="text/javascript">
    var userId = '<% getId()%>';
    alert("User Id is: " + userId);
</script>

Am I doing something wrong? Can any one help me.

Thanks

Upvotes: 0

Views: 86

Answers (2)

Ramesh Kotha
Ramesh Kotha

Reputation: 8322

Its not recommended to use JSP Scriptlets, Set User Id in session and access that in JSP using EL expression

<script type="text/javascript">
    alert("User Id is: " + ${userId});
</script>

Upvotes: 1

Razib
Razib

Reputation: 11153

You are using '<% %>'. You have to use "<%= %>". Your code be written as -

<script type="text/javascript">
    var userId = "<%=getId()%>";
    alert("User Id is: " + userId);
</script>

I think this should solve the problem

Upvotes: 0

Related Questions