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>
 

最新文章

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

热门文章

  1. 从建立yum仓库到搭建ftp以及http服务
  2. 06-File-文件
  3. vue框架中实现今天昨天前天最近时间
  4. jquery 点击加载更多
  5. python 中PIL.Image和OpenCV图像格式相互转换
  6. AI-sklearn 学习笔记(一)sklearn 一般概念
  7. C#Stopwatch的简单计时 [收藏]
  8. 1-基于Xilinx XCKU115的半高PCIe x8 硬件加速卡
  9. 继承和构造函数语法造成的一个bug
  10. 在父组件中,直接获取子组件数据-vue