gpt4 book ai didi

java - 如何将 session 值传递给 java 中的另一个 Web 应用程序/项目

转载 作者:搜寻专家 更新时间:2023-10-31 19:58:22 24 4
gpt4 key购买 nike

如何将 session 属性值传递给同一 Web 服务器中的另一个应用程序。我应该知道这一点的原因是因为我们有一个按模块划分的项目。每个模块将重定向到另一个使用 session 传递值的模块。该 session 将用于识别哪个用户正在访问该模块。

假设我有一个与其他模块分开的登录模块。

这里是我的示例代码:

示例网址 http://localhost:8080/Login

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");

示例网址 http://localhost:8080/OtherModule

在我的 Home servlet 中,我将检查 session 是否有 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

我希望这些信息可以让您了解我的代码有什么问题。请帮我解决一下这个。谢谢

最佳答案

您必须相应地配置您的网络服务器。例如,Tomcat 为此提供了一个阀门。看这里:http://tomcat.apache.org/tomcat-6.0-doc/config/host.html#Single_Sign_On

注意:您发布的 localhost-URL 仅适用于您的计算机(因此名称为“本地”)。

不过,将所有模块添加到一个 Web 应用程序或使用无数 Java Web 应用程序框架之一会容易得多。

关于java - 如何将 session 值传递给 java 中的另一个 Web 应用程序/项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5165883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com