user3504024
user3504024

Reputation: 149

want to write multiple doget method in same servlet class is it possible .....if so ...how.?

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
    {
        String param1 = req.getParameter("x");
        String param2 = req.getParameter("y");
        String param3 = req.getParameter("z");
        int x = Integer.parseInt(param1);
        int y = Integer.parseInt(param2);
        int z = Integer.parseInt(param3);

Upvotes: 3

Views: 4778

Answers (3)

You can do this if you want. But I do not recommend this. When you call GET, send another parameter, something like a function name, then use one get method, check the parameter sent before, define two functions and put the two logics that were put in your get.

Upvotes: 0

RockAndRoll
RockAndRoll

Reputation: 2277

doGet(HttpServletRequest req,HttpServletResponse res) is picked by the servlet container.So Yes You can have overloaded versions of doGet but they wont be recognized by Servlet Container.So it does not make sense to have multiple doGet.

Instead Overriding the doGet and doPost Methods in servlet makes sense.
Refer this for more info.

Upvotes: 2

Nicholas Robinson
Nicholas Robinson

Reputation: 1419

Why do you want more that one doGet in the same servlet, that kind of defeats the point. You would either have one Servlet that dispatches control to the appropriate controller (Prefered), or you would have one Servlet per request.

The doGet is an inherited method from HttpServlet, so you can overload the doGet method, but that those overloads can not be called. Since your implementation will be injected into the client, the client has coded using the HttpServlet class and your implementation is called polymorphically. As such, the client will only have access the the methods defined in HttpServlet and your overloaded implementations will not be known.

I hope this helps.

Upvotes: 3

Related Questions