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 9222561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:51:42+00:00 2026-06-18T03:51:42+00:00

public class Test extends HttpServlet { private static final long serialVersionUID = 1L; public

  • 0
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;

public Test() {
    super();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    out.println("<HTML><HEAD><TITLE>MovieDB</TITLE></HEAD>");
    out.println("<BODY><H1>MovieDB</H1>");

    out.println("<a href = '#' onclick = 'on_Click();'> Call Function </a>");

}

public void on_Click()
{
    System.out.println("HELLO");
}
}

I just want the HTML Link on my page to call my java function on_Click(), what is a good way to do this?

  • 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-18T03:51:43+00:00Added an answer on June 18, 2026 at 3:51 am

    The onclick is not Java. It’s JavaScript. It’s a completely different language than Java. The only things which they’ve in common are the first 4 characters of the language name, some keywords and a some syntax. But that’s it.

    Just let the link point to an URL which matches the URL pattern of the servlet mapping. Imagine that you’ve mapped your servlet on an URL pattern of /foo/*, then just use

    <a href="foo">Call function</a>
    

    This will just call the servlet’s doGet() method.

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hello");
    }
    

    If you want to reuse the same servlet for multiple actions, just pass some action identifier along as request parameter

    <a href="foo?action=bar">Call function</a>
    

    with in doGet() of servlet

    String action = request.getParameter("action"); // "bar"
    

    or as path info

    <a href="foo/bar">Call function</a>
    

    with in doGet() of servlet

    String action = request.getPathInfo().substring(1); // "bar"
    

    No need for weird JavaScript approaches/workarounds.

    See also:

    • Our Servlets wiki page

    Unrelated to the concrete problem, HTML belongs in JSP, not in Servlet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public class Test extends JFrame implements ActionListener{ private static
My code is : public class EventDataSQLHelper extends SQLiteOpenHelper { private static final String
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
I have following code public class TEST { public static void main(String arg[]){ try
The code I am using is: public class Test extends JFrame implements ActionListener {
Here is my code: public class Test { static { main(null); } public static
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class test extends JFrame implements MouseListener {
My Project is Named A and my class is: @WebServlet(urlPatterns={/test/*}) public class RequestHandler extends
I have the extremely simple AppWidgetProvider for a test widget: public class Test extends

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.