JSP中四种属性保存范围(2)
2024-10-20 05:23:20
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>
最新文章
- BZOJ1047: [HAOI2007]理想的正方形 [单调队列]
- iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】
- Hibernate快照
- hive 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法
- Mysql命令行中文乱码的解决方法
- nginx安装过程,报错处理:make[1]: *** [objs/addon/src/bson.o] Error 1
- webpack笔记_(2)_Refusing to install webpack as a dependency of itself
- 局部敏感哈希Locality Sensitive Hashing(LSH)之随机投影法
- HDU 1564 (博弈) Play a game
- 2014年辛星完全解读Javascript第二节
- FastDFS安装全过程记录(V5.05)
- mybatis源码之StatementHandler
- 在C# 中 如何限制在文本框(textBox)中输入的类型为正整数
- mysql函数调用过程
- LinkedHashMap为什么是有序的(与put先后顺序一致)
- CSS3-3
- (转)薛飞 基于VLC的Unity视频播放器(二)
- 图解Golang的GC算法
- falcon常用参数解析
- Go语言二叉树定义及遍历算法实现