javaee / jersey

This is no longer the active Jersey repository. Please see the README.md

Home Page:http://jersey.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to set cookie and Clear cookie in api?

mrerhuo opened this issue · comments

commented

@path("Account")
public class AccountResource {
@context
HttpServletRequest request;
@context
HttpServletResponse response;

@GET
public Response Login() {
	ResultInfo<UserModel> resultInfo=new ResultInfo<UserModel>();
	UserModel model=new UserModel();
	model.setAccount("admin");
	
	resultInfo.setModel(model);
	  Map<String,Object> m = new HashMap<String,Object>();
	  m.put("userid", model.getAccount());
      String token = JavaWebToken.createJavaWebToken(m);
      Cookie cookie = new Cookie("token",token);
      response.addCookie(cookie);
	return Response.ok(resultInfo).build();
}
@GET
@Path("{id}")
public Response LoginOut(String t) {
	Cookie[] cookies = request.getCookies();  
	ResultInfo<Integer> resultInfo=new ResultInfo<Integer>();
	resultInfo.setStatus(ResultKey.SuccessResult);
	resultInfo.setMsg(t);
	 if (null==cookies) {  
         System.out.println("没有cookie==============");  
     } else {  
         for(Cookie cookie : cookies){  
             if(cookie.getName().equals("token")){
                 System.out.println("原值为:"+cookie.getValue());  
                 cookie.setValue("");  
                 cookie.setMaxAge(0);// 设置为0
                 System.out.println("被修改的cookie名字为:"+cookie.getName()+",新值为:"+cookie.getValue());
                 response.addCookie(cookie);
             }  
         }  
     }  
	return Response.ok(resultInfo).build();
}

}