简体   繁体   English

如何将数据从变量中的servlet发布到servlet?

[英]How to post a data from a servlet in a variable to a servlet?

I have performed uploading of a file to a servlet. 我已经将文件上传到servlet。 Now I want to perform some action which will transfer me to another servlet. 现在,我想执行一些操作,将我转移到另一个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. 我已经从上传的数据中生成了一些字符串,现在我需要将其发布到另一个servlet中,该servlet将从变量中捕获该字符串。 How to do it? 怎么做?

You can forward (server side) the request to the next servlet: 您可以将请求转发(服务器端)到下一个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. 您可以将已解码的变量附加到会话对象,并在转发到的servlet中从那里检索它。 (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.) (或者,如果也可以通过参数调用servlet,请检查会话以获取变量(使用时将其删除),如果不存在该变量,请尝试解析适当的参数。)

Update 更新资料

To use the HTTP session as a way to pass your variable, add it: 要将HTTP会话用作传递变量的方法,请添加它:

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

and retrieve it in the next servlet: 并在下一个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. 如果使用会话cookie将会话状态映射到来自同一浏览器会话的一系列HTTP请求,则该会话由servlet容器自动创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM