码迷,mamicode.com
首页 > Web开发 > 详细

jsp内置对象(四):response,out,pagecontext

时间:2015-08-02 16:53:11      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:jsp

一:response对象

1.定时刷新

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘responseDemo1.jsp‘ starting page</title>
  </head>
  <body>
  <%!
  //定义一个全局变量
  int count=0;
  %>
  <%
  response.setHeader("refresh","2");
  %>
  <h3>刷新了<%=count++ %>次!</h3>
  </body>
</html>


2.设置Cookie

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘responseDemo2.jsp‘ starting page</title>
  </head>
  <body>
  <%
  //创建cookie
  Cookie ck1=new Cookie("aa","Spring");
  Cookie ck2=new Cookie("bb","Summer");
  //设置保存时间
  ck1.setMaxAge(60);
  ck2.setMaxAge(60);
  //添加cookie
  response.addCookie(ck1);
  response.addCookie(ck2);
  %>
  </body>
</html>


3.获取cookie

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘responseDemo1.jsp‘ starting page</title>
  </head>
  <body>
   <%
  //取出Cookie
  Cookie c[]=request.getCookies();
  for(int i=0;i<c.length;i++){
  %>
  <h3><%=c[i].getName() %>--><%=c[i].getValue() %></h3>
  <%
  }
  %>
  </body>
</html>

二:out对象

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘init.jsp‘ starting page</title>
  </head>
  <body>
  <%
  int buffer=out.getBufferSize();
  int avaliable=out.getRemaining();
  int use=buffer-avaliable;
  %>
  缓冲区大小:<%=buffer %><br>
  未使用的大小:<%=avaliable %><br>
  使用的大小:<%=use %>
  </body>
</html>


三:pageContext对象

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘pageContext.jsp‘ starting page</title>
  </head>
  <body>
  <%
  //页面跳转
  pageContext.forward("Demo2.jsp?info=spring");
  %>
  </body>
</html>


Demo2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘Demo2.jsp‘ starting page</title>
  </head>
  <body>
  <%
  String info=pageContext.getRequest().getParameter("info");
  %>
  <h3>info=<%=info %></h3>
  <h3>realPath=<%=pageContext.getServletContext().getRealPath("/") %></h3>
  </body>
</html>

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

jsp内置对象(四):response,out,pagecontext

标签:jsp

原文地址:http://blog.csdn.net/dzy21/article/details/47207963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!