【问题标题】:How to access sessions across different servlets in Google Appengine/ Java如何在 Google Appengine/Java 中访问不同 servlet 的会话
【发布时间】:2016-03-10 22:05:01
【问题描述】:

我读过一些地方,在一个上下文下设置的会话属性不能被另一个上下文访问。我假设这里的上下文是一个 servlet。

我在这里有一个场景,我有一个 Appengine servlet 类,我在其中设置了一些会话属性,如下所示。

public class LoginServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request,
        HttpServletResponse response) throws IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        HttpSession session = request.getSession(true);
        session.setAttribute("username", username);
        session.setAttribute("password", password);

        System.out.println(session.getId());

        // response.getWriter().println("Done");
        response.sendRedirect("/dashboard.jsp");

    }
}

如您所见,最后我将重定向到不同的页面。我试图获取会话属性的地方。但它总是返回 null。 dashboard.jsp 如下。

<%@ page import="com.bullbull.Employee" %>
<%@ page import="com.googlecode.objectify.Key" %>
<%@ page import="com.googlecode.objectify.ObjectifyService" %>

<%


HttpSession sesn = request.getSession(true);

System.out.println(sesn.getAttribute("username"));

%>


<html>
  <head>
    <title>Dashboard</title>

登录后总是打印null。

我知道,我在某处理解某事会出错。请帮忙!

谢谢!!

【问题讨论】:

  • 你做得对。一定是配置问题。
  • 嗨@Manjunath - 这解决了吗?否则,让我们看看这个......

标签: java jsp google-app-engine session servlets


【解决方案1】:

我想你可能错过了在 appengine-web.xml 中配置会话

&lt;sessions-enabled&gt;true&lt;/sessions-enabled&gt;

https://cloud.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_Enabling_sessions

来自文档

App Engine includes an implementation of sessions, using the servlet session interface. The implementation stores session data in the App Engine datastore for persistence, and also uses memcache for speed. As with most other servlet containers, the session attributes that are set with session.setAttribute() during the request are persisted at the end of the request.

This feature is off by default. To turn it on, add the following to appengine-web.xml:

&lt;sessions-enabled&gt;true&lt;/sessions-enabled&gt;

【讨论】:

  • 不,我已经添加了!!
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 2011-06-07
    • 1970-01-01
    • 2014-12-02
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多