0

次のようなサーブレットがあります

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 getリクエストを使用してテストしています

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

ステートマシンで定義されているように状態を移動すると予想されますが、常に同じ行を返します

ユーザー: ジョン

現在の状態: null

新しい状態: NEW

券売機はいたってシンプル

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 に答える