카테고리 없음

Session getAttribute Integer 주의사항

몽실KUN 2020. 10. 14. 17:57

정말 생각도 못했던 버그이다.

이유는 모르겠으나, 로컬 개발 환경(Windows)에서는 에러가 발생하지 않지만

실서버 (Linux-Tomcat)에서는 에러가 합니다.

 

stackoverflow.com/questions/6031278/session-attribute-access-and-converting-to-int

 

Session attribute access and converting to int?

I have stored user id in Session using following command in Servlet: HttpSession session = request.getSession(); session.setAttribute("user", user.getId()); Now, I want to access that user id from

stackoverflow.com

거두절미하고 결론부터 말하자면

int값을 getAttribute 할 때는 (Integer) casting을 해줘야 한다. (int casting이 아니라)

 

int randValue = rand.nextInt();
String randkey = TestController.byteArrayToHex(bytes);
//=========== SET ===========
session.setAttribute(randkey, randValue);
//===========================
//=========== GET ===========
(Integer) session.getAttribute(key);
//===========================