我如何在java中获取会话ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3475543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get session id in java
提问by Tokendra Kumar Sahu
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.
我想在java中构建一个api来解决在任何网站中将一个页面移动到另一个页面时出现的安全图像问题。我如何获取会话 ID 和 cookie,以便我可以将其与安全图像字符串一起发布。
Thanks
谢谢
采纳答案by YoK
Following should give session id in jsp
以下应该在jsp中给出会话ID
If you have EL enabled in your container, you can do it without the JSTL tag - ie just
如果您在容器中启用了 EL,则可以在没有 JSTL 标记的情况下执行此操作 - 即只需
<c:out value="${pageContext.session.id}"/>
or An alternative for containers without EL:
或 没有 EL 的容器的替代方案:
<%= session.getId() %>
Example to get Cookies is as :
获取 Cookie 的示例如下:
<%
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