JSP详细篇——session

来源:互联网 发布:sql中not in 没有效果 编辑:程序博客网 时间:2024/06/11 13:20

session对象

通过session可以在应用程序的Web页面之间进行跳转时,保存用户的状态,使整个用户会话一直存在下去,直到浏览器关闭。但是在一个会话中,客户端长时间不向服务器发出请求,session会自动消失。这个时间取决于服务器,可以通过程序进行修改。

1.创建及获取客户的会话

通过sessionsetAttribute()getAttribute()方法实现。

setAttribute()方法的语法格式:

session.setAttribute(String name,Object obj)

参数说明:

name:变量名

obj:保存在session范围的对象

范例:

将用户名“张三”保存到session范围的username变量中

session.setAttribute(“usernme1”,”张三”);

 

 

getAtrribute()的语法格式

getAttrbite(String name);

参数说明:

name:保存在session变量范围的参数名

getAttribute()方法的返回值是Object类型。

2.从会话中移除指定的绑定对象

removeAttribute()的语法格式:

removeAttribute(String name)

参数说明:

name:session中的变量名

范例:

将保存在session中的username移除

<%

session.removeAttribute(“username”);

%>

 

3.销毁session对象

session对象的invalidate()方法可以销毁session,其语法格式为:

Session.invalidate();

session对象被销毁后,将不可在使用session对象,否则将报出Session already invalidate异常

4.会话超时的管理

session对象中提供了设置会话生命周期的方法:

getLastAccessTime ():返回客户端最后一次与会话有关的请求时间

 

 

getMaxInactiveInterval():以秒为单位,返回一个会话内两个请求的最大时间间隔

 

setMaxInactiveInterval():以秒为单位,设置session的有效时间

5.session对象的应用

范例:

index.jsp页面中,提供用户输入用户名的文本框;在session.jsp页面中,将用户 输入的用户名保存在session中,用户在该页面可以添加最喜欢的地方,在result .jsp页面中,显示用户输入的用户名和最喜欢的地方

index.jsp页面如下

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    <form id="form1" name="form1" method="post" action="session/session.jsp">

     <div align="center">

     <table width="30%" border="1">

     <tr>

     <td width="40%"><div align="center">您的名字是:</div></td>

     <td width="60%">

     <lable>

     <div align="center">

     <input type="text" name="name">

     </div>

     </lable>

     </td>   

     </tr>

     <tr>

     <td colspan=“2”>

     <lable>

     <div align="center">

     <input type="submit" name="submit" value="提交" />

     </div>

     </lable>

     </td>

     </tr>

     </table>

     </div>

    </form>

  </body>

</html>

 

 

 

Result.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%

    String place = request.getParameter("place");

    session.setAttribute("place",place);//获取保存在session中的对象

    String name = request.getParameter("name");

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>result page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    

    <form id="form1" name="form1" method="post" action="">

     <table width="30%" border="1">

     <tr>

     <td colspan="2">

     <div align="center">

     <stron>显示答案:

     </stron>

     </div>

     </td>

     </tr>

     <tr>

     <td width="45%">

     <div align="left">

     您的名字是:

     </div>

     </td>

     <td width="55%">

     <label>

     <div align="left">

     <%=name %>

     </div>

     </label>

     </td>

     </tr>

     <tr>

     <td>

     <label>

     <div align="left">

     您喜欢的地方是:

     </div>

     </label>

     </td>

     <td>

     <div align="left">

     <%=place %>

     </div>

     </td>

     </tr>

     </table>

    </form>

  </body>

</html>

 

 

 

Session.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%

    String name = request.getParameter("name");//获取填写的用户名

    session.setAttribute("name", name);//将用户名保存在session中

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>session jsp</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

 

    <div align="center">

     <form id="form1" name="form1" method="post"  action="session/result.jsp">

     <table width="30%" border="1">

     <tr>

     <td>您的名字是:</td>

     <td><%=name %></td>

     </tr>

     <tr>

     <td>您最喜欢的地方是:</td>

     <td>

     <label>

     <input type="text" name="place">

     </label>

     </tr>

     <tr>

     <td colspan="2">

     <label>

     <div align="center">

     <input type="submit" name="submit" value="提交" />

     </div>

     </label>

     </table>

     </form>

    </div>

  </body>

</html>




0 0
原创粉丝点击