Adrià
Adrià

Reputation: 207

Call a jsp function by submitting a form

I want to execute a jsp function when a submit button is clicked. Here's what I'm trying to do:

<form>
    <input type="text" id="input">
    <input type="submit" onClick="(I want the function to be here)">
</form>

<@!
public static void get(){
    String s = request.getParameter("input");
    System.out.println(s);
}

(I have simplified the code, if you don't understand please let me know.)

I don't really know if this is possible in anyway, I have been searching and I have seen suggestions on similar cases of using AJAX, but I'm not familiar with it and if there is a simpler solution it would be so much easier.

Edit: I just wanted to specify that the function I'm trying to call isn't this simple, I simplified. The actual function uses code that only Java can run (using a Java library).

Upvotes: 1

Views: 4505

Answers (3)

AbA2L
AbA2L

Reputation: 110

You can try with this code:

if(request.getParameter("btnSubmit")!=null) //btnSubmit is the name of your button, not id of that button.
{
java.util.Date d = new java.util.Date();
System.out.println(d.toString()); 
}

<input type="submit" id="btnSubmit" name="btnSubmit" value="Execute Test"/>

Source: onClick function call using scriptlet

Upvotes: 0

Jonathan Laliberte
Jonathan Laliberte

Reputation: 2725

This is how you can make a simple ajax request from your jsp file to some java code:

Returning String as plain text

The jsp page:

  <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>SO question 4112686</title>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $(document).on("click", "#somebutton", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
                    $.get("someservlet", function(responseText) {   // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                        $("#somediv").text(responseText);           // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
                    });
                });
            </script>
        </head>
        <body>
            <button id="somebutton">press here</button>
            <div id="somediv"></div>
        </body>
    </html>

Servlet doGet() method:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String text = "some text";

   //you can write whatever java code you want to have here, and you can pass the results to the jsp file through the response writer. (text) This will only work from simple strings. If you want to pass more complicated information like lists or objects you will need to convert it to a json object

    response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(text);       // Write response body.
}

If you want to do it in a form you can do it like this:

Ajaxifying an existing form

<form id="someform" action="someservlet" method="post">
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <input type="text" name="baz" />
    <input type="submit" name="submit" value="Submit" />
</form>

<script>

$(document).on("submit", "#someform", function(event) {
    var $form = $(this);

    $.post($form.attr("action"), $form.serialize(), function(response) {
        // ...
    });

    event.preventDefault(); // Important! Prevents submitting the form.
});

</script>

Servlet:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String foo = request.getParameter("foo");
    String bar = request.getParameter("bar");
    String baz = request.getParameter("baz");

    //do some java stuff here

   //then return whatever you want
response.setContentType("text/plain");  
response.setCharacterEncoding("UTF-8"); /
response.getWriter().write("hello world");     
}

Examples taken from: How to use Servlets and Ajax

Upvotes: 1

SammeAyala
SammeAyala

Reputation: 99

The onClick() function is meant to be used for JavaScript or JavaScript frameworks, you can do what you are trying to do with JS.

In JS create a function with a parameter, then, the onClick() will look like this

onClick="(myFunction(variableName))"

Inside myFunction(variableName) should look like this

myFunction(variableName){ alert("The variable is: " +variableName); }

alert("The variable is: " +variableName); will create like a pop up at the top of the webpage showing your variable value.

Upvotes: 0

Related Questions