SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    ${myString};


    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);
    }
    </script>

    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..

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This looks quite dodgy... it's not valid JavaScript:

    Code:
    ${myString};
    Or is that something JSP-related?

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate the replies so far

    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..

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused The javascript is contained within the JSP file, doesn't that mean the JSP is generating it?

    I just tried to use alert (<&#37;= Math.random() %>); and it works. That's also JSP..

    Why would <%= Math.random() %> work whereas <%= request.getAttribute("myString")%> wouldn't?

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still can't get this to work.. Anyone?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind.. problem has been solved in the other forum.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •