开发者

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 开发者_如何学Govariable. How to do it?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜