getAttribute from servlet

Hi gurus,

I have a quick servlet that forwards the following to a jsp:

request.setAttribute(“myString”, myPointList);
RequestDispatcher view = getServletContext().getRequestDispatcher(“/myjsp”);
view.forward(request, response);

In my JSP file, I can access the content with:

This may be a silly question, but here is my dilemma. I have a javascript in that JSP file:

<script type=“text/javascript”>
function onload() {
var testString = ${myString};
alert (testString);

Nothing displays… If I do alert (“TEST”) it will work, but otherwise it will not. Is there a way to quickly grab that value?

Thanks in advance…

How is the onload function being called? Just because the function is called onload doesn’t mean that it will load when the page loads.

Other than that, in relation to JSP someone other than myself would need to advise specifically about JSP events.

This looks quite dodgy… it’s not valid JavaScript:


Or is that something JSP-related?

I appreciate the replies so far :slight_smile:

The onload function is called by the body when the page loads. I can confirm that it loads because if I use ‘alert(“TEST”)’ the popup will show correctly.

Also, the ${myString} is a EL markup of JSP. Could it be that JSP syntax won’t work inside javascript? I’ve tried calling ‘(String) request.getAttribute(“myString”)’ which is also a JSP syntax but that doesn’t seem to work either. And if that’s the case, how can I retrieve servlet forwarded information from inside a javascript?

Thanks again…

No, JSP syntax won’t work in JavaScript. It’s like saying, will French work in Uzbek? It won’t. They’re different languages.

The only scenario where it would work is if the JSP is generating the JavaScript, just like it would generate any amount of text.

I’m confused :confused: The javascript is contained within the JSP file, doesn’t that mean the JSP is generating it?

I just tried to use alert (<%= Math.random() %>); and it works. That’s also JSP…

Why would <%= Math.random() %> work whereas <%= request.getAttribute(“myString”)%> wouldn’t?

I still can’t get this to work… Anyone?

Nevermind… problem has been solved in the other forum. :slight_smile: