URL87
URL87

Reputation: 11012

pass values from jsp to servlet using <a href>

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 .

Upvotes: 9

Views: 70156

Answers (2)

jddsantaella
jddsantaella

Reputation: 3687

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

Upvotes: 8

Kamal
Kamal

Reputation: 5522

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

Upvotes: 2

Related Questions