0

In my servlet.java,

request.setAttribute("f4stat", "Somedata");
RequestDispatcher rd = request.getRequestDispatcher("my.jsp");
rd.forward(request, response);

Now, in my.jsp, in a javascript function I would like to access this f4stat data.

How to do it in Javascript?

I've tried,

var x = '<%= request.getAttribute("f4stat")%>'
if (x.length == 0)
    {
        document.getElementById("display").innerHTML = "<b> data </b>";
    }

But this is not working as in my <div id="display"> no content is being displayed even when f4stat has no value.

1
  • 1
    u may update this line in jsp: var x = '<%= request.getAttribute("f4stat")%>' to this one: var x = '${requestScope.f4stat}'; Commented Apr 14, 2014 at 9:48

1 Answer 1

1

You can not access HttpRequest ,HttpResponse objects of servlet in JavaScript. But This is possible by trick.

You Can Have Some Hidden Fields in my.jsp and assign value.

<script>
    function getF4(){
          document.getElementById("display").innerHTML=document.getElementById("f4").value;
    }

</script>

     <input type="hidden" id="f4" value=<%= request.getAttribute("f4stat") %>  />
     <input type="button" value="click" onclick="getF4()" />
     <div id="display"></div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.