pass values from jsp to servlet using <a href>

If you want to send parameters to the servlet using an URL, you should do it in this way

<a href="goToServlet?param1=value1&param2=value2">Go to servlet</a>

And then retrieve the values that will be available in the request.

Regarding your second question. I will say no. You can add a param in the URL, something like

<a href="goToServlet?method=methodName&param1=value1">Go to servlet</a>

And the use of that information to call a specific method.

By the way, if you use a framework like Struts, that will be easier since, in Struts, you can bound an URL to a specific Action method (let's say "servlet")

Edited:

You have defined your servlet in this way:

@WebServlet("/servlet123")

You, your servlet will be available on /servlet123. See doc.

I have tested your code and it is working:

@WebServlet(name = "/servlet123", urlPatterns = { "/servlet123" })
public class Servlet123 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("<h2>Hello Friends! Welcome to the world of servlet annotation </h2>");
        out.write("<br/>");
        out.close();
    }
}

Then, I called the servlet in http://localhost:8080/myApp/servlet123 (being myApp your application context if you are using one).


<a href="url">urltitle</a> allows you to define a url. Calling a servlet from here is as good as calling it from a browser, just give the url as you would give it in browser to call the servlet like http://mysite.com?param1=val1&param2=val2 etc.