JSP九大内置对象

① out - javax.servlet.jsp.jspWriter
out对象用于把结果输出到网页上。

方法:
1. void clear() ;
清除输出缓冲区的内容,但是不输出到客户端。

2. void clearBuffer() ;
清除输出缓冲区的内容,并输出到客户端。

3. void close() ;
关闭输出流,清除所有内容。

4. void flush() ;
输出缓冲区里面的数据。

5. int getBufferSize() ;
获取以kb为单位的目前缓冲区大小。

6. int getRemaining() ;
获取以kb为单位的缓冲区中未被占用的空间大小。

7. boolean isAutoFlush() ;
是否自动刷新缓冲区。

8. void newLine() ;
输出一个换行字符。

9. void print( boolean b ) ;
void print( char c ) ;
void print( char[] s ) ;
void print( double d ) ;
void print( float f ) ;
void print( int i ) ;
void print( long l ) ;
void print( Object obj ) ;
void print( String s ) ;
将指定类型的数据输出到Http流,不换行。

10. void println( boolean b ) ;
void println( char c ) ;
void println( char[] s ) ;
void println( double d ) ;
void println( float f ) ;
void println( int i ) ;
void println( long l ) ;
void println( Object obj ) ;
void println( String s ) ;
将指定类型的数据输出到Http流,并输出一个换行符。

11. Appendable append( char c ) ;
Appendable append( CharSequence cxq, int start, int end ) ;
Appendable append( CharSequence cxq ) ;
将一个字符或者实现了CharSequence接口的对象添加到输出流的后面。

成员:
int DEFAULT_BUFFER = 0 - 缺省缓冲区大小
int NO_BUFFER = -1 - writer是否处于缓冲输出状态
int UNBOUNDED_BUFFER = -2 - 是否限制缓冲区大小


② request - javax.servlet.http.HttpServletRequest
request对象包含所有请求的信息,如请求的来源、标头、cookies和请求相关的参数值等。

方法:
1. Object getAttribute( String name ) ;
返回由name指定的属性值,该属性不存在时返回null。

2. Enumeration getAttributeNames() ;
返回request对象的所有属性名称的集合。

3. String getAuthType() ;
返回用来保护servlet的认证方法的名称,未受保护时返回null。

4. String getCharacterEncoding() ;
返回请求中的字符编码方法,可以在response对象中设置。

5. int getContentLength() ;
返回请求的BODY的长度,不能确定长度时返回-1。可以在response中设置。

6. String getContentType() ;
返回在response中定义的内容类型。

7. String getContentPath() ;
返回请求的路径。

8. Cookie[] getCookies() ;
返回客户端所有的Cookie的数组。

9. Enumeration getHeaderNames() ;
返回所有HTTP头的名称的集合。

10. Enumeration getHeaders( String name ) ;
返回指定HTTP头的所有值的集合。

11. String getHeader( String name ) ;
返回指定名称的HTTP头的信息。

12. long getDateHeader( String name ) ;
返回指定名称的Data类型的HTTP头的信息。

13. int getIntHeader( String name ) ;
返回指定名称的Int类型的HTTP头的信息。

14. ServletInputStream getInputStream() ;
返回请求的输入流。

15. Locale getLocale() ;
返回当前页的Locale对象,可以在response中设定。

16. Enumeration getLocales() ;
返回请求中所有的Locale对象的集合。

17. String getLocalName() ;
获取响应请求的服务器端主机名。

18. String getLocalAddr() ;
获取响应请求的服务器端地址。

19. int getLocalPort() ;
获取响应请求的服务器端端口

20. String getMethod() ;
获取客户端向服务器端发送请求的方法(GET、POST)。

21. String getParameter( String name ) ;
获取客户端发送给服务器端的参数值。

22. Map getParameterMap() ;
该方法返回包含请求中所有参数的一个Map对象。

23. Enumeration getParameterNames() ;
返回请求中所有参数的集合。

24. String[] getParameterValues( String name ) ;
获得请求中指定参数的所有值。

25. String getQueryString() ;
返回get方法传递的参数字符串,该方法不分解出单独的参数。

26. String getPathInfo() ;
取出请求中处于ServletPath和QueryString之间的额外信息。

27. String getPathTranslated() ;
返回用getPathInfo()方法取得的路径信息的实际路径。

28. String getProtocol() ;
返回请求使用的协议。可以是HTTP1.1或者HTTP1.0。

29. BufferedReader getReader() ;
返回请求的输入流对应的Reader对象,该方法和getInputStream()方法在一个页面中只能调用一个。

30. String getRemoteAddr() ;
获取发出请求的客户端IP地址。

31. String getRemoteHost() ;
获取发出请求的客户端主机名

32. String getRemoteUser() ;
返回经过客户端验证的用户名,未经验证返回null。

33. int getRemotePort() ;
返回发出请求的客户端主机端口。

34. String getRealPath( String path ) ;
返回给定虚拟路径的物理路径。

35. RequestDispatcher getRequestDispatcher( String path ) ;
按给定的路径生成资源转向处理适配器对象。

36. String getRequestedSessionId() ;
返回请求的session的标识。

37. String RequestURI() ;
返回发出请求的客户端地址,但是不包括请求的参数字符串。

38. StringBuffer getRequestURI() ;
返回响应请求的服务器端地址

39. String getScheme() ;
获取协议名称,缺省值为HTTP协议。

40. String getServerName() ;
返回响应请求的服务器名称。

41. String getServletPath() ;
获取客户端所请求的脚本文件的文件路径。

42. int getServerPort() ;
获取响应请求的服务器端主机端口号。

43. void removeAttribute( String name ) ;
在属性列表中删除指定名称的属性。

44. void setAttribute( String name, Object value ) ;
在属性列表中添加/删除指定的属性。

45. void setCharacterEncoding( String name ) ;
设置请求的字符编码格式。

46. HttpSession getSession() ;
HttpSession getSession( boolean create ) ;
获取session,如果create为true,在无session的情况下创建一个。

47. boolean isRequestedSessionIdFromCookie() ;
检查请求的会话ID是否为通过Cookie传入。

48. boolean isRequestedSessionIdFromURL() ;
检查请求的会话ID是否为通过URL传入。

49. boolean isRequestedSessionIdValid() ;
检查请求的会话ID是否仍然有效。

50. boolean isSecure() ;
检查请求是否使用安全链接,如果HTTPS等。

51. boolean isUserInRole( String role ) ;
检查已经通过验证的用户是否在是role所指定的角色。

52. Principal getUserPrincipal() ;
返回包含用户登陆名的一个java.security.Principal对象。

成员:
String BASIC_AUTH = "BASIC" -
String CLIENT_CERT_AUTH = "CLIENT_CERT" -
String DIGEST_AUTH = "DIGEST" -
String FORM_AUTH = "FORM" -


③ response - javax.servlet.http.HttpServletResponse
response对象主要将JSP容器处理后的结果传回到客户端。

方法:
1. void addCookie( Cookie cookie ) ;
添加一个Cookie对象,保存客户端信息。

2. void addDateHeader( String name, long value ) ;
添加一个日期类型的HTTP头信息,覆盖同名的HTTP头信息。

3. void addHeader( String name, String value ) ;
添加一个HTTP头,覆盖同名的旧HTTP头。

4. void addIntHeader( String name, int value ) ;
添加一个整型的HTTP头,覆盖同名的旧HTTP头。

5. boolean containsHeader( String name ) ;
判断指定的HTTP头是否存在。

6. String encodeRedirectURL( String url ) ;
对sendRedirect()方法使用的URL进行编码。

7. String encodeURL( String url ) ;
将URL予以编码,回传包含session ID的URL。

8. void flushBuffer() ;
强制把当前缓冲区的内容发送到客户端。

9. int getBufferSize() ;
取得以kb为单位的缓冲区大小。

10. String getCharacterEncoding() ;
获取响应的字符编码格式。

11. String getContentType() ;
获取响应的类型。

12. Locale getLocale() ;
获取响应的Locale对象。

13. ServletOutputStream getOutputStream() ;
返回客户端的输出流对象。

14. PrintWriter getWriter() ;
获取输出流对应的writer对象。

15. boolean isCommitted() ;
判断服务器端是否已经将数据输出到客户端。

16. void reset() ;
清空buffer中的所有内容。

17. void resetBuffer() ;
情况buffer中所有的内容,但是保留HTTP头和状态信息。

18. void sendError( int xc, String msg ) ;
void sendError( int xc ) ;
发送错误,包括状态码和错误信息。

19. void sendRedirect( String locationg ) ;
把响应发送到另外一个位置进行处理。

20. void setBufferSize( int size ) ;
设置以kb为单位的缓冲区大小。

21. void setCharacterEncoding( String charset ) ;
设置响应使用的字符编码格式。

22. void setContentLength( int length ) ;
设置响应的BODY长度。

23. void setContentType( String type ) ;
设置响应的类型。

24. void setDateHeader( String name, long value ) ;
设置指定名称的Data类型的HTTP头的值。

25. void setHeader( String name, String value ) ;
设置指定名称的HTTP头的值。

26. void setIntHeader( String name, int value ) ;
设置指定名称的int类型的HTTP头的值。

27. void setStatus( int xc ) ;
设置响应状态码,新值会覆盖当前值。

成员(HTTP状态码):
int SC_CONTINUE = 100 int SC_SWITCHING_PROTOCOLS = 101
int SC_OK = 200 int SC_NON_AUTHORITATIVE_INFORMATION = 203
int SC_ACCEPTED = 202 int SC_CREATED = 201
int SC_NO_CONTENT = 204 int SC_RESET_CONTENT = 205
int SC_PARTIAL_CONTENT = 206 int SC_MULTIPLE_CHOICES = 300
int SC_MOVED_PERMANENTLY = 301 int SC_MOVED_TEMPORARILY = 302
int SC_FOUND = 302 int SC_SEE_OTHER = 303
int SC_NOT_MODIFIED = 304 int SC_USE_PROXY = 305
int SC_TEMPORARY_REDIRECT = 307 int SC_BAD_REQUEST = 400
int SC_UNAUTHORIZED = 401 int SC_PAYMENT_REQUIRED = 402
int SC_FORBIDDEN = 403 int SC_NOT_FOUND = 404
int SC_METHOD_NOT_ALLOWED = 405 int SC_NOT_ACCEPTABLE = 406
int SC_PROXY_AUTHENTICATION_REQUIRED = 407 int SC_REQUEST_TIMEOUT = 408
int SC_CONFLICT = 409 int SC_GONE = 410
int SC_LENGTH_REQUIRED = 411 int SC_PRECONDITION_FAILED = 412
int SC_REQUEST_ENTITY_TOO_LARGE = 413 int SC_REQUEST_URI_TOO_LONG = 414
int SC_UNSUPPORTED_MEDIA_TYPE = 415 int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416
int SC_EXPECTATION_FAILED = 417 int SC_INTERNAL_SERVER_ERROR = 500
int SC_NOT_IMPLEMENTED = 501 int SC_BAD_GATEWAY = 502
int SC_SERVICE_UNAVAILABLE = 503 int SC_GATEWAY_TIMEOUT = 504
int SC_HTTP_VERSION_NOT_SUPPORTED = 505


④ session - javax.servlet.http.HttpSession
session对象表示目前个别用户的会话状态,用来识别每个用户。

方法:
1. Object getAttribute( String name ) ;
获取与指定名字相关联的session属性值。

2. Enumeration getAttributeNames() ;
取得session内所有属性的集合。

3. long getCreationTime() ;
返回session的创建时间,最小单位千分之一秒。

4. String getId() ;
取得session标识。

5. long getLastAccessedTime() ;
返回与当前session相关的客户端最后一次访问的时间,由1970-01-01算起,单位毫秒。

6. int getMaxInactiveInterval( int interval ) ;
返回总时间,以秒为单位,表示session的有效时间(session不活动时间)。-1为永不过期。

7. ServletContext getServletContext() ;
返回一个该JSP页面对应的ServletContext对象实例。

8. HttpSessionContext getSessionContext() ;


9. Object getValue( String name ) ;
取得指定名称的session变量值,不推荐使用。

10. String[] getValueNames() ;
取得所有session变量的名称的集合,不推荐使用。

11. void invalidate() ;
销毁这个session对象。

12. boolean isNew() ;
判断一个session是否由服务器产生,但是客户端并没有使用。

13. void pubValue( String name, Object value ) ;
添加一个session变量,不推荐使用。

14. void removeValue( String name ) ;
移除一个session变量的值,不推荐使用。

15. void setAttribute( String name, String value ) ;
设置指定名称的session属性值。

16. void setMaxInactiveInterval( int interval ) ;
设置session的有效期。

17. void removeAttribute( String name ) ;
移除指定名称的session属性。


⑤ pageContext - javax.servlet.jsp.PageContext
pageContext对象存储本JSP页面相关信息,如属性、内建对象等。

方法:
1. void setAttribute( String name, Object value, int scope ) ;
void setAttribute( String name, Object value ) ;
在指定的共享范围内设置属性。

2. Object getAttribute( String name, int scope ) ;
Object getAttribute( String name ) ;
取得指定共享范围内以name为名字的属性值。

3. Object findAttribute( String name ) ;
按页面、请求、会话和应用程序共享范围搜索已命名的属性。

4. void removeAttribute( String name, int scope ) ;
void removeAttribute( String name ) ;
移除指定名称和共享范围的属性。

5. void forward( String url ) ;
将页面导航到指定的URL。

6. Enumeration getAttributeNamesScope( int scope ) ;
取得指定共享范围内的所有属性名称的集合。

7. int getAttributeScope( String name ) ;
取得指定属性的共享范围。

8. ErrorData getErrorDate() ;
取得页面的errorData对象。

9. Exception getException() ;
取得页面的exception对象。

10. ExpressionEvaluator getExpressionEvaluator() ;
取得页面的expressionEvaluator对象。

11. JspWriter getOut() ;
取得页面的out对象。

12. Object getPage() ;
取得页面的page对象。

13. ServletRequest getRequest() ;
取得页面的request对象。

14. ServletResponse getResponse() ;
取得页面的response对象。

15. ServletConfig getConfig() ;
取得页面的config对象。

16. ServletContext getServletContext() ;
取得页面的servletContext对象。

17. HttpSession getSession() ;
取得页面的session对象。

18. VariableResolver getVariableResolver() ;
取得页面的variableResolver对象。

19. void include( String url, boolean flush ) ;
void include( String url ) ;
包含其他的资源,并指定是否自动刷新。

20. void release() ;
重置pageContext内部状态,释放所有内部引用。

21. void initialize( Servlet servlet, ServletRequest request, ServletResponse response,
String errorPageURL, boolean needSession, int bufferSize, boolean autoFlush ) ;
初始化未经初始化的pageContext对象。

22. BodyContext pushBody() ;
BodyContext pushBody( Writer writer ) ;
保存当前的out对象,并更新pageContext中page范围内的out对象。

23. JspWrite popBody() ;
取出由pushBody()方法保存的out对象。

24. void handlePageException( Exception e ) ;
void handlePageException( Thrwoable t ) ;


成员:
int PAGE_SCOPE = 1 - 页面共享范围
int REQUEST_SCOPE = 2 - 请求共享范围
int SESSION_SCOPE = 3 - 会话共享范围
int APPLICATION_SCOPE = 4 - 应用程序共享范围
String PAGE = "javax.servlet.jsp.jspPage"
String PAGECONTEXT = "javax.servlet.jsp.jspPageContext"
String REQUEST = "javax.servlet.jsp.jspRequest"
String RESPONSE = "javax.servlet.jsp.jspResponse"
String CONFIG = "javax.servlet.jsp.jspConfig"
String SESSION = "javax.servlet.jsp.jspSession"
String OUT = "javax.servlet.jsp.jspOut"
String APPLICATION = "javax.servlet.jsp.jspApplication"
String EXCEPTION = "javax.servlet.jsp.jspException"


⑥ application - javax.servlet.ServletContext
application主要功用在于取得或更改Servlet的设定。

方法:
1. Object getAttribute( String name ) ;
返回由name指定的application属性。

2. Enumeration getAttributes() ;
返回所有的application属性。

3. ServletContext getContext( String uripath ) ;
取得当前应用的ServletContext对象。

4. String getInitParameter( String name ) ;
返回由name指定的application属性的初始值。

5. Enumeration getInitParameters() ;
返回所有的application属性的初始值的集合。

6. int getMajorVersion() ;
返回servlet容器支持的Servlet API的版本号。

7. String getMimeType( String file ) ;
返回指定文件的类型,未知类型返回null。一般为"text/html"和"image/gif"。

8. int getMinorVersion() ;
返回servlet容器支持的Servlet API的副版本号。

9. String getRealPath( String path ) ;
返回给定虚拟路径所对应物理路径。

10. RequestDispatcher getNamedDispatcher( String name ) ;
为指定名字的Servlet对象返回一个RequestDispatcher对象的实例。

11. RequestDispatcher getRequestDispatcher( String path ) ;
返回一个RequestDispatcher对象的实例。

12. URL getResource( String path ) ;
返回指定的资源路径对应的一个URL对象实例,参数要以"/"开头。

13. InputStream getResourceAsStream( String path ) ;
返回一个由path指定位置的资源的InputStream对象实例。

14. Set getResourcePaths( String path ) ;
返回存储在web-app中所有资源路径的集合。

15. String getServerInfo() ;
取得应用服务器版本信息。

16. Servlet getServlet( String name ) ;
在ServletContext中检索指定名称的servlet。

17. Enumeration getServlets() ;
返回ServletContext中所有servlet的集合。

18. String getServletContextName() ;
返回本web应用的名称。

19. Enumeration getServletContextNames() ;
返回ServletContext中所有servlet的名称集合。

20. void log( Exception ex, String msg ) ;
void log( String msg, Throwable t ) ;
void log( String msg ) ;
把指定的信息写入servlet log文件。

21. void removeAttribute( String name ) ;
移除指定名称的application属性。

22. void setAttribute( String name, Object value ) ;
设定指定的application属性的值。


⑦ config - javax.servlet.ServletConfig
config对象用来存放Servlet初始的数据结构。

方法:
1. String getInitParameter( String name ) ;
返回名称为name的促使参数的值。

2. Enumeration getInitParameters() ;
返回这个JSP所有的促使参数的名称集合。

3. ServletContext getContext() ;
返回执行者的servlet上下文。

4. String getServletName() ;
返回servlet的名称。


⑧ exception - java.lang.Throwable
错误对象,只有在JSP页面的page指令中指定isErrorPage="true"后,才可以在本页面使用exception对象。

方法:
1. Throwable fillInStackTrace() ;
将当前stack信息记录到exception对象中。

2. String getLocalizedMessage() ;
取得本地语系的错误提示信息。

3. String getMessage()
取得错误提示信息。

4. StackTrackElement[] getStackTrace() ;
返回对象中记录的call stack track信息。

5. Throwable initCause( Throwable cause ) ;
将另外一个异常对象嵌套进当前异常对象中。

6. Throwable getCause() ;
取出嵌套在当前异常对象中的异常。

7. void printStackTrace() ;
void printStackTrace( printStream s ) ;
void printStackTrace( printWriter s ) ;
打印出Throwable及其call stack trace信息。

8. void setStackTrace( StackTraceElement[] stackTrace )
设置对象的call stack trace信息。


⑨ page - javax.servlet.jsp.HttpJspPage
page对象代表JSP对象本身,或者说代表编译后的servlet对象,
可以用( (javax.servlet.jsp.HttpJspPage)page )来取用它的方法和属性。


示例

JSP内置对象
1.request对象
客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。
序号 方 法 说 明
1 object getAttribute(String name) 返回指定属性的属性值
2 Enumeration getAttributeNames() 返回所有可用属性名的枚举
3 String getCharacterEncoding() 返回字符编码方式
4 int getContentLength() 返回请求体的长度(以字节数)
5 String getContentType() 得到请求体的MIME类型
6 ServletInputStream getInputStream() 得到请求体中一行的二进制流
7 String getParameter(String name) 返回name指定参数的参数值
8 Enumeration getParameterNames() 返回可用参数名的枚举
9 String[] getParameterValues(String name) 返回包含参数name的所有值的数组
10 String getProtocol() 返回请求用的协议类型及版本号
11 String getScheme() 返回请求用的计划名,如:http.https及ftp等
12 String getServerName() 返回接受请求的服务器主机名
13 int getServerPort() 返回服务器接受此请求所用的端口号
14 BufferedReader getReader() 返回解码过了的请求体
15 String getRemoteAddr() 返回发送此请求的客户端IP地址
16 String getRemoteHost() 返回发送此请求的客户端主机名
17 void setAttribute(String key,Object obj) 设置属性的属性值
18 String getRealPath(String path) 返回一虚拟路径的真实路径
19
20

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request对象_例1</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
<input type="text" name="qwe">
<input type="submit" value="提交">
</form>
请求方式:<%=request.getMethod()%><br>
请求的资源:<%=request.getRequestURI()%><br>
请求用的协议:<%=request.getProtocol()%><br>
请求的文件名:<%=request.getServletPath()%><br>
请求的服务器的IP:<%=request.getServerName()%><br>
请求服务器的端口:<%=request.getServerPort()%><br>
客户端IP地址:<%=request.getRemoteAddr()%><br>
客户端主机名:<%=request.getRemoteHost()%><br>
表单提交来的值:<%=request.getParameter("qwe")%><br>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.Enumeration"%>
<html>
<head>
<title>request对象_例2</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
用户名:<input type="text" name="username">  
密 码:<input type="text" name="userpass">  
<input type="submit" value="进入" >
</form>
<%
String str="";
if(request.getParameter("username")!=null &&
request.getParameter("userpass")!=null){
Enumeration enumt = request.getParameterNames();
while(enumt.hasMoreElements()){
str=enumt.nextElement().toString();
out.println(str+":"+request.getParameter(str)+"<br>");
}
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request对象_例3</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
擅长:<input type="checkbox" name="cb" value="ON1">VC++ 
<input type="checkbox" name="cb" value="ON2">JAVA 
<input type="checkbox" name="cb" value="ON3">DELPHI 
<input type="checkbox" name="cb" value="ON4">VB 
<br>
<input type="submit" value="进入" name="qwe">
</form>
<%
if(request.getParameter("qwe")!=null ){
for(int i=0;i<request.getParameterValues("cb").length;i++){
out.println("cb"+i+":"+request.getParameterValues("cb")[i]+"<br>");
}
out.println(request.getParameter("qwe"));
}
%>
</body>
</html>
 
 
2.response对象
response对象包含了响应客户请求的有关信息,但在JSP中很少直接用到它。它是HttpServletResponse类的实例。
序号 方 法 说 明
1 String getCharacterEncoding() 返回响应用的是何种字符编码
2 ServletOutputStream getOutputStream() 返回响应的一个二进制输出流
3 PrintWriter getWriter() 返回可以向客户端输出字符的一个对象
4 void setContentLength(int len) 设置响应头长度
5 void setContentType(String type) 设置响应的MIME类型
6 sendRedirect(java.lang.String location) 重新定向客户端的请求
7  
8  
 
3.session对象

session对象指的是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开连接为止。它是HttpSession类的实例.
序号 方 法 说 明
1 long getCreationTime() 返回SESSION创建时间
2 public String getId() 返回SESSION创建时JSP引擎为它设的惟一ID号
3 long getLastAccessedTime() 返回此SESSION里客户端最近一次请求时间
4 int getMaxInactiveInterval() 返回两次请求间隔多长时间此SESSION被取消(ms)
5 String[] getValueNames() 返回一个包含此SESSION中所有可用属性的数组
6 void invalidate() 取消SESSION,使SESSION不可用
7 boolean isNew() 返回服务器创建的一个SESSION,客户端是否已经加入
8 void removeValue(String name) 删除SESSION中指定的属性
9 void setMaxInactiveInterval() 设置两次请求间隔多长时间此SESSION被取消(ms)
10    
11    
12    
13    
14    
15    
 
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<html>
<head><title>session对象_例1</title><head>
<body><br>
session的创建时间:<%=session.getCreationTime()%>  <%=new
Date(session.getCreationTime())%><br><br>
session的Id号:<%=session.getId()%><br><br>
客户端最近一次请求时间:<%=session.getLastAccessedTime()%>  <%=new
java.sql. Time(session.getLastAccessedTime())%><br><br>
两次请求间隔多长时间此SESSION被取消(ms):<%=session.getMaxInactiveInterval()%><br><br>
是否是新创建的一个SESSION:<%=session.isNew()?"是":"否"%><br><br>
<%
session.putValue("name","霖苑编程");
session.putValue("nmber","147369");
%>
<%
for(int i=0;i<session.getValueNames().length;i++)

out.println(session.getValueNames()[i]+"="+session.getValue(session.getValueNames()[i]));
%>
<!--返回的是从格林威治时间(GMT)1970年01月01日0:00:00起到计算当时的毫秒数-->
</body>
</html>
 
 
 
4.out对象
out对象是JspWriter类的实例,是向客户端输出内容常用的对象
序号 方 法 说 明
1 void clear() 清除缓冲区的内容
2 void clearBuffer() 清除缓冲区的当前内容
3 void flush() 清空流
4 int getBufferSize() 返回缓冲区以字节数的大小,如不设缓冲区则为0
5 int getRemaining() 返回缓冲区还剩余多少可用
6 boolean isAutoFlush() 返回缓冲区满时,是自动清空还是抛出异常
7 void close() 关闭输出流
8
9
10    
11    
12    
13    
14    
15    
 
<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>out对象_例1:缓存测试</title></head>
<%@page buffer="1kb"%>
<body>
<%
for(int i=0;i<2000;i++)
out.println(i+"{"+out.getRemaining()+"}");
%><br>
缓存大小:<%=out.getBufferSize()%><br>
剩余缓存大小:<%=out.getRemaining()%><br>
自动刷新:<%=out.isAutoFlush()%><br>
<%--out.clearBuffer();--%>
<%--out.clear();--%>
<!--缺省情况下:服务端要输出到客户端的内容,不直接写到客户端,而是先写到一个输出缓冲区中.只有在下面三中情况下,才会把该缓冲区的内容输出到客户端上:

1.该JSP网页已完成信息的输出
2.输出缓冲区已满
3.JSP中调用了out.flush()或response.flushbuffer()
-->
</body>
</html>
 
 
 
5.page对象
page对象就是指向当前JSP页面本身,有点象类中的this指针,它是java.lang.Object类的实例
序号 方 法 说 明
1 class getClass 返回此Object的类
2 int hashCode() 返回此Object的hash码
3 boolean equals(Object obj) 判断此Object是否与指定的Object对象相等
4 void copy(Object obj) 把此Object拷贝到指定的Object对象中
5 Object clone() 克隆此Object对象
6 String toString() 把此Object对象转换成String类的对象
7 void notify() 唤醒一个等待的线程
8 void notifyAll() 唤醒所有等待的线程
9 void wait(int timeout) 使一个线程处于等待直到timeout结束或被唤醒
10 void wait() 使一个线程处于等待直到被唤醒
11 void enterMonitor() 对Object加锁
12 void exitMonitor() 对Object开锁
13    
14    
15    
 
6.application对象

application对象实现了用户间数据的共享,可存放全局变量。它开始于服务器的启动,直到服务器的关闭,在此期间,此对象将一直存在;这样在用户的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性的操作,都将影响到其他用户对此的访问。服务器的启动和关闭决定了application对象的生命。它是ServletContext类的实例。
序号 方 法 说 明
1 Object getAttribute(String name) 返回给定名的属性值
2 Enumeration getAttributeNames() 返回所有可用属性名的枚举
3 void setAttribute(String name,Object obj) 设定属性的属性值
4 void removeAttribute(String name) 删除一属性及其属性值
5 String getServerInfo() 返回JSP(SERVLET)引擎名及版本号
6 String getRealPath(String path) 返回一虚拟路径的真实路径
7 ServletContext getContext(String uripath)
返回指定WebApplication的application对象
8 int getMajorVersion() 返回服务器支持的Servlet API的最大版本号
9 int getMinorVersion() 返回服务器支持的Servlet API的最大版本号
10 String getMimeType(String file) 返回指定文件的MIME类型
11 URL getResource(String path) 返回指定资源(文件及目录)的URL路径
12 InputStream getResourceAsStream(String path) 返回指定资源的输入流
13 RequestDispatcher getRequestDispatcher(String uripath)
返回指定资源的RequestDispatcher对象
14 Servlet getServlet(String name) 返回指定名的Servlet
15 Enumeration getServlets() 返回所有Servlet的枚举
16 Enumeration getServletNames() 返回所有Servlet名的枚举
17 void log(String msg) 把指定消息写入Servlet的日志文件
18 void log(Exception exception,String msg) 把指定异常的栈轨迹及错误消息写入Servlet的日志文件
19 void log(String msg,Throwable throwable) 把栈轨迹及给出的Throwable异常的说明信息
写入Servlet的日志文件
20
 
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION对象_例1</title><head>
<body><br>
JSP(SERVLET)引擎名及版本号:<%=application.getServerInfo()%><br><br>
返回/application1.jsp虚拟路径的真实路径:<%=application.getRealPath("/application1.jsp")%><br><br>
服务器支持的Servlet API的大版本号:<%=application.getMajorVersion()%><br><br>
服务器支持的Servlet API的小版本号:<%=application.getMinorVersion()%><br><br>
指定资源(文件及目录)的URL路径:<%=application.getResource("/application1.jsp")%><br><br><!--可以将application1.jsp换成一个目录-->
<br><br>
<%
application.setAttribute("name","霖苑计算机编程技术培训学校");
out.println(application.getAttribute("name"));
application.removeAttribute("name");
out.println(application.getAttribute("name"));
%>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION对象_例2</title><head>
<body><br>
<!--由于application一直存在于服务器端,可以利用此特性对网页记数-->
<%
if(application.getAttribute("count")==null)
application.setAttribute("count","1");
else
application.setAttribute("count",Integer.toString(Integer.valueOf(application.getAttribute("count").toString()).intValue()+1));
%>
你是第<%=application.getAttribute("count")%>位访问者
</body>
<!--由于getAttribute()方法得到的是一个Object类型对象,用getString()方法转化为String类型-->
<!--用Integer类的valueOf()方法把得到的String转化成Integer的对象,在用intValue()方法得到int型,再加1,最后把计算的结果用Integer.toString()方法转化成setAttribute()方法所要求的String类型-->
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION对象_例3</title><head>
<body><br>
<!--由于application一直存在于服务器端,可以利用此特性对网页记数-->
<%
String
str=application.getAttribute("count").toString();//getAttribute("count")返回的是Object类型
int i=0;
if(str==null)
application.setAttribute("count","1");
else
i=Integer.parseInt(str); //out.println(i);
application.setAttribute("count",++i+"");
%>
你是第<%=application.getAttribute("count")%>位访问者
</body>
</html>
 
7.exception对象

exception对象是一个例外对象,当一个页面在运行过程中发生了例外,就产生这个对象。如果一个JSP页面要应用此对象,就必须把isErrorPage设为true,否则无法编译。他实际上是java.lang.Throwable的对象
序号 方 法 说 明
1 String getMessage() 返回描述异常的消息
2 String toString() 返回关于异常的简短描述消息
3 void printStackTrace() 显示异常及其栈轨迹
4 Throwable FillInStackTrace() 重写异常的执行栈轨迹
5
 
8.pageContext对象

pageContext对象提供了对JSP页面内所有的对象及名字空间的访问,也就是说他可以访问到本页所在的SESSION,也可以取本页面所在的application的某一属性值,他相当于页面中所有功能的集大成者,它的本
类名也叫pageContext。
序号 方 法 说 明
1 JspWriter getOut() 返回当前客户端响应被使用的JspWriter流(out)
2 HttpSession getSession() 返回当前页中的HttpSession对象(session)
3 Object getPage() 返回当前页的Object对象(page)
4 ServletRequest getRequest() 返回当前页的ServletRequest对象(request)
5 ServletResponse getResponse() 返回当前页的ServletResponse对象(response)
6 Exception getException() 返回当前页的Exception对象(exception)
7 ServletConfig getServletConfig() 返回当前页的ServletConfig对象(config)
8 ServletContext getServletContext() 返回当前页的ServletContext对象(application)
9 void setAttribute(String name,Object attribute) 设置属性及属性值
10 void setAttribute(String name,Object obj,int scope) 在指定范围内设置属性及属性值
11 public Object getAttribute(String name) 取属性的值
12 Object getAttribute(String name,int scope) 在指定范围内取属性的值
13 public Object findAttribute(String name) 寻找一属性,返回起属性值或NULL
14 void removeAttribute(String name) 删除某属性
15 void removeAttribute(String name,int scope) 在指定范围删除某属性
16 int getAttributeScope(String name) 返回某属性的作用范围
17 Enumeration getAttributeNamesInScope(int scope) 返回指定范围内可用的属性名枚举
18 void release() 释放pageContext所占用的资源
19 void forward(String relativeUrlPath) 使当前页面重导到另一页面
20 void include(String relativeUrlPath) 在当前位置包含另一文件
21

 
<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>pageContext对象_例1</title></head>
<body><br>
<%
request.setAttribute("name","霖苑编程");
session.setAttribute("name","霖苑计算机编程技术培训");
//session.putValue("name","计算机编程");
application.setAttribute("name","培训");
%>
request设定的值:<%=pageContext.getRequest().getAttribute("name")%><br>
session设定的值:<%=pageContext.getSession().getAttribute("name")%><br>
application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>
范围1内的值:<%=pageContext.getAttribute("name",1)%><br>
范围2内的值:<%=pageContext.getAttribute("name",2)%><br>
范围3内的值:<%=pageContext.getAttribute("name",3)%><br>
范围4内的值:<%=pageContext.getAttribute("name",4)%><br>
<!--从最小的范围page开始,然后是reques、session以及application-->
<%pageContext.removeAttribute("name",3);%>
pageContext修改后的session设定的值:<%=session.getValue("name")%><br>
<%pageContext.setAttribute("name","应用技术培训",4);%>
pageContext修改后的application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>
值的查找:<%=pageContext.findAttribute("name")%><br>
属性name的范围:<%=pageContext.getAttributesScope("name")%><br>
</body></html>
 
 
9.config对象

config对象是在一个Servlet初始化时,JSP引擎向它传递信息用的,此信息包括Servlet初始化时所要用到的参数(通过属性名和属性值构成)以及服务器的有关信息(通过传递一个ServletContext对象)
序号 方 法 说 明
1 ServletContext getServletContext() 返回含有服务器相关信息的ServletContext对象
2 String getInitParameter(String name) 返回初始化参数的值
3 Enumeration getInitParameterNames() 返回Servlet初始化所需所有参数的枚举
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值