gkiko
gkiko

Reputation: 2289

Call servlets doGet() method with RequestDispatcher

How is it possible to call doGet() method from RequestDispatcher?

RequestDispatcher rd = sc.getRequestDispatcher("/CartServlet");
rd.forward(request, response);

This code calls doPost() as the default action.

Upvotes: 0

Views: 7389

Answers (2)

N_E
N_E

Reputation: 787

Check out below link, using HttpURLConnection to send request internally by POST or GET methods. I had felt the need for this for a long long time.

Java - sending HTTP parameters via POST method easily

Upvotes: 0

AlexR
AlexR

Reputation: 115338

It calls doPost() because your original request used POST method. Generally servlets cannot "call" each other. They just can forward or redirect request. In both cases the same HTTP method that was used in original request is used.

If you want to call doGet() of other servlet it is the time to refactor your application, i.e. separate the logic implemented in doGet(), put it to other class and call this class from both servlets.

Upvotes: 5

Related Questions