user1733583
user1733583

Reputation:

Passing servlet values into <div>

on my Java Servlet I have something like this,

request.setAttribute("InfoLog", info);
RequestDispatcher rd = request.getRequestDispatcher("gc.jsp");

and on my jsp page I have a <div>

<div id="box"></div>

Now using Javascript I want to get the servlet values InfoLog and populate that into my div tag, the purpose of this is that I am verifying some conditions in my Javascript function.

How do I get servlet values in Javascript?

Upvotes: 0

Views: 1772

Answers (2)

balaji
balaji

Reputation: 794

In the jsp, you get the value from Servlet as below,

    <% String infoLog = (String)request.getAttribute("InfoLog"); %>

and use this infoLog variable in the div as

    <div id="box"><%=infoLog%></div>

and in the javascript function particulary in that if condition you can have below code

    if(val == "InfoLog")
{
    var infoLog = '<%=infoLog%>';

}

Thanks, Balaji

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

You can simply get your attribute in your gc.jsp

<div id="box"> <%=request.getAttribute("InfoLog")%> </div>

Then, if you want to get this value in javascript you can write -

var val = document.getElementById("box").innerHTML;

Upvotes: 0

Related Questions