Reputation: 12626
I have a servlet like the following
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);
}
}
}
}
I am trying to store a state of a ticket machine for each user who requests a ticket. I'm running this on Oracle WebLogic Server and testing it using cURL get requests that looks like the following
curl --cookie "user=John" 127.0.0.1:7001/myApp/Ticket
I would expect it to move through states as they are defined in the state machine, but it always returns the same lines
user: John
current state: null
new state: NEW
The ticket machine is quite simple
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;
}
}
What am I doing wrong here?
Upvotes: 0
Views: 606
Reputation: 96
When session is created it adds to response cookies session parameters such as session id. Your command to cURL does not stores cookies from server. You have to store cookies as follows curl --cookie oldcookies.txt --cookie-jar newcookies.txt http://www.example.com
.
Also read section about cookies at http://curl.haxx.se/docs/httpscripting.html
Upvotes: 2