1.session

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. //获取session ID Manager 可以将session保存在本地上
  10. String id = session.getId();
  11. %>
  12. <h1><%=id %></h1>
  13. </body>
  14. </html>
  1. <%@ page language="java" contentType="text/html"
  2. pageEncoding="GBK"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <title>Insert title here</title>
  7. </head>
  8. <body>
  9. <%
  10. if(session.isNew()){
  11. %>
  12. <h1>欢迎新用户</h1>
  13. <%
  14. }else{
  15. %>
  16. <h1>老用户</h1>
  17. <%
  18. }
  19. %>
  20. </body>
  21. </html>
  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. long start = session.getCreationTime(); //创建此会话的时间
  10. long end = session.getLastAccessedTime();//客户端上一次发送与此会话关联的请求的时间,最后一次操作此会话时间
  11. long time = (end-start)/1000; // 毫秒转换为秒
  12. %>
  13. <h1>您已经在此网页停留了<%=time %>秒</h1>
  14. </body>
  15. </html>

2.request

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. Cookie c1 = new Cookie("hw","hello world");
  10. Cookie c2 = new Cookie("hi","nice to meet you");
  11. c1.setMaxAge(300);//设置最大生存时间300秒
  12. c2.setMaxAge(30); //设置最大生存时间30秒
  13. response.addCookie(c1);
  14. response.addCookie(c2);
  15. %>
  16. </body>
  17. </html>
  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. Cookie [] c = request.getCookies();
  10. if(c!=null){
  11. for(int i=0;i<c.length;i++){
  12. %>
  13. <h1>name:<%=c[i].getName() %>--value:<%=c[i].getValue() %></h1>
  14. <%
  15. }
  16. }
  17. %>
  18. </body>
  19. </html>

3.定时刷新

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%!
  9. int count = 0;
  10. %>
  11. <%
  12. response.setHeader("refresh","1"); //每隔1秒刷新一次
  13. %>
  14. <h3>已经刷新了<%=count++ %>次</h3>
  15. </body>
  16. </html>

4.定时跳转页面一

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1>访问错误,5秒后跳转至首页,如果没有跳转,请点击<a href=hello.jsp>这里</a></h1>
  9. <%!
  10. int count = 0;
  11. %>
  12. <%
  13. response.setHeader("refresh","5; url=hello.jsp");
  14. %>
  15. </body>
  16. </html>

4.定时跳转页面二

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="refresh" content="5;url=hello.jsp" charset="utf-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1>访问错误,5秒后跳转至首页,如果没有跳转,请点击<a href=hello.jsp>这里</a></h1>
  9. </body>
  10. </html>

5.重定向

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. response.sendRedirect("hello.jsp");
  10. %>
  11. </body>
  12. </html>

6.addcookie

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. Cookie c1 = new Cookie("hw","hello world");
  10. Cookie c2 = new Cookie("hi","nice to meet you");
  11. c1.setMaxAge(300);//设置最大生存时间300秒
  12. c2.setMaxAge(30); //设置最大生存时间30秒
  13. response.addCookie(c1);
  14. response.addCookie(c2);
  15. %>
  16. </body>
  17. </html>
  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. Cookie [] c = request.getCookies();
  10. if(c!=null){
  11. for(int i=0;i<c.length;i++){
  12. %>
  13. <h1>name:<%=c[i].getName() %>--value:<%=c[i].getValue() %></h1>
  14. <%
  15. }
  16. }
  17. %>
  18. </body>
  19. </html>
  1.  

7.练习

  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form action="login.jsp" method="post">
  9. 用户名:<input type="text" name="uname" ><br/>
  10. 密 码:<input type="password" name="upass" ><br/>
  11. <input type="submit" value="登陆">
  12. <input type="reset" value="清空">
  13. </form>
  14. <%
  15. //用户名scott,密码orcl
  16. String name=request.getParameter("uname");
  17. String password=request.getParameter("upass");
  18.  
  19. if(!(name==null||"".equals(name.trim())||password==null||"".equals(password.trim()))){
  20. if("scott".equals(name)&&"orcl".equals(password)){
  21. response.setHeader("refresh","2;url=welcome.jsp");//2秒后跳转welcome页面
  22. session.setAttribute("userName",name); //传值
  23. %>
  24. <h1>登陆成功,两秒后跳转至欢迎页 </h1>
  25. <h1>如果没有跳转,请点击<a href="welcome.jsp">这里</a></h1>
  26. <%
  27. }else{
  28. %>
  29. <h3>用户名或密码错误,请重新登陆...</h3>
  30. <%
  31. response.setHeader("refresh","2;url=login.jsp");
  32. }
  33. }else{
  34. }
  35. %>
  36. </body>
  37. </html>
  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. session.invalidate();
  10. response.setHeader("refresh","2;login.jsp");
  11. %>
  12. <h2>注销成功,两秒后跳转至首页</h2>
  13. <h1>如果没有跳转,请点击<a href="login.jsp">这里</a></h1>
  14. </body>
  15. </html>
  1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <%
  9. if(session.getAttribute("userName")!=null){
  10. %>
  11. <h2>欢迎<%=session.getAttribute("userName") %>登陆本系统</h2>
  12. <h1>注销请点击<a href="loginout.jsp">注销</a></h1>
  13. <%
  14. }else{
  15. %>
  16. <h1>请先<a href="login.jsp">登录</a></h1>
  17. <%
  18. }
  19. %>
  20. </body>
  21. </html>
  1.  

JSP中四种属性保存范围(2)的更多相关文章

  1. JSP中四种属性保存范围(1)

    一.四种属性范围 在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效request:在一次服务请求范围内,服务器跳转后依然有效session:-在一次会话范围内,无论何种跳 ...

  2. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

  3. css样式表中四种属性选择器

    学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...

  4. (转)JSP中四种传递参数的方法:

    1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...

  5. JSP中四种传递参数中文乱码问题

    查看来源:http://blog.csdn.net/hackerain/article/details/6776083

  6. JSP九大内置对象和四种属性范围解读

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...

  7. Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读

    一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...

  8. jsp四种属性范围

    在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...

  9. JavaScript中四种不同的属性检测方式比较

    JavaScript中四种不同的属性检测方式比较 1. 用in方法 var o = {x:1}; "x" in o; //true "y" in o; //fa ...

随机推荐

  1. BZOJ 1179 (Tarjan缩点+DP)

    题面 传送门 分析 由于一个点可以经过多次,显然每个环都会被走一遍. 考虑缩点,将每个强连通分量缩成一个点,点权为联通分量上的所有点之和 缩点后的图是一个有向无环图(DAG) 可拓扑排序,按照拓扑序进 ...

  2. 事件对象e的实现原理

    转自:https://segmentfault.com/q/1010000007337410?_ea=1313467 事件对象传递原理 1.前置知识回顾 在讲传递原理前,我们先看看普通函数是如何传递参 ...

  3. 用jquery制作的简单轮播图

    我也是进入H5前端的小菜鸟一枚,最近才进入jquery的学习,所以打算对自己的学习进行记录. 今天分享的是一个简单的轮播图,这个轮播图的特效很简单,能够进行图片的轮播以及点击相应图片,图片能够跳转到相 ...

  4. maven整合S2SH

    1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...

  5. 1. AtomicInteger 、Unsafe 及 CAS方法的整理

    本文摘自: https://blog.csdn.net/fanrenxiang/article/details/80623884 http://ifeve.com/sun-misc-unsafe/ h ...

  6. Tunnel connection failed: 407 Proxy Authentication Required

    报错信息 : Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connecti ...

  7. HttpClientUtil工具类封装

    package com.jd.ng.shiro.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; ...

  8. windows 安装 mysql 5.6

    从官方网站下载安装包:mysql-5.6.33-winx64.zip,解压到d:\java,然后将解压后的bin目录加入系统环境变量Path中,进入mysql根目录,编辑my-default.ini, ...

  9. 【leetcode】870. Advantage Shuffle

    题目如下: 解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”.言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大 ...

  10. 百度编辑器UEditor使用总结

    官网下载地址:http://ueditor.baidu.com/website/download.html 我下载的是jsp版本,下载后将整个目录复制到项目的js包下,然后将jsp包下的lib下的ja ...