How can i get session id in java

Tokendra Kumar Sahu picture Tokendra Kumar Sahu · Aug 13, 2010 · Viewed 48.8k times · Source

I want to build an api in java to solve the security image problem occurred while moving one page to another page in any website. How can i get the session id and cookies so that i can post it with the security image string.

Thanks

Answer

YoK picture YoK · Aug 13, 2010

Following should give session id in jsp

If you have EL enabled in your container, you can do it without the JSTL tag - ie just

<c:out value="${pageContext.session.id}"/>

or An alternative for containers without EL:

<%= session.getId() %>

Example to get Cookies is as :

<%
String cookieName = "username";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null){
  for (int i = 0; i < cookies.length; i++) {
    if (cookies [i].getName().equals (cookieName)){
      myCookie = cookies[i];
      break;
    }
  }
}
%>

Referenced from: http://www.roseindia.net/jsp/jspcookies.shtml