0

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?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
drs_india
  • 21
  • 5

2 Answers2

1

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);
}

}
Aditya Ekbote
  • 1,493
  • 3
  • 15
  • 29
  • If my answer is usefull to you, will you please vote up my answer. Thanking You. – Aditya Ekbote Apr 18 '14 at 08:16
  • Hi aditya, Ur answer is good. but still want some help from you. I have server.java and client.java. when i click on on button, server has to start and message has to go to ip address.please give me idea.. – drs_india Apr 18 '14 at 10:51
0

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

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56