Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 848
URL87 Avatar asked Aug 07 '12 06:08

URL87


People also ask

Can we call servlet from href?

getContextPath() is used to return the portion of the request URL and specifies the context of the request. You have to put the servlet url of desired servlet as href="<%=request. getContextPath()%>/CallServlet? . CallServlet is the url of the Servlet.

How we can communicate between JSP and servlet?

A Servlet can communicate with JSP by using the RequestDispatcher mechanism. RequestDispatching is the process hand overing the request to another web component,and this component takes the response of generating the response.


1 Answers

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

like image 153
jddsantaella Avatar answered Sep 22 '22 10:09

jddsantaella