0

I want to access values on JSP in Javascript as follows,

<%String logs = (String)request.getAttribute("logs");
String errlog = (String)request.getAttribute("ErrLogs");%>

<script type='text/javascript'>

$( "#logopt" ).change(function() {
  if($("#logopt option:selected").text()=="Complete Log")
  {
    alert("hi");
    document.getElementById("box").innerHTML += '<%=logs%>';    
  }
});

but this displays nothing, what is my mistake? what is the correct method to access JSP values using scriptlets?

I've referred this answer and tried '&lt;%=logs;'; but this is displaying <%=logs%> but not the value in logs.

Community
  • 1
  • 1
  • 1
    There is vey little point in looking at some JSP and saying "the JS this generates doesn't work". Look at the JS it generates. Figure out why that JS doesn't work. Then worry about changing the JSP to output correct JS. – Quentin Apr 22 '14 at 06:12
  • what kind of element with the id "box"? – Frank Yeung Apr 22 '14 at 06:42
  • What markup does this JSP generate? – Aron Apr 25 '14 at 10:01

2 Answers2

1

I believe if you change

document.getElementById("box").innerHTML += <%=logs%>

to

document.getElementById("box").innerHTML += '<%=logs%>';

it will work. You will still need to have the string literal in single/double quotation marks to have valid resulting JavaScript.

UweB
  • 4,080
  • 2
  • 16
  • 28
  • Still displays nothing.. –  Apr 22 '14 at 06:11
  • Have a look at the resulting HTML page in your browser's debugger. Chances are that you will see: `document.getElementById("box").innerHTML += ''`, meaning your logs variable was empty. Do you get the actual `alert` you put in there? – UweB Apr 22 '14 at 06:13
  • Yes I am getting the alert and I've ensured that logs is not empty –  Apr 22 '14 at 06:14
  • The only other thing I can think of is that the DOM `box` element is hidden. I don't think that `innerHTML` is ever `null` (which would cause the `+=` to produce a script error...) – UweB Apr 22 '14 at 06:18
0

Try this

  <%String logs = (String)request.getAttribute("logs");
  String errlog = (String)request.getAttribute("ErrLogs");%>

 <script type='text/javascript'>
   var logs1=<%=logs %>
  $( "#logopt" ).change(function() {
 if($("#logopt option:selected").text()=="Complete Log")
  {
 alert("hi");
 document.getElementById("box").innerHTML += logs1;    
 }
 });

Feel free to ask anything and plz respond it worked or not.

TruePS
  • 493
  • 2
  • 12
  • 33
  • Nope doesn't work, it fails to show even the alert –  Apr 22 '14 at 06:19
  • @Zedai when run your program in Mozilla firefox right click mouse button and click inspect element and see if it is showing any error and tell me that error. – TruePS Apr 22 '14 at 06:23
  • Yes it shows `upload 182: my not defined` where upload is my servlet and line 182 is blank –  Apr 22 '14 at 06:24
  • @Zedai if you could plz post your whole program i would see what's the problem – TruePS Apr 22 '14 at 06:25