0

Is it possible to call a method in Java by button onclick in JSP?

For example:

Java:

@UrlBinding("/Student/import.action")
public class StudentAction implements ActionBean {

  public void testMe() {
      //do something
    }
}

JSP

<stripes:form beanclass="com.example.action.StudentAction">
   <input type="button" value="Test" onClick="call testMe"/>
</stripes:form>

I read some posts on Internet that it could be done by Ajax/jQuery, but I couldn't understand how they do it. I just know itshould be something like this:

$.ajax({
    type:"POST"
    url: 
})

or

$(document).ready(function() {
    $("#button").click(function(){

    })
})

is there any other way to do this, and if no, I would appreciate a simple explanation of how to do it with Ajax/JQuery.

Note: I don't want to use submit input or stripes:submit!

1
  • What about learning basics about Ajax, HTTP and JSP? Commented Feb 12, 2015 at 10:35

2 Answers 2

1

It will not call Java method, you have to write Servlet then call the doGet() or doPost()method of Servlet on form submission or using ajax call. refer this example

Sign up to request clarification or add additional context in comments.

1 Comment

Writing a pure servlet, when using a servlet framework seems like a bad idea to me.
0

If you have an ActionBean (which is a servlet deep down), you can bind an URL to it with @UrlBinding (as you already do). You just need to make a HTTP request to that URL using Ajax in the 'click' event handler of the button.

So you could have this markup:

<stripes:form beanclass="com.example.action.TestMeActionBean">
    <input type="button" value="Test" onClick="testMe()"/>
</stripes:form>
<script>
function testMe () {
    $.ajax({
        type: 'post',
        url: 'action/testMeAction',
        ...
    });
}
</script>

On the server side, you just need to create the handling action:

@UrlBinding("/action/testMeAction")
public TestMeActionBean implements ActionBean {

After this, you only need to write a handler method. For example, using your method declaration:

    @DefaultHandler
    public void testMe() {
        //do something
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.