[jboss-user] [JBoss Seam] - How to set and get cookie in seam?

Stateless Bean do-not-reply at jboss.com
Sun Dec 16 10:14:03 EST 2007


Hi,
I have one stateless bean in my app, and my scenario is:
1. first time visit page set cookie with some value,
2. second and more visits check if cookie exist and read it's value.

I don't know where is my problem, each time I read only 2 cookies whitch aren't mine:
1. session id cookie
2. identity cookie


  | package pl.sguni.universum;
  | 
  | import java.math.BigInteger;
  | import java.util.Random;
  | 
  | import org.jboss.seam.ScopeType;
  | 
  | import javax.ejb.EJBException;
  | import javax.ejb.Stateless;
  | import javax.ejb.TransactionAttribute;
  | import javax.ejb.TransactionAttributeType;
  | import javax.faces.context.FacesContext;
  | import javax.persistence.Query;
  | import javax.servlet.http.Cookie;
  | import javax.servlet.http.HttpServletRequest;
  | import javax.servlet.http.HttpServletResponse;
  | 
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Scope;
  | import org.jboss.seam.annotations.web.RequestParameter;
  | 
  | import pl.sguni.constants.LetterType;
  | import pl.sguni.engine.BattleSystem;
  | import pl.sguni.model.Moves;
  | 
  | @Stateless
  | @Scope(ScopeType.SESSION)
  | @Name("advAction")
  | public class AdvAction extends BattleSystem implements AdvConsole {
  | 	public AdvAction() {}
  | 
  | 	@In 
  | 	protected FacesContext facesContext;
  | 	
  | 	@RequestParameter
  | 	private String id; 
  | 	
  | 	@In("#{remoteAddr}") private String ipString;
  | 	//*******************************************************************************
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public String adv() {
  | 		String ret = "/AdvDone.xhtml";
  | 	   	try {
  | 	   		long a = System.currentTimeMillis();
  | 	   		//=================================================
  | 			if ((id.length() > 0) && (ipString.length() > 0)) {
  | 				if (!checkCookie()) {
  | 					createCookie(ipString);
  | 					ret = "/Adv.xhtml";
  | 				}
  | 			}
  | 	   		//=================================================			 
  | 			long b = System.currentTimeMillis();
  | 			System.out.println("TIME: " + (b - a) + " ms.");
  | 	   	} catch(EJBException e) {
  | 			log.error("User: ->advAction - adv(): " + e.getMessage());			
  | 		}
  | 	   	return ret;
  | 	}
  | 
  | 
  | 	private void createCookie(String value) {
  |         Cookie cookie = new Cookie("adv", id);
  |         cookie.setMaxAge(0);
  |         cookie.setSecure(false);
  |         cookie.setVersion(32140800);
  |         HttpServletResponse res = (HttpServletResponse)facesContext.getExternalContext().getResponse();
  |         res.addCookie(cookie);
  | 	}
  | 	public boolean checkCookie() {		
  | 		String cookieName = null;
  | 		Cookie[] cookie = ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies();
  | 		
  | 		if(cookie != null && cookie.length > 0) {
  | 			System.out.println("Cookie : [" + cookie + "] of length : " + cookie.length);
  | 			for (int i = 0; i < cookie.length;i++) {
  | 				Cookie currentCookie = cookie;
  | 				cookieName = currentCookie.getName();
  | 				if (cookieName.contentEquals("adv")) {
  | 					System.out.println("DEBUG:cookie_value = " + currentCookie.getValue());
  | 					return true;
  | 				}
  | 			}
  | 		} else
  | 			System.out.println("Cannot find any cookie");
  | 		return false;
  | 	}
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4113213#4113213

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4113213



More information about the jboss-user mailing list