9

I have JSP page -

<html>
<head>
</head>
<body>
         <%
               String valueToPass = "Hello" ; 
         %>
    <a href="goToServlet...">Go to servlet</a>
</body>
</html>

And servlet -

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

        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {

        }

        public void foo() {

        }
}

What should I write in <a href="goToServlet...">Go to servlet</a> in order to pass values (like valueToPass or maybe add the value as argument in the ) to the servlet123 ?

Can I invoke the specific method in servlet123 (like foo()) using the link in the JSP?

EDIT:

How can I call servlet in URL? My pages hierarchy is like the following -

WebContent
 |-- JSPtest
 |    |-- callServletFromLink.jsp
 |-- WEB-INF
 :    :

And I want to call the servlet123 in the folder src->control .

I tried <a href="servlet123">Go to servlet</a> but it did not find the servlet when I press on the link.

2nd EDIT:

I tried <a href="http://localhost:8080/MyProjectName/servlet123">Go to servlet</a> and it work .

Jerold Joel
  • 227
  • 1
  • 9
  • 22
URL87
  • 10,667
  • 35
  • 107
  • 174

2 Answers2

8

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).

Jerold Joel
  • 227
  • 1
  • 9
  • 22
jddsantaella
  • 3,657
  • 1
  • 23
  • 39
  • thanks , so if I use use URL like "goToServlet?param1=value1&param2=value2" which method in the servlet will be invoked ? the doGet ? – URL87 Aug 07 '12 at 07:10
  • You should use doGet. Take a look to this answer: http://stackoverflow.com/a/2349741/980472 – jddsantaella Aug 07 '12 at 07:17
  • Then use request.getParameter("method") to get the value "methodName". You should url encode any parameter values, see [URL Encoding](http://www.w3schools.com/tags/ref_urlencode.asp). If within Java you can use the built in [URLEncoder](http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html) class. – mrswadge Aug 07 '12 at 07:31
  • @mrswadge I know about the encode . I don't know how to call the servlet in URL . – URL87 Aug 07 '12 at 07:37
  • @jddsantaella I did it , but the link not work ,I edited the post again. – URL87 Aug 07 '12 at 07:55
  • is there a way to use this approach by calling several servlets ? – roeygol Aug 25 '15 at 20:39
  • @RoeyGolzarpoor I do no understand your question. You can build links that target different servlets – jddsantaella Aug 26 '15 at 10:10
2

<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.

Kamal
  • 5,462
  • 8
  • 45
  • 58