如何处理Google App Engine中的会话?

时间:2015-02-24 07:05:10

标签: java google-app-engine session

我在servlet中成功创建会话,我可以获得jsp的会话/会话属性,但不能在端点类中获取。我想在端点类中获取会话信息。请帮帮我。

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

我还读了article关于这一点,除了如何启用会话I我什么都不懂。

此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;
    }

即使我创建了会话,我仍然得到“无名字”,我可以获得会话属性,这里是“名字”

1 个答案:

答案 0 :(得分:9)

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

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

AppEngine使用数据存储来存储会话信息和内存缓存以便更快地访问

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

访问此HttpSession的代码在您使用的框架中有所不同,如果您希望我可以使用特定的代码片段来帮助更好地理解它

更新:

如果您正在使用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");
}

对于谷歌云终端使用它如下

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