How to use the "session" in the HTML page

How to use the "session" in the HTML page

In the program development among the session's usefulness it can be very convenient, but when there is static demand, we have to put into jsp Html, only to find that session can not be used. So i have my static pages how to solve the session it?

Why not use HTML in session

session is one of jsp (Java Server Pages) built nine objects that request objects, response objects, session objects, application objects, out objects, pageContext objects, config objects, page objects, exception objects. Since it is a jsp built-in objects, then it stands to reason that we are less than in HTML, so got a thing today instead of session

sessionStorage

sessionStorage is a property in JavaScript, its use is able to do the same for easy access session as
a single storage
sessionStorage.setItem ( 'key', 'value ');
single acquisition
var value = sessionStorage.getItem ( 'key' ) ;
and we can put it into a package js file

Examples

Create a project architecture

First, we create a project, and create a js file in the folder jsTest items

Edit js file

// globalData 可以随便定义,调用的时候相同即可
var globalData ={
 
 /**
  * setUserInfo 方法名
  *  uid 用户id
  *  uname 用户姓名
  *  usex 用户性别
  */
 //多个存储
 setUserInfo:function (uid,uname,usex){
  //单个存储
  sessionStorage.setItem("uid",uid);
  sessionStorage.setItem("uname",uname);
  sessionStorage.setItem("usex",usex);
 },
 //单个获取
 getUserUid:function(){
  return sessionStorage.getItem("uid");
 },
 getUserUName:function(){
  return sessionStorage.getItem("uname");
 },
 getUserUSex:function(){
  return sessionStorage.getItem("usex");
 }
}

Such js completed

Html page editor

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <!--导入js文件-->
  <script src="js/global.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript">
   //调用多个存储方法
   globalData.setUserInfo(1,"张三","123456")
   //输出测试
   alert(globalData.getUserUid()+globalData.getUserUName()+globalData.getUserUSex());
  </script>
 </head>
 <body>
 </body>
</html>

Run the test

Success

Epilogue

sessionStorage is a very convenient property
in addition to delete
sessionStorage.removeItem ( 'key');
and emptying sessionStorage
sessionStorage.clear ();

Published an original article · won praise 0 · Views 8

Guess you like

Origin blog.csdn.net/andaying/article/details/104091812