coderslay
coderslay

Reputation: 14370

How to post a data from a servlet in a variable to a servlet?

I have performed uploading of a file to a servlet. Now I want to perform some action which will transfer me to another servlet. I have generated some string from this uploaded data and now I need to post it to another servlet which will catch that string from a variable. How to do it?

Upvotes: 0

Views: 1171

Answers (1)

rsp
rsp

Reputation: 23373

You can forward (server side) the request to the next servlet:

RequestDispatcher dispatcher = request.getRequestDispatcher("/nexturl");
dispatcher.forward(aRequest, aResponse);

You can attach the decoded variable to your session object and retrieve it from there in the servlet you forward to. (Or, if the servlet can be called with a parameter too, check the session for the variable (remove it when you use it) and if it's not there try to parse the apropriate parameter.)

Update

To use the HTTP session as a way to pass your variable, add it:

HttpSession session = request.getSession();
session.setAttribute("name", "value");

and retrieve it in the next servlet:

HttpSession session = request.getSession();
String value session.getAttribute("name");
session.removeAttribute("name");

The session is created automatically by the servlet container, if uses a session cookie to map session state to a series of HTTP requests from the same browser session.

Upvotes: 3

Related Questions