0

I have a servlet load_plan, which its doPost method loads a plan with this code:

int id = Integer.parseInt(request.getParameter("id"));

I have another servlet hire_new_plan, which its doGet method sends the id to the load_plan servlet.

hire_new_plan doPost() --> load_plan doGet().

How can I send the id from the doGet method so that I can capture it with the request.getParameter() in the doPost method of the receiver servlet?

There are two problems as I have read.

First of all, I can't call the doPost method from another servlet's doGet method.

Secondly, it seems as if the request.setAttribute("id", id) doesn't match with the int id = Integer.parseInt(request.getParameter("id"));. I execute in the receiver servlet.

What can I do to fix this?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
mikizas
  • 181
  • 1
  • 10
  • You can *forward* to another servlet, that is pass the control to that servlet and never come back, *include* it that is call its service method (and appropriate doGet or doPost or...) and continue after the include, or *redirect* that is send a special response to client browser to ask it to send a new request to another url. Every solution will be implemented differently, so what do you want to do exactly? I assume you do not want to redirect since you want to hit a `doPost` (redirect can only GET), but do you want to forward or include ? – Serge Ballesta Oct 01 '15 at 14:06
  • I want to forward it, since I don't need to return the control to the first Servlet. – mikizas Oct 01 '15 at 16:16

1 Answers1

1

You can use RequestDispatcher to forward to other servlet.

RequestDispatcher rd = request.getRequestDispatcher("load_plan");
rd.forward(request, response);
//or rd.include(request, response);

While forwarding same request object is sent to the called servlet.So you do not need to do anything.It will be available in that request object by default.

If you go this way,doGet will get called of another servlet. If you can't move your logic in that,I would suggest you to use HttpURLConnection object.You can fire POST request programmatically like this

HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8080/WebAppName/SecondServlet").openConnection();
connection.setRequestMethod("POST");
InputStream response = connection.getInputStream();
// ... Write to OutputStream of your HttpServletResponse

See More

Community
  • 1
  • 1
Prince
  • 2,577
  • 2
  • 12
  • 31
  • 1
    You should at least wrap the request, because it is a GET request and OP want to process it in a doPost method. – Serge Ballesta Oct 01 '15 at 14:07
  • Yeah, That's what I am doing, but it is not working. I am doing request.getRequestDispatcher("/user_area/load_plan").forward(request, response); but it goes to the doGet method, and I need it to go to the doPost method. Also I need to pass the parameter from the 1st servlet to the second one, so that I can get it using request.getParameter('id') in the second Servlet. – mikizas Oct 01 '15 at 16:18
  • yes if you forward it,it goes to the doGet method.I would suggest you to move your logic in doGet and if you cant do that,i would suggest you to use HttpURLConnection object to call doPost method.Editing my answer. – Prince Oct 02 '15 at 08:57