0

I am executing a query in jsp and using resultSet.getString() to get that field.

String eKnowledge = resultset.getString("knowledge");

Then using request.setAttribute() to set that field in jsp.

request.setAttribute("actualKnowledge", eKnowledge);

I am using request.getAttribute() in javascript to get that object.

<script type="text/javascript"> 

    function moreInformationDisplay() {
        var jsKnowledge = <%=request.getAttribute("actualKnowledge")%>;
        alert(jsKnowledge); 
    }

</script>

But the alert message shows null. Can someone please help me with this problem.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Dhruv Kadia
  • 71
  • 1
  • 12
  • `<%=request.getAttribute("actualKnowledge")%>` - this is pre-processed on the server side before the page is loaded into the browser - are you hoping for some magic two way communication between server and client without using AJAX? – Jaromanda X Mar 16 '17 at 23:27

1 Answers1

0

If your javaScript block is inserted in a valid JSP page, after the call to .setAttribute and resultset.getString("knowledge") is not null, then your code should work fine.

However this is bad practice and should be avoided. You should be using JSTL How to avoid Java code in JSP files?

Your code could also be vunerable to XSS injection, if eKnowledge contains user input. It would be a good idea to escape it with a tool like OWASP Java encoder

Community
  • 1
  • 1