0

我有一个如下的servlet

public class Ticket extends HttpServlet {
private static final long serialVersionUID = 1L;

public Ticket() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // check cookies
    Cookie[] receivedCookies = request.getCookies();
    if(receivedCookies != null){
        Cookie user = receivedCookies[0];

        response.getWriter().println("user: " + user.getValue());
        response.addCookie(user);

        // check session
        HttpSession session = request.getSession(true);
        Object atribVal = session.getAttribute(user.getValue()); // get a current state

        if(atribVal == null){
            response.getWriter().println("current state: null");
        }
        else{
            response.getWriter().println("current state: " + atribVal.toString());
        }           

        String newState = TicketMachine.getNextState(atribVal); // get a new state based on the current one

        response.getWriter().println("new state: " + newState);

        if(newState == "COMPLETED"){ // ticket completed, destroy session
             session.invalidate();
             return;
        }
        else{ // move to the next state
            session.setAttribute(user.getValue(), newState);                
        }           
    }
}
}

我正在尝试为每个请求票的用户存储售票机的状态。我在 Oracle WebLogic Server 上运行它并使用如下所示的 cURL 获取请求对其进行测试

curl --cookie "user=John" 127.0.0.1:7001/myApp/Ticket

我希望它能够像在状态机中定义的那样在状态中移动,但它总是返回相同的行

用户:约翰

当前状态:空

新状态:新

售票机很简单

public class TicketMachine {    

    public static String getNextState(Object currentState){

        if(currentState == null)
            return "NEW";

        switch(currentState.toString()){        
        case "NEW":
            return "PAYMENT";
        case "PAYMENT":
            return "COMPLETED";
        }

        return null;
    }
}

我在这里做错了什么?

4

1 回答 1

2

创建会话时,它会将会话参数(例如会话 ID)添加到响应 cookie 中。您对 cURL 的命令不会存储来自服务器的 cookie。您必须按如下方式存储 cookie curl --cookie oldcookies.txt --cookie-jar newcookies.txt http://www.example.com

另请阅读http://curl.haxx.se/docs/httpscripting.html上有关 cookie 的部分

于 2013-10-25T17:50:32.377 回答