Java 如何将字符串值从一个 servlet 传递到另一个 servlet?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19946277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:45:22  来源:igfitidea点击:

How to pass a String value from one servlet to another servlet?

javajspservlets

提问by mujib009

I have one jsp from which I am getting a string value in servlet1 using 'request.getParameter' I want to link servlet1 and servlet2 and send the string value which i have got in servlet1 to servlet2.

我有一个jsp,从中我使用'request.getParameter' 在servlet1 中获取字符串值我想链接servlet1 和servlet2 并将我在servlet1 中获得的字符串值发送到servlet2。

please help.

请帮忙。

Many thanks in advance.

提前谢谢了。

回答by Prasad Kharkar

You need to set request attribute

您需要设置请求属性

In your servlet1.

在你的 servlet1.

request.setAttribute("attributeName",yourStringVAlue);
RequestDispatcher rd = request.getRequestDispatcher("yourServletPattern");
rd.forward(request,response);

In your Servlet2

在你的 Servlet2 中

String someName = (String)request.getAttribute("attributeName");

回答by Hussain Akhtar Wahid 'Ghouri'

In Servlet 1 :

在 Servlet 1 中:

request.setAttribute("myAwesomeAttributeName",myAwesomeAttributeValue);

then recieve it in servlet 2 by

然后在 servlet 2 中通过

request.getAttribute("myAwesomeAttributeName");

回答by Imranmadbar

You can used Session:

您可以使用会话:

servlet 1:

小服务程序 1:

      HttpSession session = request.getSession();
      session.setAttribute("yourDataKey",yourDataValue );

servlet 2:

小服务程序 2:

 HttpSession session = request.getSession();
 DataType data = (DataType) session.getAttribute("yourDataKey");

You can get help from my git project: https://github.com/imrangthub/BlogUsingCoreJava

你可以从我的 git 项目中获得帮助:https: //github.com/imrangthub/BlogUsingCoreJava