JSP五个重要内置对象

JSP五个重要内置对象

 

request、response、session、out、application对象,在_jspService方法内自动定义,由于JSP声明产生的是_jspService方法之外的代码,因此在声明中不能访问这些变量。

 

application是当前pageContext所得到的servletContext的对象引用。

 

 1.request对象

 

 客户端的请求信息,如请求的来源、标头、cookies和请求相关的参数值等,被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。request 对象实现javax.servlet.http.HttpServletRequest接口的,所提供的方法可以分为四大类:

1.储存和取得属性方法


void setAttribute(String name, Object value)       设定name属性的值为value
Enumeration getAttributeNamesInScope(int scope)    取得所有scope 范围的属性
Object getAttribute(String name)                   取得name 属性的值
void removeAttribute(String name)                  移除name 属性的值

 

2.取得请求参数的方法


String getParameter(String name)                   取得name 的参数值
Enumeration getParameterNames(                   取得所有的参数名称
String [] getParameterValues(String name)          取得所有name 的参数值
Map getParameterMap(                             取得一个要求参数的Map

 

3.能够取得请求HTTP 标头的方法


String getHeader(String name)                     取得name 的标头
Enumeration getHeaderNames()                     取得所有的标头名称
Enumeration getHeaders(String name)             取得所有name 的标头
int getIntHeader(String name)                     取得整数类型name 的标头
long getDateHeader(String name)                 取得日期类型name 的标头
Cookie [] getCookies(                         取得与请求有关的cookies
    

4.其他的方法


String getContextPath(                         取得Context 路径
String getMethod(                             取得HTTP 的方法(GET、POST)
String getProtocol(                       取得使用的协议 (HTTP/1.1、HTTP/1.0 )
String getQueryString(               取得请求的参数字符串,HTTP的方法必须为GET
String getRequestedSessionId(                 取得用户端的Session ID
String getRequestURI(               取得请求的URL,但是不包括请求的参数字符串
String getRemoteAddr(                            取得用户的IP 地址
String getRemoteHost(                            取得用户的主机名称
int getRemotePort(                            取得用户的主机端口
String getRemoteUser(                         取得用户的名称
void setCharacterEncoding(String    encoding)   设定编码格式,解决传递中文的问题

 

 

2.response对象

 

 response 对象主要将JSP 处理数据后的结果传回到客户端,response 对象实现了javax.servlet.http.HttpServletResponse 接口,response对象所提供的方法如下:


   1.设定表头的方法


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(java.lang.String name, java.lang.String value)
          Sets a response header with the given name and value.
 void setIntHeader(java.lang.String name, int value)
          Sets a response header with the given name and integer value.

 

例如:response.setHeader("refresh","1");//设置页面刷新时间,每隔1秒钟刷新一次


   void setRedirect(String url)

 

进行页面的重定向,这个URL既可以是绝对URL,也可以是相对URL;系统自动将相对URL转换成绝对URL。

需要注意的是,重定向的源组件和目标组件之间不共用同一个HttpServletRequest对象,因此不能共享request范围内的共享数据。

 

2.设定响应状态码的方法


void sendError(int sc)                             传送状态码(status code)
void sendError(int sc, String msg)                传送状态码和错误信息
void setStatus(int sc)                             设定状态码

 

3.用来URL 重写(rewriting)的方法    


public java.lang.String encodeURL(java.lang.String url)

Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.
public java.lang.String encodeRedirectURL(java.lang.String url)
Encodes the specified URL for use in the  sendRedirect method or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is separete from the  encodeURL method.


3.session对象

 

 session对象是HttpSession类的实例,指的是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开连接为止。session对象的信息保存在服务器中,但ID保存在客户机的Cookie中,如果客户机不支持Cookie则转为URL重写,一般在使用session对象时不必考虑其实现的细节问题。

 

public void setAttribute(String Key,Object obj)
   session对象类似于散列表,可以调用该方法将参数Object指定的对象obj添加到session对象中,并为其添加的对象指定了一个索引关键字.如果添加的两个对象的关键字相同,则先前添加的对象被清除.
  
   public Object getAttribute(String Key)
   获取session对象中含有的关键字是Key的对象.由于任何对象都可以添加到session对象中,因此该方法取回对象时,应进行强制类型转换.
  
   public Enumeration getAttributeName()
   session对象调用该方法产生一个枚举对象.该枚举对象使用nextElement()方法遍历session对象所含有的全部对象.
  
   public long getCreationTime()
   session对象调用该方法要以获取对象创建的时间.单位是毫秒(从1970年7月1日午夜起至该对象创建时刻所走过的毫秒数).
  
   public long getLastAccessedTime()
   获取当前session对象最后一次被操作的时间.
  
  public int getMaxInactiveInterval()
   得到允许session对象处于非活动状态的最长时间.
  
   public vodi setMaxInactiveInterval(int n)
   设置允许session对象处于非活动状态的最长时间.(单位是秒)
  
   public void removeAttribute(String Key)
   从当前session对象中删除关键字是key的对象.
  
  public String getId()
   获取session对象的编号.
  
   invalidate()
   使得session对象无效.
  

4.out对象

 

 out对象是JspWriter类的实例,把结果输出到网页上,主要是用来控制管理输出的缓冲区(buffer)和输出流(output stream)。

void clear() 清除缓冲区的内容

void clearBuffer() 清除缓冲区的当前内容

void flush() 清空流

int getBufferSize() 返回缓冲区以字节数的大小,如不设缓冲区则为0

int getRemaining() 返回缓冲区还剩余多少可用

boolean isAutoFlush() 返回缓冲区满时,是自动清空还是抛出异常

void close() 关闭输出流

 

5.application对象

 

 application对象实现了用户间数据的共享,可存放全局变量。它开始于服务器的启动,直到服务器的关闭,在此期间,此对象将一直存在;这样在用户的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性的操作,都将影响到其他用户对此的访问。服务器的启动和关闭决定了application对象的生命.application 对象实现javax.servlet.ServletContext 接口,ServletContext接口容器所提供的方法


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文件中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值