web开发日记-JSP内置对象2

作者: 在南方的北方人_Elijah | 来源:发表于2017-02-18 10:10 被阅读13次

    Session

    Session相当于一个服务器端的保管箱,服务器发送Sessionid给客户端,当客户端返回数据时,将会通过sessionid来判断客户端的唯一性。

    Session常用方法

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>session</title>
    </head>
    <body>
    session的唯一表示符<%=
        session.getId()
    %><br />
    
    session的创建时间<%=
       new java.util.Date(session.getCreationTime()).toString()
    %><br />
    
    session的最后访问时间<%=
        new java.util.Date(session.getLastAccessedTime()).toString()//使用Data对象将时间变为Data类型字符串
    %><br />
    
    session的失效时间<%=
        session.getMaxInactiveInterval()
    %><br />
    
    </body>
    </html>
    

    在/WEB-INF/web.xml中 可以设置session失效时间,时间以分钟为单位,设置session-timeout,更改默认失效时间。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
            version="3.1">
       <session-config>
           <session-timeout>
           15
           </session-timeout>
       </session-config>
    </web-app>
    

    PS :Mac下有一个很好用的API查询软件Dash

    Session会话小实例 登陆退出的会话功能

    功能简介:四个页面 login.jsp do_login.jsp logout.jsp welcome.jsp.
    login收集表单数据 传给do_login 进行处理,而后验证通过后,转入welcome页面。
    logout行为触发后清空session对象,而后转入welcome欢迎新用户注册登录。

    login.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>login</title>
    </head>
    <body>
    <form action="/SessionDemo/do_login.jsp" method="post">
        userName:<input type="text" name="userName" />
        password:<input type="password" name="password"/>
        <input type="submit" value="submit" />
    
        </form>
    </body>
    </html>
    

    do_login.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if (userName != null && password != null) {
            session.setAttribute("userName", userName);//在session中添加一个键值对
            response.setHeader("refresh", "2;URL=/SessionDemo/welcome.jsp");//设置刷新 跳转到welcome页面
        }
    %>
    

    welcome.jsp session.isNew()方法判断是否时新创建的

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>welcome</title>
    </head>
    <body>
    <%
        if (session.getAttribute("userName") != null) {%>
    欢迎:<%=session.getAttribute("userName")%>
    <a href="/SessionDemo/logout.jsp">注销</a>
    <br />
    <%}else{%>
    请先登陆
    <a href="/SessionDemo/login.jsp">登录</a>
    <%}%>
    <%if(session.isNew()){   //判断是否是新创建的%>
    欢迎新用户!
    <%}else{%>
        欢迎老用户!
    <%}%>
    
    
    </body>
    </html>
    

    logout.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        session.invalidate();//登出时直接干掉整个session对象,清除的非常彻底
        response.setHeader("refresh", "2;URL=/SessionDemo/welcome.jsp");//响应到welcome
    %>
    

    JSP内置对象 Application

    Application代表的是目前的应用程序,存在于服务器的系统内存中,一旦应用启动就会创建一个Application对象。Application生命周期更长,为用户使用全局信息提供了方便。

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>application</title>
    </head>
    <body>
    服务器信息<%=
    application.getServerInfo()
    %><br/>
    应用名称<%=
    application.getServletContextName()
    %><br/>
    主机名称<%=
    application.getVirtualServerName()
    %><br/>
    
    
    </body>
    </html>
    

    Application对象是如何共享信息的

    页面访问计数器

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>counter</title>
    </head>
    <body>
    <%
        Object obj = application.getAttribute("counter");//获取键值对
        if (obj == null) {//看键值对是否为空,为空则第一次访问,不为空 则计数累计
            application.setAttribute("counter", new Integer(1));//保存键值对到application中
            out.println("页面被访问了1次");
        }else{
            int counterValue = Integer.parseInt(obj.toString());//创建计数标记值
            //若不为空,则将计数加一输出
            ++counterValue;
            out.println("该页面被访问了" + counterValue + "次<br />");
            application.setAttribute("counter",counterValue);//最后将变化后的值保存到application对象中
        }
    
        //java中会自动将int类型的值转化成一个Integer对象 进行一个装箱操作
    %>
    </body>
    </html>
    

    内置对象Config

    在配置文件和servlet中更常用,在jsp中较少直接用内置Config对象。

    相关文章

      网友评论

        本文标题:web开发日记-JSP内置对象2

        本文链接:https://www.haomeiwen.com/subject/mqxkwttx.html