Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a specific Java method on a click/submit event of a specific button in JSP?

Tags:

jsp

servlets

My Java file is:

public class MyClass {      public void method1() {             // some code     }      public void method2() {         //some code     }      public void method3() {         //some code     } } 

In my JSP page I have three HTML buttons.

If I click on button1, then only method1 will be called, if I click on button2 then only method2 will execute, and if button3, then only method3, and so on.

How can I achieve this?

like image 288
Sarde Avatar asked Feb 06 '13 07:02

Sarde


People also ask

How can we call a function in button click in JSP?

No you cannot call that JSP magically from JS. However you can send an Ajax request or post the form to jsp. BTW, I strongly suggest you to move the java code to a servlet and use it. Show activity on this post.

How do I run a Java program by clicking a button in HTML?

In that case, the solution may be to add your <object> to a <div> that has a css-property of visibility:hidden; . Then create a HTML-button element that accesses the <DIV> element and changes its visibility property, e.g. You will need to adapt it to your purposes, but it should show the basic idea.

How do you call a Java method from HTML?

Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"

Can we call Java method from JavaScript?

JavaScript cannot call java method directly since it is on the server. You need a Java framework like JSP to call when a request is received from JavaScript.


1 Answers

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

You only need to make sure that the button inputs have type="submit" as in <input type="submit"> and <button type="submit"> and not type="button", which only renders a "dead" button purely for onclick stuff and all.

E.g.

<form action="${pageContext.request.contextPath}/myservlet" method="post">     <input type="submit" name="button1" value="Button 1" />     <input type="submit" name="button2" value="Button 2" />     <input type="submit" name="button3" value="Button 3" /> </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.method1();         } else if (request.getParameter("button2") != null) {             myClass.method2();         } else if (request.getParameter("button3") != null) {             myClass.method3();         } else {             // ???         }          request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);     }  } 

Alternatively, use <button type="submit"> instead of <input type="submit">, then you can give them all the same name, but an unique value. The value of the <button> won't be used as label, you can just specify that yourself as child.

E.g.

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

with

@WebServlet("/myservlet") public class MyServlet extends HttpServlet {      @Override     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         MyClass myClass = new MyClass();         String button = request.getParameter("button");          if ("button1".equals(button)) {             myClass.method1();         } else if ("button2".equals(button)) {             myClass.method2();         } else if ("button3".equals(button)) {             myClass.method3();         } else {             // ???         }          request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);     }  } 

See also:

  • Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
  • How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
like image 197
BalusC Avatar answered Oct 15 '22 05:10

BalusC