Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:06:42+00:00 2026-06-17T18:06:42+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:06:43+00:00Added an answer on June 17, 2026 at 6:06 pm

    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");
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna know that how to call JFrame from jsp. When I click button
I'm trying to call method from .jar But when I want to declare array
I'm trying to call a method from another class with a simple button in
I have a problem when call applet method from javascript.. I used this function
I wish to call a java class method from JSP whenever I submit my
I am writing a jsp page that uses a scriptlet to call a method
I have a question like when i call the jsp page from within iframe,
As title says, how do I call a java class method from a jsp,
I am trying to call a Java method from the code. C code listens
How do I call a method from a class in a ruby gem? i.e.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.