随笔-71  评论-4  文章-0  trackbacks-0
Servlet session(HttpSession)对象会在使用者第一次存取Web服务器时产生,服务器会产生一个独特的session id来表示这个客户端,浏览器在之后的每次请求中都包括这个session id(可能是使用Cookie或是URL rewriting,这个细节不用您来担心),服务器根据这个session id来对应该客户端的session对象,您可以使用getId()方法来取得session id,例如:
<%
    out.println("Session ID:" + session.getId());
%>


显示的session id型式如下:
Session ID:2F892EDF2669858811B8D121119AE90B


session id默认是使用Cookie来储存于客户端,并在每一次浏览器发送请求时夹带这个讯息给服务器,服务器根据session id来对应出HttpSession对象,假如Cookie没有开启,则浏览器将无法储存session id,也就无法将session id的讯息传送给服务器,也就无法进行进程追踪,即使数据对象确实储存于HttpSession中,我们也无法取出,下面这个程序在浏览器Cookie功能关闭的情况下,只会一直显示session not found, reset!讯息:
<%@page contentType="text/html;charset=Big5"%>
<html>
<head><title>session demo</title></head>
<body>
<H1>
    <%
        if(session.getAttribute("info") == null) {
            session.setAttribute("info", "session information");
            out.println("session not found, reset!");
        }
        else
            out.println("session found: " + session.getAttribute("info"));
    %>
</H1>
</body>
</html>


如果Cookie功能关闭,则session id无法储存,也就无法在下一次请求时一并送至服务器,为了让进程追踪得以进行,我们必须进行URL rewriting来传送session id,所幸的是有一个简单的方法可以帮您进行这个动作,使用response的encodeURL()可以自动将session id编进URL中,例如:
<%@page contentType="text/html;charset=Big5"%>
<html>
<head><title>session demo</title></head>
<body>
<H1>
    <%
        if(session.getAttribute("info") == null) {
            session.setAttribute("info", "session information");
            out.println("session not found, reset!");
        }
        else
            out.println("session found: " + session.getAttribute("info"));
        out.println("<br><a href='" + response.encodeURL("sessiondemo.jsp") + "'>" + "进程追踪" + "</a>");
    %>
</H1>
</body>
</html>


如果您的浏览器Cookie功能关闭,您必须使用response的encodeURL()自动将session id编进URL中,如果Cookie功能可以运作,则encodeURL()会原封不动的传回指定的URL,否则会在指定的URL后加上sessiond id,例如上面的JSP网页在Cookie功能关闭的情况下,会传回以下的内容:
<html>
<head><title>session demo</title></head>
<body>
<H1>
    session not found, reset!
<br><a href='sessiondemo.jsp;jsessionid=7A2A0BFA32D0022D8BB80A5E690A9D10'>进程追踪</a>

</H1>
</body>
</html>


简单的说,按下经过URL rewriting的连结,浏览器就可以将session id传送至服务器,然而您的网址列上就会出现session id的讯息:
http://localhost:8080/myjsp/sessiondemo.jsp;jsessionid=7A2A0BFA32D0022D8BB80A5E690A9D10


这是一个有危险性的讯息,任何人只要在session的存活期限获得这个讯息,就可以进行进程追踪,所以基本上还是建议使用者开启Cookie功能,以免session id曝露在网址列上。

我们上一个主题的登入网页如果在Cookie功能关闭的情况下也将无法运作,我们必须这样改写login.jsp:
<%@page contentType="text.html;charset=Big5"%>
<%
    String user = request.getParameter("user");
    String password = request.getParameter("password");
    String memberURL = "http://localhost:8080/myjsp/member.jsp";
    String loginFormURL = "http://localhost:8080/myjsp/form.html";

    if(user == null || password == null) {
        response.setHeader("Refresh", "0; " + loginFormURL);
    }
    else if(user.equals("justin") && password.equals("1234")) {
        session.setAttribute("user", user);
        memberURL = response.encodeURL(memberURL);
        response.setHeader("Refresh", "3; " + memberURL);
        out.println(user + "欢迎登入!3秒后进入会员页面!");
    }
    else {
        response.setHeader("Refresh", "3; " + loginFormURL);
        out.println("使用者或密码错误,请重新登入(3秒后回到登入窗体)");
    }
%>


或者是您可以直接使用response的sendRedirect()方法,由于sendRedirect()要求完整的地址讯息,也就是包括http://开始的地址讯息,您可以使用response的encodeRedirectURL()传回这个地址,同样的,如果Cookie有开启,则只是原封不动传回原来指定的URL,我们也可以改写login.jsp程序如下:
<%@page contentType="text.html;charset=Big5"%>
<%
    String user = request.getParameter("user");
    String password = request.getParameter("password");
    String memberURL = "http://localhost:8080/myjsp/member.jsp";
    String loginFormURL = "http://localhost:8080/myjsp/form.html";

    if(user == null || password == null) {
        response.setHeader("Refresh", "0; " + loginFormURL);
    }
    else if(user.equals("justin") && password.equals("1234")) {
        session.setAttribute("user", user);
        memberURL = response.encodeRedirectURL(memberURL);
        response.sendRedirect(memberURL);
    }
    else {
        response.setHeader("Refresh", "3; " + loginFormURL);
        out.println("使用者或密码错误,请重新登入(3秒后回到登入窗体)");
    }
%>


session具有其存活期限,关闭浏览器、服务器关闭可能让session失效,当客户端停止活动一段时间(Tomcat预设是30分钟),session会自动失效,您可以使用getMaxInactiveInterval()取得session的等待期限,取得的值以秒为单位,或是以setMaxInactiveInterval()设定等待期限,设定的值也是以秒为单位:
<%
    out.println("default session life: " + session.getMaxInactiveInterval());
    session.setMaxInactiveInterval(600);
    out.println("now session life: " + session.getMaxInactiveInterval());
%>


您可以在web.xml中设定预设的session等待期限,使用<session-config>与<session-timeout>来进行设定,注意设定的单位为分钟数,例如下面的设定将session等待期限预设为10分钟:
    <session-config>
        <session-timeout>
            10 <!--分钟-->
        </session-timeout>
    </session-config>

以上示例已通过tomcat的测试,详见192.168.1.10demo程序(http://192.168.1.10:8080/demo)

posted on 2006-01-12 12:18 zjw_albert 阅读(443) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: