JSP中四种属性保存范围(2)
1.session
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
//获取session ID Manager 可以将session保存在本地上
String id = session.getId();
%>
<h1><%=id %></h1>
</body>
</html>
<%@ page language="java" contentType="text/html"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
if(session.isNew()){
%>
<h1>欢迎新用户</h1>
<%
}else{
%>
<h1>老用户</h1>
<%
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
long start = session.getCreationTime(); //创建此会话的时间
long end = session.getLastAccessedTime();//客户端上一次发送与此会话关联的请求的时间,最后一次操作此会话时间
long time = (end-start)/1000; // 毫秒转换为秒
%>
<h1>您已经在此网页停留了<%=time %>秒</h1>
</body>
</html>
2.request
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Cookie c1 = new Cookie("hw","hello world");
Cookie c2 = new Cookie("hi","nice to meet you");
c1.setMaxAge(300);//设置最大生存时间300秒
c2.setMaxAge(30); //设置最大生存时间30秒
response.addCookie(c1);
response.addCookie(c2);
%>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Cookie [] c = request.getCookies();
if(c!=null){
for(int i=0;i<c.length;i++){
%>
<h1>name:<%=c[i].getName() %>--value:<%=c[i].getValue() %></h1>
<%
}
}
%>
</body>
</html>
3.定时刷新
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%!
int count = 0;
%>
<%
response.setHeader("refresh","1"); //每隔1秒刷新一次
%>
<h3>已经刷新了<%=count++ %>次</h3>
</body>
</html>
4.定时跳转页面一
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>访问错误,5秒后跳转至首页,如果没有跳转,请点击<a href=hello.jsp>这里</a></h1>
<%!
int count = 0;
%>
<%
response.setHeader("refresh","5; url=hello.jsp");
%>
</body>
</html>
4.定时跳转页面二
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5;url=hello.jsp" charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>访问错误,5秒后跳转至首页,如果没有跳转,请点击<a href=hello.jsp>这里</a></h1>
</body>
</html>
5.重定向
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
response.sendRedirect("hello.jsp");
%>
</body>
</html>
6.addcookie
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Cookie c1 = new Cookie("hw","hello world");
Cookie c2 = new Cookie("hi","nice to meet you");
c1.setMaxAge(300);//设置最大生存时间300秒
c2.setMaxAge(30); //设置最大生存时间30秒
response.addCookie(c1);
response.addCookie(c2);
%>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Cookie [] c = request.getCookies();
if(c!=null){
for(int i=0;i<c.length;i++){
%>
<h1>name:<%=c[i].getName() %>--value:<%=c[i].getValue() %></h1>
<%
}
}
%>
</body>
</html>
7.练习
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="login.jsp" method="post">
用户名:<input type="text" name="uname" ><br/>
密 码:<input type="password" name="upass" ><br/>
<input type="submit" value="登陆">
<input type="reset" value="清空">
</form>
<%
//用户名scott,密码orcl
String name=request.getParameter("uname");
String password=request.getParameter("upass"); if(!(name==null||"".equals(name.trim())||password==null||"".equals(password.trim()))){
if("scott".equals(name)&&"orcl".equals(password)){
response.setHeader("refresh","2;url=welcome.jsp");//2秒后跳转welcome页面
session.setAttribute("userName",name); //传值
%>
<h1>登陆成功,两秒后跳转至欢迎页 </h1>
<h1>如果没有跳转,请点击<a href="welcome.jsp">这里</a></h1>
<%
}else{
%>
<h3>用户名或密码错误,请重新登陆...</h3>
<%
response.setHeader("refresh","2;url=login.jsp");
}
}else{
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
session.invalidate();
response.setHeader("refresh","2;login.jsp");
%>
<h2>注销成功,两秒后跳转至首页</h2>
<h1>如果没有跳转,请点击<a href="login.jsp">这里</a></h1>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
if(session.getAttribute("userName")!=null){
%>
<h2>欢迎<%=session.getAttribute("userName") %>登陆本系统</h2>
<h1>注销请点击<a href="loginout.jsp">注销</a></h1>
<%
}else{
%>
<h1>请先<a href="login.jsp">登录</a></h1>
<%
}
%>
</body>
</html>
JSP中四种属性保存范围(2)的更多相关文章
- JSP中四种属性保存范围(1)
一.四种属性范围 在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效request:在一次服务请求范围内,服务器跳转后依然有效session:-在一次会话范围内,无论何种跳 ...
- jsp中四种传递参数的方法
jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...
- css样式表中四种属性选择器
学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...
- (转)JSP中四种传递参数的方法:
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- JSP中四种传递参数中文乱码问题
查看来源:http://blog.csdn.net/hackerain/article/details/6776083
- JSP九大内置对象和四种属性范围解读
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...
- Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读
一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...
- jsp四种属性范围
在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...
- JavaScript中四种不同的属性检测方式比较
JavaScript中四种不同的属性检测方式比较 1. 用in方法 var o = {x:1}; "x" in o; //true "y" in o; //fa ...
随机推荐
- ssh远程钥匙对连接
1.服务器必须启动ssh服务 2.在客户机执行命令:ssh-keygen -t rsa 两次回车即可 3.在客户机家目录下的.ssh\下生成钥匙对 4.将公钥传输到要连接的服务器主机要连接的用户家目录 ...
- Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍
原文:Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本 ...
- struts2导入多个xml引入报错<include>
struts.xml <?xml version="1.0" encoding="UTF-8"?> <!-- 指定Struts 2配置文件的D ...
- document.body.scrollTop值为0的解决方法[转]
做页面的时候可能会用到位置固定的层,读取document.body.scrollTop来设置层的位置,像这样, window.onscroll=function () { ...
- 浅谈协议(四)——wireshark强力解析视频流协议
参考链接: https://wenku.baidu.com/view/460f016e49d7c1c708a1284ac850ad02de800722.html https://wenku.baidu ...
- 动态规划—distinct-subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- Message相关函数对比
SendMessage PostMessage 过程类型 同步过程:等待其他程序处理消息完了之后才返回,继续执行 异步过程:只把消息放入队列,不管其他程序是否处理都返回,然后继续执行 返回值 表 ...
- 伊朗Cisco路由器遭黑客攻击 全国互联网几乎瘫痪
2018年4月9日,黑客攻击了伊朗的国家信息数据中心.伊朗internet信息安全部称,此次大规模袭击影响了全球约二十万个思科Cisco路由交换器,也包括伊朗的几千个路由器.攻击也影响了互联网服务供应 ...
- 未来HTML5的发展前景如何?黑客专家是这样回答的
如果你想进军IT行业,如果你准备好掌握一项新技术,那么就选择HTML5.近日,我们采访了国内知名网络黑客安全专家郭盛华,帮助您了解当今最重要的技术.在本篇文章中,黑客安全专家郭盛华回答了有关HTML5 ...
- 数据库(一):事务的特性与事务(在同一个 JVM 中)的传递
参考文章 https://blog.csdn.net/shuaihj/article/details/14163713 https://blog.csdn.net/shfqbluestone/arti ...