Search code examples
javajspjsessionid

How can i get session id in java


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


Solution

  • 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