JSP之内置对象Session与application

一,session

1.概述:Session对象是和请求相关的HttpSession对象,封装了属于客户回话的所有信息,Session对象的ID是指当一个客户首次访问服务器上的一个JSP页面时,JSP引擎产生一个session对象,同时分配一个字符类型的ID,JSP引擎同时将这个ID号发送到客户端,存放在cookie

2.简单的说,Session服务器给客户端一个ID,一个浏览器一个ID,这样session对象和客户之间就建立了一一对应的关系

3.例如:当用户把物品放入购物车时,就可以将用户选定的商品信息存放在session中,当需要进行付款等操作时,又可以将session中的信息取出来

序号 方法名 作用
01 getAttribute(String name) 获得指定名字的属性
02 getAttributeNames() 回session对象中存储的每一个属性对象
03 getCreationTime() 返回session对象的创建时间
04 getId() 返回当前session对象的编号
05 getLastAccessedTime 返回当前session对象的最后一次被操作的时间
06 getMaxInactiveInterval() 返回session对象的生存时间
07 removeAttribute(String name) 删除指定属性的属性值和属性名
08 setAttribute(String name,Object obj) 设置指定名字的属性
09 Invalidate() 注销当前的session
10 isNew() 判断是否是一个新的session

4.推荐几个不错的网站
内置对象session的练习

```login.html记录用户表单
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form method="POST" action="login1.jsp">
<p>用户名:<input type="text" name="user" size="18"></p>
<p>密码:<input type="text" name="pass" size="18"></p>
<p><input type="submit" value="提交" name="ok">
<input type="reset" value="重置" name="cancel"></p>
</from>
</body>
</html>
login1.jsp记录表单

<%@ page contentType="text/html;charest=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>session 应用演示</title></head>
<%
if(request.getParameter("user")!=null && request.getParameter("pass")!=null)
{
String strName=request.getParameter("user");
String strPass=request.getParameter("pass");
if(strName.equals("QIE") && strPass.equals("QIE"))
{
session.setAttribute("login","OK");
session.setAttribute("me",strName);
response.sendRedirect("welcome.jsp");
}
else{
out.println("<h2>登录错误,请输入正确的用户名和密码</h2>");
}
}
%>
</html>

session读取

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>欢迎光临</title></head>
<body>
<%
String strLogin=(String)session.getAttribute("login");
String strUser=(String)session.getAttribute("me");
if(strLogin==null)
{
out.println("<h2>请先登录,谢谢!<h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login1.html");
}
else
{
if(strLogin.equals("OK"))
{
out.println(strUser+"<h2>欢迎进入我们的网站!</h2>");
}
else
{
out.println("<h2>用户名或密码错误,请重新登录!<h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login1.html");
}
}
%>
</body>
</html>

二,application

1.概述:session是存储用户的信息,那么application就是存储服务器的信息。当服务器关闭时,application生命周期结束。

2.实例application计数器

创建applicationdemo1.jsp
<%@ page contentType="text/html;charest=UTF-8" %>
<html>
<head><title>application应用演示</title></head>
<body>
<% 
application.setAttribute("user","QIE");
application.setAttribute("pass","QIE518");
%>
<jsp:forward page="applicationdemo2.jsp"/>
</body>
</html>
创建applicationdemo2.jsp
<%@ page contentType="text/html;charest=UTF-8;pageEncoding=UTF-8" %>
<html>
<head><title>application应用演示</title></head>
<body>
<% 
String strName=(String) application.getAttribute("user");
String strPass=(String) application.getAttribute("pass");
out.println("user="+strName);
out.println("pass="+strPass);
%>
</body>
</html>
创建applicationdemo3.jsp
<%@ page contentType="text/html;charset=UTF-8" import="java.util.Date"%>
<html>
<head><title>计数器应用</title></head>
<body>
<center>
<font size=5 color=blue >application计数器</font>
</center>
<hr>
<%
String strNum=(String) application.getAttribute("num");
int num=0;
if(strNum !=null)
num=Integer.parseInt(strNum)+1;

application.setAttribute("num", String.valueOf(num));
%>
访问次数为:
<font color=red><%=num%></font></br>
</body>
</html>

发布了15 篇原创文章 · 获赞 0 · 访问量 141

猜你喜欢

转载自blog.csdn.net/qq_44230959/article/details/105428641
今日推荐