drs_india
drs_india

Reputation: 21

calling java program from Jsp on click

I have a JSP page with two buttons. One is On and other one is OFF.
If I click on ON button in JSP, On click some predefined string will have to send to IP address.

How can we call Java program from JSP on click button?

Upvotes: 0

Views: 575

Answers (2)

Aditya Ekbote
Aditya Ekbote

Reputation: 1553

Just give the individual button elements an unique name. When pressed, the button's name is available as a request parameter the usual way like as with input elements

E.g.

<form action="${pageContext.request.contextPath}/myservlet" method="post">
    <input type="submit" name="button1" value="Button 1" />
</form>

with

@WebServlet("/myservlet") public class MyServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    MyClass myClass = new MyClass();

    if (request.getParameter("button1") != null) 
    {
        myClass.function1();
    } 
    else 
    {
        // ???
    }

    request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);
}

}

Upvotes: 1

Santhosh
Santhosh

Reputation: 8217

You can call it using Ajax . AJAX request will invoke any java program you want by sending the request to the server. see this for more info.

There are also other possible options you can use DWR for secure transactions.

See here for jquery ajax post . Also here is good example for using it with servlets.

Hope this helps !!

side-note: If you need any specific help , please post us the code which you are trying

Upvotes: 0

Related Questions