java-如何在第二个Servlet中初始化会话(以获取第一个Servlet在会话变量中存储的数据) [英] java- how to initialise session in second servlet (to get data stored by first servlet in session variable)

查看:108
本文介绍了java-如何在第二个Servlet中初始化会话(以获取第一个Servlet在会话变量中存储的数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在会话数据中存储了一个变量,该变量来自一个Servlet,用户尝试在其中登录我的应用程序.现在,我想从会话中的同一应用程序的另一个servlet中检索用户数据.

I am storing a variable in session data, from a servlet where the user tries logging in to my application. Now I want to retrieve the user data from session, in another servlet in the same application.

如何在第二个servlet中初始化会话变量?我将请求"作为"HttpServletRequest",将会话变量编码为"HttpSession session = null;".或作为 "HttpSession会话= request.getSession(true);"?还是其他方式?

How do I initialise the session variable in the second servlet? Taking "request" as the "HttpServletRequest" do I code the session variable as "HttpSession session = null;" or as "HttpSession session = request.getSession(true);"? Or is it some other way?

请注意,在应用程序流程中,用户从第一个servlet转到外部页面,然后从外部页面将用户重定向到第二个servlet. (外部页面基本上是通过Google/Yahoo/Hotmail等中的oauth登录用户的.)

Note that in the application flow, the user goes to an external page from the first servlet, and from the external page he is redirected to the second servlet. (The external page basically logs in the user via oauth in Google/Yahoo/Hotmail etc).

这是否意味着在这种情况下我不能使用会话变量?我必须使用应用程序范围的变量吗?

Does this mean that I cant use session variables in this case? Do I Have to use application scoped variables?

如果我的问题听起来很愚蠢,请原谅,今天只是我开始使用Servlets进行编码的第三天...

Please excuse me if my question sounds dumb, today is only the 3rd day of my starting coding in Servlets...

推荐答案

Arvind,

您使用的JSP:

   <% Object something = request.getSession().getAttribute( "foo" ) %>

当然,哪一个-毫不奇怪-与Thilo的答案非常相似...:-)

which of course - unsurprisingly - is extremely similar to Thilo's answer... :-)

JSP代码是在服务器端评估的,因此您所要做的就是确保JSP与Servlet链接在一起,例如. Servlet的作用类似于

JSP code is evaluated on the server side, so all you have to is make sure that the JSP is chained from the servlet, eg. servlet does something like a

   response.sendRedirect( "your.jsp" )

/安德斯/

这篇关于java-如何在第二个Servlet中初始化会话(以获取第一个Servlet在会话变量中存储的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆