JavaWeb-024-利用URL重写HttpSession

介绍

  • Servlet规范中引入了一种补充的会话管理机制,它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话。这种补充机制要求在响应消息的实体内容中必须包含下一次请求的超链接,并将会话标识号作为超链接的URL地址的一个特殊参数。
  • 将会话标识号以参数形式附加在超链接的URL地址后面的技术称为URL重写。如果在浏览器不支持Cookie或者关闭了Cookie功能的情况下,WEB服务器还要能够与浏览器实现有状态的会话,就必须对所有可能被客户端访问的请求路径(包括超链接、form表单的action属性设置和重定向的URL)进行URL重写。

  • HttpServletResponse接口中定义了两个用于完成URL重写方法:

    • encodeURL方法
    • encodeRedirectURL方法

测试代码

微信公众号:JavaWeb架构师

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>
    <fieldset>
        <marquee>
            会话ID :
            <%= session.getId() %>
            <br /> 是新会话? :
            <%= session.isNew() %>
            <br /> 最大内部闲置:
            <%= session.getMaxInactiveInterval() %>
            <br /> 创建时间 :
            <%= session.getCreationTime() %>
            <br /> 最后一次访问时间 :
            <%= session.getLastAccessedTime() %>
        </marquee>

        <%
            //2.获取重新登录的username
            Object username = session.getAttribute("username") == null ? "" : session.getAttribute("username"); 
        %>
        <!-- url重写HttpSession:jsessionid回去以URL一部分的形式附带在url上  -->
        <!-- hello.jsp;jsessionid=22A10B2F1E451F767E5D4020A374B7FE  -->
        <form action="<%=response.encodeUrl("hello.jsp") %>" method="post">
            <table>
                <tr>
                    <td><label for="userID">用户名:</label></td>
                    <!-- 已经登录过,就显示出来 -->
                    <td><input type="text" name="username" value="<%=  username%>"
                        id="userID" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="登录" /></td>
                </tr>
            </table>
        </form>
    </fieldset>
</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>

    <body>
        <fieldset>
        <marquee behavior="alternate">
            会话ID : <%= session.getId() %>
            <br />
            是新会话? : <%= session.isNew() %>
            <br />
            最大内部闲置: <%= session.getMaxInactiveInterval() %>
            <br />
            创建时间 : <%= session.getCreationTime() %>
            <br />
            最后一次访问时间 : <%= session.getLastAccessedTime() %>
        </marquee>
        <details>
            <summary>你好!</summary>
            <%= request.getParameter("username") %>
        </details>
        <%
            //1.设置会话的属性值,想在重新登录的页面能直接显示上一次的登录名
            session.setAttribute("username", request.getParameter("username"));
        %>
        <a href="<%=response.encodeUrl("index.jsp")%>"> 重新登录 </a>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="<%= response.encodeUrl("logout.jsp")%>">注销</a>
        </fieldset>
    </body>
</html>

logout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>

    <body>
        <fieldset>
        <marquee behavior="alternate">
            会话ID : <%= session.getId() %>
            <br />
            是新会话? : <%= session.isNew() %>
            <br />
            最大内部闲置: <%= session.getMaxInactiveInterval() %>
            <br />
            创建时间 : <%= session.getCreationTime() %>
            <br />
            最后一次访问时间 : <%= session.getLastAccessedTime() %>
        </marquee>

        <details>
            <summary>再见</summary>
            <%= session.getAttribute("username") %>
        </details>
        <%
            //销毁session
            session.invalidate();
        %>
        </fieldset>
    </body>
</html>

微信公众号:JavaWeb架构师


其它

  • 源码下载
关注下方公众号,回复:javaweb_course.code

完整教程PDF版本下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值