简体   繁体   English

Jersey中的会话变量(Java / JBoss)

[英]Session variables in Jersey (Java/JBoss)

I'm developing a RESTful Web Service in Java with Jersey and JBoss and I need to store "Session variables" like in PHP. 我正在用Jersey和JBoss用Java开发RESTful Web服务,并且需要像在PHP中那样存储“会话变量”。 To mantain the session of a client but without using coockies. 保持客户会话,但不使用coockies。

@Context
private UriInfo context;

@Context
private HttpServletRequest httpRequest;

@Path("/firstPage")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public String createInstance(@QueryParam("instance") String inst) 
        throws JSONException 
{
    // Here I "declare" the session variable
    HttpSession session = httpRequest.getSession(true);
    session.setAttribute("instance", inst);

    // Creation of the DTO (Data Transfer Object)
    JSONObject innerObj = new JSONObject();
    JSONObject outterObj = new JSONObject();

    innerObj.put("ContactID", "{{Contact.Id}}");
    innerObj.put("EmailAddress", "{{Contact.Field(C_EmailAddress)}}");
    outterObj.put("recordDefinition", innerObj);

    return outterObj.toString();
}

@Path("/secondPage")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public String validateInstance(@QueryParam("instance") String inst) 
        throws JSONException 
{
    // Here I "validate" the session variable
    HttpSession session = httpRequest.getSession(true);
    if ((String)session.getAttribute("instance") == inst) {
        JSONObject obj= new JSONObject();

        outterObj.put("status", 200);
        outterObj.put("instance", inst);

        return obj.toString();
    } else {
        JSONObject obj = new JSONObject();
        obj.put("error", 401);
        return obj.toString();
    }
}

Here is what I did but it's not working as I expected. 这是我所做的,但是没有按预期工作。 Any solution? 有什么办法吗?

As somone already pointed out: Sessions are handled with a cookie that contains the session id which php uses to connect clients to sessions. 正如somone所指出的:使用cookie来处理会话,该cookie包含php用于将客户端连接到会话的会话ID。

For your java rest service. 为您的Java Rest服务。 per definition sessions are not part of rest services and even discouraged. 每个定义的会话都不是休息服务的一部分,甚至不鼓励使用。

Please refer to the original document from field about the rest architecture: https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm 请参阅有关其余架构的字段原始文档: https : //www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

5.1.3 Stateless 5.1.3无状态

We next add a constraint to the client-server interaction: communication must be stateless in nature, ... ach request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. 接下来,我们为客户端-服务器交互添加了一个约束:通信本质上必须是无状态的,...从客户端到服务器的每个请求必须包含理解该请求所需的所有信息,并且不能利用服务器上任何存储的上下文。服务器。 Session state is therefore kept entirely on the client. 因此,会话状态完全保留在客户端上。

Is there any reason against taking the created data along between requests and attach them to the second request? 是否有任何理由反对在两次请求之间收集创建的数据并将其附加到第二个请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM