Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: call a JSP method

  1. #1
    sauravsinha is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Thumbs down call a JSP method

    how can we call a JSP method within a javascript function?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    See this below sample code for calling jsp meth in javascript.

    <%!
    String hai()
    {
    return "Iam Ramya";
    }
    %>
    <HTML>
    <HEAD><TITLE> Example of onClick Event Handler</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function callJsp(){

    alert("<%= hai()%>");
    }
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR="FFFFFF" TEXT="000000">

    <form name="myform">
    <INPUT TYPE="button" VALUE="Click Here" onClick="callJsp()">
    </form>
    </BODY>
    </HTML>
    Ramya:cool:

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Except, of course, that method is not actually being called in the Javascript.
    It's being called on the server when the JSP is run, which puts a value into that place, which is then used by the Javascript on the client.
    Very different to it actually being called by the Javascript.

    In short, you can't. JSP code runs on the server, and Javascript on the client.

    You can make calls to the server to do stuff, but I doubt that's what you want.
    Fubarable likes this.

  4. #4
    seenuvasan is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: call a JSP method

    Hi Ramya,
    i have one doubt in ur code.
    how to pass params to jsp method from js.
    ex:
    <SCRIPT LANGUAGE="JavaScript">
    function callJsp(){
    var name="seenu";
    alert("<%= hai(name)%>");
    }
    </SCRIPT>

    in above code shows error . how to pass name params to jsp method?.

    Quote Originally Posted by RamyaSivakanth View Post
    See this below sample code for calling jsp meth in javascript.

    <%!
    String hai()
    {
    return "Iam Ramya";
    }
    %>
    <HTML>
    <HEAD><TITLE> Example of onClick Event Handler</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function callJsp(){

    alert("<%= hai()%>");
    }
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR="FFFFFF" TEXT="000000">

    <form name="myform">
    <INPUT TYPE="button" VALUE="Click Here" onClick="callJsp()">
    </form>
    </BODY>
    </HTML>

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: call a JSP method

    You can't (as I said 18 months ago).
    "name" exists on the client.
    hai() exists on the server.

    Unless your client makes a call (say an Ajax call) to the server then they will never meet.

Similar Threads

  1. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  2. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  3. Call java method from jsp
    By saiphani723 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 11:21 AM
  4. How to call method in servet by using JSP?
    By frankjava1 in forum Java Servlet
    Replies: 2
    Last Post: 10-24-2008, 04:20 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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