我需要编写一个servlet,在调用时,获取有关当前打开的会话列表的信息.
有没有办法做到这一点?
在我的grails应用程序中,我已经实现了接口HttpSessionListener来监听会话创建,如下所示:
class MyHttpSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
log.info "***************** Session created: id= ${event.getSession()?.id}"
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想记录负责会话创建的IP地址.
我怎样才能做到这一点?
我需要编写一个小型Java类,使我能够添加和读取当前用户会话.
我看到的一切都是指Servlets,但我最好只想使用一个普通的老类.
任何人都可以帮助这个Java新手吗?
谢谢
在Java Servlet API中,获取的唯一方法ServletContext是通过HttpSession(Javadoc)的实例.
如果我不想创建会话并且只需要servlet上下文怎么办?换句话说,为什么课堂上没有getServletContext()方法HttpServletRequest?
编辑
我知道我可以ServletContext从servlet本身获取,因为它在初始化期间接收它.但是,HttpServletRequest即使它与servlet相关联,我也无法从单独中获取它.那么如果我有一个请求,但没有引用任何servlet怎么办?
HttpSession 是建立在cookie和url重写之上的高级界面,这意味着只有一个会话ID存储在客户端,与之关联的数据存储在服务器端。
哪里是HttpSession数据实际存储在服务器端?在JVM内存中还是其他地方?我可以更改存储位置,例如将其保存到内存数据库中吗?
如果它不在数据库中,那么当许多客户端同时处理同一会话数据时,是否存在并发问题?
由于我对Spring Session范围的实现没有深入的了解。谁能告诉我使用Spring Session范围的bean是否明智,因为HttpSession对象非常重要。就像Web应用程序一样,成千上万的用户可以同时访问该站点。
Spring Session作用域bean是否保存在HttpSession对象中?
或者,即使HttpSession对象仅引用spring会话作用域的bean,我们是否也不会增加会话对象的负担?
直接在HttpSession对象中存储任何bean的形式有何不同(使HttpSession对象的观点繁重)?
我想知道什么时候正是一个HttpSession将过期(而不是摧毁了一样)?
我试图弄清楚每当请求带有相同的会话ID时,session.getLastAccessedTime()+(session.getMaxInactiveInterval()*1000)是否会给我确切的时间(以毫秒为单位)会话到期!
来自javadocs:
long getLastAccessedTime()
Run Code Online (Sandbox Code Playgroud)Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.int getMaxInactiveInterval()
Run Code Online (Sandbox Code Playgroud)Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
假设我们有以下内容:
Treq1 - the time the container received the 1st request (HttpSession.lastAccessedTime)
Tresp1 - the time …
我最近开始维护在线系统。它在前端使用带有PrimeFaces的JSF,并在Java后端使用。JSF对我来说是一种新技术。
在登录过程中,整个用户表(包括明文密码(将很快得到修复))被加载到HttpSession属性中,并在系统的其他部分中引用。该系统的用户数不到50,因此数据大小对我来说不是什么大问题。
但是,所有用户数据都驻留在会话中这一事实令人担忧。
我是否不必担心,还是有办法通过客户端漏洞利用来访问此信息?我还有其他理由可以作为紧急改写该机制的理由吗?
下面的缩写代码:
login.xhtml
<p:inputText id="username" value="#{userBean.userName}" name="username"></p:inputText>
<p:password id="password" value="#{userBean.password}"></p:password>
<p:commandButton id="loginSubmit" value="Login" action="#{userBean.auth}"></p:commandButton>
Run Code Online (Sandbox Code Playgroud)
UserBean.java
@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean {
public String auth() {
// ...
FacesContext fctx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fctx.getExternalContext().getSession(true);
HashMap<Long, UserDetail> usersMap = dbBean.getAllUserDetails();
session.setAttribute("usersMap", usersMap);
// ...
}
}
Run Code Online (Sandbox Code Playgroud) 我在tomcat中使用java servlet.我在哈希表中保存用户名和带有属性usrname的httpsession我想知道是否有办法知道httpsession是否有效.
我试过了:
try{
String user = httpSession.getAttribute("username")
return "is valid";
}catch(IllegalStateException e){
return "is not valid";
}
Run Code Online (Sandbox Code Playgroud)
得到答案的答案,但如果我不认为"已登录"的用户从一个地方连接起来怎么办?如果我只控制我创建一个新的会话,我不知道他是否连接另一个会话.
我的JSP项目中有三个简单的HttpServlet类,"LoginServlet","LogoutServlet"和"ProfileServlet".
最后两个servlet如下所示我认为是有问题的.
@SuppressWarnings("serial")
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(false);
session.invalidate();
request.getRequestDispatcher("link.jsp").include(request, response);
out.print("You are successfully logged out!");
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
和
@SuppressWarnings("serial")
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("link.jsp").include(request, response);
HttpSession session = request.getSession(false);
if (session != null) {
String name = (String) session.getAttribute("name");
out.print("Hello, " + …Run Code Online (Sandbox Code Playgroud)