如何将会话属性值传递给同一Web服务器中的另一个应用程序.我之所以知道这是因为我们有一个由模块划分的项目.每个模块将重定向到使用会话传递值的另一个模块.该会话将用于识别哪个用户正在访问该模块.
假设我有一个与我的其他模块分开的LogIn模块.
这是我的示例代码:
示例网址http:// localhost:8080 /登录
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");
Run Code Online (Sandbox Code Playgroud)
示例Url http:// localhost:8080/OtherModule
在我的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
Run Code Online (Sandbox Code Playgroud)
我希望这些信息可以让您了解我的代码有什么问题.请帮我解决一下这个.谢谢
如果您的共享数据与身份验证/登录相关,那么 SSO(单点登录)就是 @Ridcully 所说的方法 - 它由应用程序服务器为您管理,您的应用程序不需要担心它。
如果问题更普遍 - 如何在 Web 应用程序之间共享数据 - 那么一个非常干净的方法是使用 JNDI。Tomcat(以及任何其他 servlet/J2EE 服务器)提供了所有 Web 应用程序共用的查找空间,此机制最常用于以可共享的方式在应用程序外部定义数据库(或其他资源)配置。
因此,您可以编写一个类来包含数据,将其放在 JNDI 中,然后让每个应用程序查找它(使用显式 JNDI 调用或资源注入)。如果您需要更多这方面的信息,请告诉我。
| 归档时间: |
|
| 查看次数: |
16588 次 |
| 最近记录: |