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.

3
  • 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. Commented Apr 22, 2014 at 6:12
  • what kind of element with the id "box"? Commented Apr 22, 2014 at 6:42
  • What markup does this JSP generate? Commented Apr 25, 2014 at 10:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Still displays nothing..
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?
Yes I am getting the alert and I've ensured that logs is not empty
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...)
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.

4 Comments

Nope doesn't work, it fails to show even the alert
@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.
Yes it shows upload 182: my not defined where upload is my servlet and line 182 is blank
@Zedai if you could plz post your whole program i would see what's the problem