相关疑难解决方法(0)

servlet如何工作?实例化,会话,共享变量和多线程

假设,我有一个拥有大量servlet的Web服务器.对于在这些servlet之间传递的信息,我正在设置会话和实例变量.

现在,如果有2个或更多用户向此服务器发送请求,那么会话变量会发生什么?它们对所有用户都是通用的,或者对于每个用户而言都是不同的.如果它们不同,那么服务器如何区分不同的用户?

还有一个类似的问题,如果有n用户访问特定的servlet,那么这个servlet只在第一个用户第一次访问它时实例化,或者是否为所有用户单独实例化?换句话说,实例变量会发生什么?

java multithreading servlets session-variables instance-variables

1105
推荐指数
6
解决办法
28万
查看次数

web.xml中设置的超时在java中不起作用

我试图在Tomcat 7应用服务器上设置我的应用程序超时.首先我在web.xml中测试我的超时为一分钟

 <session-config>
       <session-timeout>1</session-timeout>
  </session-config> 
Run Code Online (Sandbox Code Playgroud)

and I am using HttpSessionListener to make sure my Timeout is working fine.I declared my sessionListener Class in web.xml .

public class HttpSessionChecker implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date());
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date());
    }
}
Run Code Online (Sandbox Code Playgroud)

and In web.xml

<listener>
    <listener-class>com.test.util.HttpSessionChecker</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

But when start my server and launch my application I see session …

java jsf listener session-timeout

6
推荐指数
1
解决办法
1万
查看次数

浏览器关闭时删除cookie

我需要在浏览器关闭时删除cookie,并且已经将window.onbeforeunload和其他内容放在一起,如下所示:

 window.onbeforeunload = function(){
        location.replace("admin.jsp?action=logout");
        deleteCookie('Times');
 }
Run Code Online (Sandbox Code Playgroud)

使用setCookie如下:

 function setCookie(name,value,days) {
         var expires;
         if (days) {
                  var date = new Date();
                  date.setTime(date.getTime()+(days*24*60*60*1000));
                  expires = "; expires="+date.toGMTString();
         }
         else {
                  expires = "";
              }
                  document.cookie = name+"="+value+expires+"; path=/";
     }
Run Code Online (Sandbox Code Playgroud)

当涉及到deleteCookie时,它包含如下:

    setCookie(name,value,-1);
Run Code Online (Sandbox Code Playgroud)

问题是每当我重新启动浏览器时,它总是来到window.onbeforeunload以便deleteCookie被触发.我实际上需要这个,以便在用户登录时重置我的倒数计时器,因为如果计数器没有结束,用户有时会在结束前关闭窗口/标签,因此cookie永远不会被删除.所以,我的想法是在用户登录时重置计数器,或者在用户注销时删除cookie.但是,我仍然无法弄清楚如何对此进行编码.谁能帮我吗?不过,我们将不胜感激.CMIIW

javascript

4
推荐指数
1
解决办法
3万
查看次数

为什么我的servlet会话不持久?

除了关闭浏览器(不删除cookie)之外,我的servlet仍按预期工作,会话丢失了。在使会话无效或删除Cookie之前,如何无限期保存会话?

@WebServlet(name="ServletOne", urlPatterns={"/", "/ServletOne"})
public class ServletOne extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        String newValue = request.getParameter("newValue");

        if (session.isNew()) {
            session = request.getSession(true);
            session.setAttribute("myAttribute", "value");
        }

        if (newValue != null)
            session.setAttribute("myAttribute", newValue);

        RequestDispatcher rd = request.getRequestDispatcher("test.jsp");
        rd.forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        doGet(request, response);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib …
Run Code Online (Sandbox Code Playgroud)

java session jsp servlets

2
推荐指数
1
解决办法
4685
查看次数