简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM