【问题标题】:How to deal with Sessions in Google App Engine?如何处理 Google App Engine 中的会话?
【发布时间】:2015-04-25 17:31:47
【问题描述】:

我在 servlet 中成功创建了会话,我可以将会话/会话属性获取到 jsp,但不能在端点类中。我想在端点类中获取会话信息。请帮我解决这个问题。

我在 Eclipse 中使用 maven,并在 appengine-web.xml 中启用了会话

我还阅读了关于此的article,除了如何启用会话我什么都不懂。

这个servlet是用来检查是否已经有一个会话

public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FirstServlet () {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request, response);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    HttpSession session=request.getSession(false);
    if (session != null) {
        String name = session.getAttribute("name");
        // do something
    } else {
        // do something
    }

}
}

如果会话不存在,则使用此 servlet 创建会话

public class SeccondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SeccondServlet() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request, response);
}

@Override

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");
    request.getRequestDispatcher("login.html").include(request, response);  
    String name = request.getParameter("name"); 
    HttpSession session=request.getSession();
    session.setAttribute("name", name);
    // do something 
}
}

这是我的端点 api 类(Google Cloud Endpoints)

@Api(
    name = "myapi",
    version = "v1",
    clientIds = "given client ids")
public class MyApi{
    @ApiMethod(name = "name", path = "name", httpMethod = "post")
    public List<String> getUser( HttpServletRequest servletReq) {
        HttpSession session = servletReq.getSession(false);
        List<String> name= new ArrayList<String>();
        if(session == null) {
            userName.add("no Name");
        } else {
            name.add((String)session.getAttribute("name"));
        }

       return name;
    }

即使我创建了会话并且我可以获得会话属性,这里仍然是“名称”

【问题讨论】:

  • 你能再清楚一点,端点是什么意思吗?你使用谷歌云端点或普通的旧 servlet 或任何 Mvc 框架,如 Spring 等
  • 是的,我正在使用谷歌云端点。我想在谷歌云端点类中获取会话属性。

标签: java google-app-engine session


【解决方案1】:

假设您了解 HttpSessions(如果不了解,它只是在服务器和客户端之间交换 cookie 以处理登录用户)。

所以所有与用户相关或任何其他与会话相关的信息都存储在服务器端,表示该信息的会话 ID 将作为 cookie 发送给客户端,客户端将在每次 HTTP 请求时将其发送回。

AppEngine 使用 Datastore 存储会话信息和 memcache 以便更快地访问该信息。

您可以使用在每个 HTTP 请求中注入的标准 HttpSession 对象来访问会话数据。

访问此 HttpSession 的代码因您使用的框架而异。如果你愿意,我可以指定代码sn-ps来帮助理解。

更新:

如果您使用的是 servlet,那么访问会话信息将如下所示:

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String userID = "Pankaj";
private final String password = "journaldev";

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) {
  HttpSession session=request.getSession();
  // access any value
  User user = (User)session.getAttribute("loggedInUser");
}

对于 Google Cloud 端点,请使用以下内容:

@ApiMethod
public Response getUser( HttpServletRequest servletReq) {
    HttpSession session = servletReq.getSession();
    session.getAttribute("loggedInUser");
}

【讨论】:

  • 谢谢@Ramesh,它非常有帮助,但我收到错误消息,要求我在这些 org.jsoup.helper.HttpConnection.Response 或 com.google.appengine 中导入我需要导入的内容。 repackaged.com.google.protos.gdata.proto2api.Core.Response;
  • Response 是我为 http 请求返回的自定义类,您可以使用任何对象,例如 Map 因为我们需要返回 json 作为响应
  • 即使在使用用户名创建会话后我无法在谷歌云端点中获得会话它是空的请再次考虑问题我更新了我的代码以便你可以建议我
  • @user4430114 Stack Overflow 不应该是您的调试服务。如果 Ramesh 回答了您最初的问题,请点赞并接受它,然后再问另一个问题 :)。
猜你喜欢
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 2011-06-14
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2015-01-14
相关资源
最近更新 更多