如何处理Google App Engine中的会话? [英] How to deal with Sessions in Google App Engine?

查看:48
本文介绍了如何处理Google App Engine中的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地在servlet中创建了会话,我可以将session/session属性获取到jsp,但是不能在端点类中获取.我想在端点类中获取会话信息.请帮助我.

I am successfully creating sessions in servlet and I can get sessions/ session attribute to jsp but not in endpoints class. I want to get the sessions info in endpoints classes. Please help me with this.

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

I am using maven in eclipse and I enabled sessions in appengine-web.xml

我阅读了文章,除了如何阅读启用会话,我什么都不懂.

I read an article about this also except how to enable session I din't understand anything.

此servlet用于检查是否已有会话

This servlet is to check whether there is an already a session

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创建会话

If session is not there create session using this 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)

This is my endpoints api class (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;
    }

即使创建了会话,我仍然得到"no Name"的结果,并且我可以获取session属性,这里是"name"

I am still getting "no Name" as result even though I created session and i can get the session attribute, here "name"

推荐答案

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

Assuming you know about HttpSessions (if not it's simply cookie exchanged between sever and client in order to deal with the logged in user )

因此,所有与用户相关的信息或任何其他与会话相关的信息都存储在服务器端,并且代表该信息的会话ID将以cookie的形式发送给客户端,客户端将在每个http请求上将其发送回去.

So all user related or any other session related informations are stored in the server end and an session Id representing the informations will be sent as cookie to client and client will sent it back on every http request.

AppEngine使用数据存储区存储会话信息和内存缓存,以加快访问速度

AppEngine uses Datastore to store the session informations and memcache for faster access

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

you can access the session data using standard HttpSession object injected in every http request

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

The code to access this HttpSession varies in frameworks you use, if you want I can specific code snippets to help to understand it better

更新:

如果您使用的是servlet,则访问会话如下所示

if you are using servlets then accessing session will looks like below

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终结点,请按以下方式使用

for google cloud endpoints use it like below

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

这篇关于如何处理Google App Engine中的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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