JSP内建对象

来源:互联网 发布:淘宝今日销量 编辑:程序博客网 时间:2024/05/01 19:06
使用内建对象的目的
 -jsp为了简化页面的开发提供了一些内建对象
 -这些内建对象在使用之前不需要实例化
 -它们由容器(如:tomcat)来实现和管理
 -在所有jsp页面中都能使用内建对象
 -所有的内建对象只能在代码块和表达式中使用
 -不能在jsp声明中使用

-out内建对象,类型为javax.servlet.jsp.JspWriter,page scope
 -作用
  out对象被封装成javax.servlet.jsp.JspWriter接口
  表示为客户端打开的输出流
  PrintWriter使用它向客户端发送输出流
 -主要方法
  输出各种数据类型的数据,如:out.println(boolean)
  out.newLine();输出一个换行符
  out.flush();输出缓冲区里的数据
  out.close();关闭输出流
  out.clearBuffer();清除缓冲区中的数据,并把数据输出到客户端
  out.clear();清除缓冲区中的数据,但不把数据输出到客户端
  out.getBufferSize();获得缓冲区大小
  out.getRemaining();获得缓冲区中剩余大小
  out.isAutoFlush();判断缓冲区是否自动刷新

-request内建对象,类型为javax.servlet.http.HttpServletRequest,request scope
 -作用
   request对象代表请求对象
   它被封装成HttpServleRequest接口
   具有HttpServletRequest接口的所有特征
   它作为_jspService()方法的一个参数由容器传递给jsp页面
 -主要方法
   取得请求参数的方法
    -String getParameter(String name);取得name的参数值
    -Enumeration getParameterNames();取得所有的参数名称
    -String[] getParameterValues(String name);取得所有name的参数值
    -Map GetParameterMap();取得一个要求参数的Map
   取得请求标头的方法
    -String getHeader(String name);取得name的标头
    -Enumeration getHeaderNames();取得所有的标头名称
    -Enumeration getHeaders(String name);取得所有name 的标头
    -Map getParameterMap();取得一个要求参数的Map
    -int getIntHeader(String name);取得整数类型name的标头
    -long getDateHeader(String name);取得日期类型name的标头
    -Cookie[] getCookies();取得与请求有关的cookies
   其他请求的方法
    -String getContextPath();取得context路径
    -String getMethod();取得http的方法(get、post)
    -String getProtoco();取得使用的协议(http/1.1、http/1.0)
    -String getQueryString();取得请求的参数字符串,不过http的方法必须为get
    -String getRequestedSessionId();取得用户端的SessionId
    -String getRequestURI();取得请求的URI,但不包括请求的参数字符串
    -Cookie[] getCookies();取得与请求有关的cookies
    -String getRemoteAddr();取得用户的ip地址
    -String getRemoteHost();取得用户的主机名称
    -String getRemoteUser();取得用户的名称
    -int getRemotePort();取得用户的主机端口
    -void getCharacterEncoding(String encoding);设定编码格式,用来解决页面传递中文的问题

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><%String path=request.getContextPath();String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();out.println(path);out.println(basePath);String uri=request.getRequestURI();//相对路径out.println(uri);String url=request.getRequestURL().toString();//绝对路径out.println(url);%></body></html>


-response内建对象,类型为javax.servlet.http.HttpServletResponse,page scope
 -作用
  response对象主要将jsp处理数据后的结果传回到客户端
  response对象是实现javax.servlet.http.HttpServletResponse接口
 -主要方法
  设定表头的方法
   -void addCookie(Cookie cookie);新增cookie
   -void addDateHeader(String name,long date);新增long类型的值到name标头
   -void addHeader(String name,String value);新增String类型的值到name标头
   -void addIntHeader(String name,int value);新增int类型的值到name标头
   -void setDateHeader(String name,long date);指定long类型的值到name标头
   -void setHeader(String name,String value);指定String类型的值到name标头
   -void setIntHeader(String name,int value);指定int类型的值到name标头
  设定响应状态码的方法
   -void sendError(int sc);传递状态码(status code)
   -void sendError(int sc,String msg);传递状态码和错误信息
   -void setStatus(int sc);设定状态码
  用来URL重写(rewriting)的方法
   -String encodeRedirectURL(String url);对使用sendRedirect()方法的URL予以编码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><%//response.setStatus(404);response.sendRedirect("http://www.bing.com.cn");//remember http:// 与跳转%></body></html>


-session内建对象,类型为javax.servlet.http.HttpSession,session scope
 -作用
  session对象表示目前用户的会话(session)状况
  用此项机制可以轻易识别每一个用户,然后针对每一个别用户的要求,给予正确的响应
  session对象实现javax.servlet.http.HttpSession接口
 -主要方法
  long getCreationTime();取得session产生的事件,单位是毫秒,由1970年1月1日零时算起
  String getId();取得session的id
  long getLastAccessedTime();取得用户最后通过这个session送出请求的的时间,单位是毫秒,由1970年1月1日零时算起
  long getMaxInactiveInterval();取得最大session不活动的时间,若超过这时间,session将会失效,时间单位为秒
  void invalidate();取消session对象,并将对象存放的内容完全失效
  boolean isNew();判断session是否为“新”,所谓“新”的session是说session已由服务器产生,但是client尚未使用
  void setMaxInactiveInterval(int interval);设定最大session不活动的时间,若超过这时间,session将会失效,时间单位为秒

一个传递,一个接收

传递
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><%String name=(String)session.getAttribute("name");String pass=(String)request.getAttribute("pass");String word=(String)application.getAttribute("word");out.print(name+" "+pass+" "+word);%></body></html>

接收
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><%session.setAttribute("name", "hi");request.setAttribute("pass", "wi");application.setAttribute("word", "yi");%></body></html>


-pageContext内建对象,类型为javax.servlet.jsp.PageContext,page scope

-exception内建对象,类型为java.lang.Throwable,page scope

-config内建对象,类型为javax.servlet.ServletConfig,page scope

-page内建对象,类型为java.lang.Object,page scope

-application内建对象,类型为javax.servlet.ServletContext,application scope
 -作用
  application对象实现javax.servlet.ServletContext接口
  它主要用在取得或更改servlet的设定
 -主要方法
  容器相关信息的方法
   -int getMajorVersion();取得Container主要的servlet API版本
   -int getMinorVersion();取得Container次要的servlet API版本
   -String getServerInfo();取得Container的名称和版本
  有关服务器的路径和文件的方法
   -String getMimeType(String file);取得指定文件的MIME类型
   -ServletContext getContext(String uripath);取得指定Local URL 的Application context
   -String getRealPath(String path);取得本地端path的绝对路径
  有关日志记录的方法
   -void log(String message);将信息写入log文件中
   -void log(String message,Throwable throwable);将stack trace所产生的异常信息写入log文件中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><%String path=application.getRealPath("upload");//取得upload文件夹的绝对路径out.println(path);HttpSession s=pageContext.getSession();//pageContext用法HttpServletRequest r=(HttpServletRequest)pageContext.getRequest();JspWriter jw=pageContext.getOut();%></body></html>


0 0
原创粉丝点击