How to use Jsp Implicit objects in javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsuns82
    New Member
    • Mar 2007
    • 58

    How to use Jsp Implicit objects in javascript?

    Hi,
    I am trying use jsp implicit objects in side a java script method.
    can any one tell me how to implement this requirement?

    <script>
    function fun()
    {
    var path = <%=request.getC ontextPath()%>;
    }
    </script>

    is the above mentioned a valid one,if not wats the right way to achieve this?

    regards,
    sundar.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You should be aware that javascript runs on client side and jsp-code on server side.
    You can only pass results (strings, numbers), calculated from impicit objects, to the client, but you cannot pass serverside objects directly or manipulate them. These results can be calulations from implicit objects as you have shown in your code.

    therefore you don't pass the implicit object "request", but you can pass a string that its method getContextpath produces. With this knowledge you should be aware that you have to use quotation marks here. So to make it working, you should correct your code to

    Code:
    var path = [B]"[/B]<%=request.getContextPath()%>[B]"[/B];

    By the way:
    There is only one way of accessing/manipulating objects on the server from the clientside javascript : Ajax.
    But again, not directly, but manipulating them in an indirect way.

    Comment

    • gsuns82
      New Member
      • Mar 2007
      • 58

      #3
      Originally posted by chaarmann
      You should be aware that javascript runs on client side and jsp-code on server side.
      You can only pass results (strings, numbers), calculated from impicit objects, to the client, but you cannot pass serverside objects directly or manipulate them. These results can be calulations from implicit objects as you have shown in your code.

      therefore you don't pass the implicit object "request", but you can pass a string that its method getContextpath produces. With this knowledge you should be aware that you have to use quotation marks here. So to make it working, you should correct your code to

      Code:
      var path = [B]"[/B]<%=request.getContextPath()%>[B]"[/B];

      By the way:
      There is only one way of accessing/manipulating objects on the server from the clientside javascript : Ajax.
      But again, not directly, but manipulating them in an indirect way.

      thanks a lot,good one

      Comment

      Working...