如何将会话值传递给 Java 中的另一个 Web 应用程序/项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5165883/
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
How to pass session value to another web application/project in java
提问by ace
How can I pass the session attribute value to another application which is in same web server. The reason why I should know this is because we have a project which is divide by a module. Each module will redirect to another that pass value using session. That session will be used to identify which user is accessing that module.
如何将会话属性值传递给同一 Web 服务器中的另一个应用程序。我之所以应该知道这一点,是因为我们有一个被模块划分的项目。每个模块将重定向到另一个使用会话传递值的模块。该会话将用于识别哪个用户正在访问该模块。
Suppose I have a LogIn Module that separate from my other module.
假设我有一个与其他模块分开的登录模块。
Here my sample code:
这是我的示例代码:
Sample Url http://localhost:8080/Login
示例网址http://localhost:8080/Login
authorization.jsp : This page will call after user input a userId and then submit
authorization.jsp : 这个页面会在用户输入userId后调用然后提交
page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
HttpSession sessionid = request.getSession();
String userId = request.getParameter("userId");
sessionid.setAttribute("userId", userId);
System.out.println("SESSION IS :" + sessionid.getAttribute("userId"));
response.sendRedirect("http://localhost:8080/OtherModule");
Sample Url http://localhost:8080/OtherModule
示例网址http://localhost:8080/OtherModule
In my Home servlet I will check if the session have a userId
在我的 Home servlet 中,我将检查会话是否有 userId
protected void doGet(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
if(session.getAttribute("userId") != null){
System.out.println("SESSION ID @ GET: " + session.getAttribute("userId"));
} else {
System.out.println("No Session for userId");
}
}
//I also tried this with post but still I can't get the session
I hope this information may give you idea what's wrong with my code. Please help me with this. Thanks
我希望这些信息可以让您了解我的代码有什么问题。请帮我解决一下这个。谢谢
采纳答案by Shay Rojansky
If your shared data is related to authentication/login, then SSO (single sign on) is the way to go as @Ridcully says - it's managed for you by the application server, and your apps shouldn't need to worry about it.
如果您的共享数据与身份验证/登录相关,那么 SSO(单点登录)是@Ridcully 所说的方法 - 它由应用程序服务器为您管理,您的应用程序无需担心。
If the problem is more general - how to share data between webapps - then a very clean approach is to use JNDI. Tomcat (and any other servlet/J2EE server) provide a lookup space that can be common to all webapps, this mechanism is most often used to define database (or other resource) configuration outside apps in a way that can be shared.
如果问题更普遍——如何在 webapps 之间共享数据——那么一个非常干净的方法是使用 JNDI。Tomcat(和任何其他 servlet/J2EE 服务器)提供了一个可供所有 web 应用程序通用的查找空间,这种机制最常用于以可以共享的方式定义应用程序外部的数据库(或其他资源)配置。
So, you could write a class to contain the data, have it in JNDI, and have each application look it up (using explicit JNDI calls or resource injection). If you need more info on this let me know.
因此,您可以编写一个类来包含数据,将其放在 JNDI 中,并让每个应用程序查找它(使用显式 JNDI 调用或资源注入)。如果您需要更多信息,请告诉我。
回答by Ridcully
You will have to configure your web-server accordingly. Tomcat for example provides a valve for that. See here: http://tomcat.apache.org/tomcat-6.0-doc/config/host.html#Single_Sign_On
您必须相应地配置您的网络服务器。例如,Tomcat 为此提供了一个阀门。请参阅此处:http: //tomcat.apache.org/tomcat-6.0-doc/config/host.html#Single_Sign_On
Note: The localhost-URL you posted, only works on your computer (hence the name "local").
注意:您发布的 localhost-URL 仅适用于您的计算机(因此名称为“local”)。
It would be much easier though to just add all of your modules into one Web-Application or to use one of the countless Java Web Application Frameworks.
将所有模块添加到一个 Web 应用程序或使用无数 Java Web 应用程序框架之一会容易得多。
回答by BalusC
Single sign on indeed solves the shared-logged-in-user issue.
单点登录确实解决了共享登录用户的问题。
It does however not allow for sharing the same session among all deployed webapplications. If that is after all your actualintent, then you need to set emptySessionPath
attribute of the <Connector>
element in /conf/server.xml
to true
.
然而,它不允许在所有部署的 web 应用程序之间共享相同的会话。如果这毕竟是您的实际意图,那么您需要emptySessionPath
将<Connector>
元素的属性设置/conf/server.xml
为true
.
<Connector ... emptySessionPath="true">
See also Tomcat 6.0 HTTP Connector documentation.