user1851366
user1851366

Reputation: 346

Call a bean in JSP from button

How do i call a method from a jsp when i click in a button?

I wrote this code,but dont work..

<%@page import="my.package.class.MyClass" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
<input type="submit" name="myButton" value="button" onclick="callMethod()"/>
</body>
</html>

<%! 
void callMethod(){ 
new MyClass().print();} %>

there is any way more easy? or this is the right way for to do?

ps: I dont want to use javascript

edit: My class just have a method "print",that prints something like "test" in system.out.println

Upvotes: 2

Views: 13901

Answers (3)

BalusC
BalusC

Reputation: 1109402

You need a servlet ( <-- click the link, it isn't included for decoration only).

To the point, provided that you're targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, etc), then just create this class:

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        new MyClass().print();
    }

}

And fix your JSP's <body> as follows (provided that JSP file is placed in root of web content and not in a subfolder):

<form action="hello" method="post">
    <input type="submit" name="myButton" value="button" />
</form>

That's it.

The servlet is via @WebServlet registered to listen on an URL of /hello, relative to context root. The HTML form is instructed to submit to exactly that URL, relative to the URL of the JSP page itself. The method="post" will invoke the servlet's doPost() method wherein you've all the freedom to write down the desired Java code.

If you want more finer grained control depending on the button pressed, then give the button an unique name.

<form action="hello" method="post">
    <input type="submit" name="myButton1" value="button1" />
    <input type="submit" name="myButton2" value="button2" />
    <input type="submit" name="myButton3" value="button3" />
</form>

then you can just check the button pressed as follows in servlet's doPost() method:

if (request.getParameter("myButton1") != null) {
    // button1 is pressed.
}
else if (request.getParameter("myButton2") != null) {
    // button2 is pressed.
}
else if (request.getParameter("myButton3") != null) {
    // button3 is pressed.
}

A completely different alternative is to go for an existing MVC framework which abstracts all this boilerplate away in a high degree, such as JSF, Spring MVC, Struts2, etc.

With JSF, it would look like this (you only need to replace legacy JSP by its successor Facelets; how Facelets look like is explained in a bit sane JSF tutorial, again, click the "JSF" link in previous paragraph for details):

<h:form>
    <h:commandButton value="button1" action="#{bean.button1}" />
    <h:commandButton value="button2" action="#{bean.button2}" />
    <h:commandButton value="button3" action="#{bean.button3}" />
</h:form>

with just this class without ugly if/elses:

@ManagedBean
@RequestScoped
public class Bean {

    public void button1() {
        System.out.println("button1 invoked");
    }

    public void button2() {
        System.out.println("button2 invoked");
    }

    public void button3() {
        System.out.println("button3 invoked");
    }

}

Upvotes: 5

Nick Holt
Nick Holt

Reputation: 34321

You are confusing where the bits of code involved in a JSP and the resulting HTML, which the browser displays, are running.

The bean that is accessible inside the JSP is in the servlet container (on the server) at the time the HTTP request is being processed and the HTML is generated from the JSP, while the button that fires the onclick event is inside the clients browser - therefore the button cannot invoke the bean directly.

In order to invoke server-side logic when the button is clicked you need to make an AJAX call back to the server (unless you're going to refresh the whole page, which is a bit crap).

Upvotes: 0

Noah Martin
Noah Martin

Reputation: 1809

why not use servlets and MVC pattern, you have no need to write java methods within JSP.

Upvotes: 0

Related Questions