简体   繁体   English

从一个Web应用程序重定向到另一个Web应用程序时如何传递数据?

[英]How to pass data when redirecting from one web application to another?

I have two web application which are deployed in the same J2EE Application server. 我有两个Web应用程序,它们部署在同一个J2EE应用程序服务器中。 I need to redirect from one web application to another. 我需要从一个Web应用程序重定向到另一个Web应用程序。 Also I need to set some information in the Header from web application 1 so that it is available in web application 2. When I try to access the information which set in the header of Web App1 in Web App2, I'm getting "null". 此外,我需要在Web应用程序1的Header中设置一些信息,以便在Web应用程序2中可用。当我尝试访问Web App2中Web App1标题中设置的信息时,我得到“null” 。 I'm using response.sendRedirect("http://localhost/webapp2") in Web Application 1 to redirect. 我在Web应用程序1中使用response.sendRedirect("http://localhost/webapp2")来重定向。 Please help me to solve this. 请帮我解决这个问题。

Thanks, Apps. 谢谢,应用程序。

A redirect is processed by the client (the browser). 客户端(浏览器)处理重定向。 So only the client gets the headers you sent. 因此,只有客户端获取您发送的标头。 The headers will not be passed to the webapp redirected to. 标头不会传递给重定向到的webapp。

You can do one of the following things to pass information from one webapp to another: 您可以执行以下操作之一将信息从一个Web应用程序传递到另一个Web应用程序:

  • pass data as request parameter 将数据作为请求参数传递
  • send data as cookie without path restriction 将数据作为cookie发送,不受路径限制
  • use cross context dispatching 使用跨上下文调度

A cookie received by the client will be send back to the server. 客户端收到的cookie将被发送回服务器。 The client does not know it is a different webapp. 客户端不知道它是一个不同的webapp。 You just need to set the cookie path to / . 您只需将cookie路径设置为/

Cross context dispacthing is done by an internal forward in the container (use a RequestDispatcher from the ServletContext). 交叉上下文调度由容器中的内部转发完成(使用ServletContext中的RequestDispatcher )。 The client will never know, the request is handled by another webapp. 客户端永远不会知道,请求由另一个webapp处理。 You than can set a request attribute to pass data. 您可以设置请求属性来传递数据。 Cross context dispatching has to be enabled by the container for security reasons. 出于安全原因,容器必须启用跨上下文调度。

You could send the information as URL parameters. 您可以将信息作为URL参数发送。

Redirect to http://localhost/webapp2?param1=value1&param2=value2 .... 重定向到http://localhost/webapp2?param1=value1&param2=value2 ....

其中一个选项是将简单的传递信息作为您在sendRedirect调用中使用的URL中的GET属性。

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

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