4

I wish to call a method in JSP onClick, the method is on the same JSP inside scriptlet.

How should I archive this?

<%@ page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
<%@ page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>


<%!
    public String sendMail(String to, String sub, String msg) {
        String res = null;
        System.out.println("HI");       
        return res;
    }%>

<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
    <center>
        <h1>Send Email using JSP</h1>
    </center>
    <form>  
        <label>Email To</label><br />       
            <input type="text" name="to" /><br /> 
        <label>Subject</label><br />        
            <input type="text" name="sub" /><br /> 
        <label for="body">Message</label><br />
            <input type="text" name="msg" /><br /> 
        <input type="submit" onClick="sendMail( to, sub, msg )"/>
    </form>
</body>
</html>


Note

The methods name is "sendMail", It's called on submit button I want to do the whole code in JSP only.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62
  • 2
    You need to understand the difference between JSP and Javascript, those are not execute on the same environnement – AxelH Jan 11 '17 at 10:18
  • Possible duplicate of [jsp scriptlet function called from button click](http://stackoverflow.com/questions/7585472/jsp-scriptlet-function-called-from-button-click) or [invoking java scriptlet in JSP using HTML](http://stackoverflow.com/questions/16032899/invoking-java-scriptlet-in-jsp-using-html?rq=1). There are plenty of questions about this already – AxelH Jan 11 '17 at 10:20

3 Answers3

6

The onclick event occurs when the user clicks on an element. This attribute has the ability to call JS functions (front end)

In your case, you want to call a JAVA function (server side) so the best way is to move the java code to a servlet and use it.

Anyway if you want to keep the JAVA function in the jsp, you can do this via ajax in this way

<script type="text/javascript">
        $(document).ready(function() {
            $('#sendMailBtn').click(function (){
                $.ajax({
                    type: "post",
                    url: "/path", 
                    data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
                    success: function(msg){      
                        //
                    }
                });
            });
        });
 </script>

AJAX is a developer's dream, because you can

  • Update a web page without reloading the page
  • Request data from a server - after the page has loaded
  • Receive data from a server - after the page has loaded
  • Send data to a server - in the background
  • Check the full code here

    <%@ page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
    <%@ page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*"%>
    
    
    <%!
        public String sendMail(String to, String sub, String msg) {
            String res = null;
            System.out.println("HI");       
            return res;
        }
    %>
    
    <html>
        <head>
            <title>Send Email using JSP</title>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        </head>
    
        <body>
            <center>
                <h1>Send Email using JSP</h1>
            </center>
            <form>  
                <label>Email To</label><br />       
                <input id="email" type="text" name="to" /><br /> 
                <label>Subject</label><br />        
                <input id="subject" type="text" name="sub" /><br /> 
                <label for="body">Message</label><br />
                <input id="msg" type="text" name="msg" /><br /> 
                <input id="sendMailBtn" type="submit" />
            </form>
        </body>
    
        <script type="text/javascript">
            $(document).ready(function() {
                $('#sendMailBtn').click(function (){
                    $.ajax({
                        type: "post",
                        url: "/path", 
                        data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
                        success: function(msg){      
                            //
                        }
                    });
                });
            });
        </script>
    </html>
    

    For more information check

  • AJAX Introduction: http://www.w3schools.com/xml/ajax_intro.asp
  • onclick Event: http://www.w3schools.com/tags/ev_onclick.asp
  • Jad Chahine
    • 6,849
    • 8
    • 37
    • 59
    4

    JSP- Executes on Server.

    JavaScript - executes in browser.

    No you cannot call that JSP magically from JS. However you can send an Ajax request or post the form to jsp. BTW, I strongly suggest you to move the java code to a servlet and use it.

    Suresh Atta
    • 120,458
    • 37
    • 198
    • 307
    2

    This is what I ended up doing

        <%@ page import= "java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
        <%@ page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
        <%@ page import="javax.servlet.http.*,javax.servlet.*"%>
    
    
        <%!
              public String sendMail(String to, String sub, String msg) {
                String res = null;      
                System.out.println("HI");      
                return res;
             }
         %>
    
        <%        
        String a = request.getParameter("to");
        if(a != null){
            sendMail(request.getParameter("to"),request.getParameter("sub"),request.getParameter("msg"));
        }
        %>
        <html>
        <head>
        <title>Send Email using JSP</title>
        </head>
        <body><center>
            <form action="#" method="post"> 
                <label>Email To</label><br />       
                    <input type="text" name="to" /><br /> <br /> 
                <label>Subject</label><br />        
                    <input type="text" name="sub" /><br /> <br /> 
                <label for="body">Message</label><br />
                    <input type="text" name="msg" /><br /> <br /> 
                <input type="submit"/>
            </form>
        </center></body>
        </html>
    

    The action="#" reloads the page, and there is a if condition which calls the required method if the parameter is not blank( Please keep in mind that by default on first call the parameter will be null ).

    Abhishek Patil
    • 1,373
    • 3
    • 30
    • 62