jsp对象以及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 )来取用它的方法和属性。

posted @ 2006-02-12 11:35 活在JAVA岛的日子 阅读(320) | 评论 (0) 编辑  收藏
 
  • 为什么GenericServlet在init(ServletConfig config)基础上增加了一个init()方法?

    init()方法被GenericServlet.init(ServletConfig config)方法调用。
    init()方法方便了开发人员定制Servlet的初始化,而无须去维护ServletConfig对象的存储工作。
    重写GenericServlet.init(ServletConfig config)必须要显示的调用super.init(config)方法。

  • ServletContext.getContect(java.lang.String uripath)的作用是什么?

    返回同一Server中指定的path对应的ServletContext对象,通过该对象可以实现与Server中的其他Context打交道。
    uripath必须是以"/"开始(该路径的含义是相对于整个Servlet文档的根路径,而不是当前ServletContext的根路径)。

  • Servlet生命周期是什么?

    一般的Servlet(GenericServlet,即与协议无关的Servlet)的生命周期:init() --> GenericServlet.service(ServletRequest req, ServletResponse res) --> destroy.
    HttpServlet的生命周期: init() --> GenericServlet.service(ServletRequest req, ServletResponse res)---> service(HttpServletRequest req, HttpServletResponse resp) --> doXXXX()-->destroy.

  • 有没有必要重写GenericServlet.service()方法?

    对于HttpServlet来说没有必要。只需要重写它的doXXXX()方法就可以了。HttpServlet中service()方法会自动的根据用户请求类型把请求转发给相应的doXXXX()方法(例如doGet()方法)。

  • ServletRequest.getReader()和ServletRequest.getInputStream()如何使用?

    注意两个方法不能同时使用。

  • ServletRequest.getRealPath(String path)方法已经不推荐使用。

    请使用ServletContext.getRealPath(String path)方法。

  • ServletResponse缺省的字符集(charset)是什么?

    ServletResponse缺省的字符集(charset)是ISO-8859-1,可以通过setContentType(java.lang.String)方法改变新的字符集。
    例如:setContentType("text/html; charset=Shift_JIS").
    关于字符集信息,可以浏览RFC 2045

  • HttpServletRequest.getRequestURI()和HttpServletRequest.getRequestURL()区别是什么?

    request.getRequestURI() 返回值类似:/xuejava/requestdemo.jsp
    request.getRequestURL() 返回值类似:http://localhost:8080/xuejava/requestdemo.jsp

  • HttpServletRequest.encodeURL()和HttpServletRequest.encodeRedirectURL(()区别是什么?为什么要有两个不同的方法呢?

    当用URL-rewriting方式来管理Session的时候,需要用到以上的两个方法。
    两个方法的不同点是:两个方法确定是否需要包含session ID的逻辑不同。
    在调用HttpServletResponse.sendRedirect前,应该先调用encodeRedirectURL()方法,否则可能会丢失Sesssion信息。 ...

  • 如何使你的Servlet或者JSP实现Single Thread Model?

    对于Servlet实现javax.single.SingleThreadModel接口。
    对于JSP,在Page Directive中写如下的语句

  • JSP Tag 和 JSP XML-based Tag

    ...

  • 如何把某一个JSP Page定义成为Error Page?为什么要这样做?

    实现方法:
    为什么? 因为需要获取Exception 对象(缺省情况下,在JSP Page中是不能直接使用“隐含对象” exception的)。

  • JSP Page的执行顺序是如何的?

    JSP Page的执行顺序如下:

    • JSP Page Translation. JSP Page --> Servlet source code.
    • JSP Page Compilation. Servlet source code --> Servlet class.
    • Load Class(First time or the server restarted)
    • Create instance(可能会很多次,如果JSP Page中声明了 )
    • Call jspInit method(一般的JSP Page都没有重写这个方法,重写需要在声明语句段中)。
    • Call _jspService method(类似与一般HttpServlet的doGet和doPost方法,但是可以同时用来处理Post和Getq请求)。
    • Call jspDestroy method(Server在卸载Servet的时候,例如当Servlet很久没有使用的情况)。

       

  • JSP Page中有哪些隐含对象(Implicity Object)?各自的类型和作用是什么?

     

    • request --
    • reponse --
    • session --
    • application --
    • out --
    • page --
    • pagecontext --
    • exception -- 只有在当前JSP Page为Error Page的时候才有效。
    • config --

       

  • 和 <@ include file="/foo/foo.jsp" %>的区别是什么?


    <@ include ... -- Page translation time.

  • Servlets/JSP Container(Engine)有几种运行方式?

     

    • Standalone
      Tomcat standalone mode
    • In-process
      Tomcat running inside Apache Web Server.
    • Out-of-process
      Apache + mod_jk + Tomcat

       

  • Servlet,Servlet开发人员,Servlet API, Servlet Container的关系是什么?
    Servlet,Servlet开发人员 --->Servlet API --> Servlet Container
  • The parts of an HTTP message
     Message part Description The initial line: Specifies the purpose of the request or response message 例子:GET /reports/sales/index.html HTTP/1.0 The header section:Specifies the meta-information, such as size, type, and encoding, about the content of the message A blank line: An optional message body: The main content of the request or response message
    下面是一个Response的例子:
     

     HTTP/1.0 200 OK Date: Tue, 01 Dec 2001 23:59:59 GMT Content-Type: text/html Content-Length: 52

    Hello, John!

     

  • HTTP规范中定义了哪些方法?各自有什么用途?
    • GET
    • HEAD
    • POST
      从 Http 1.1规范开始,增加了以下的方法:
    • PUT
    • OPTIONS
    • TRACE
    • DELETE
    • CONNECT
  • ServetRequest中为什么要定义:getContentType(),getContentLength()方法。
    根据HTTP协议规范,Request 和 Response一样也有这些必不可少的内容!
    所以需要首先了解 HTTP Message的概念和其内容的格式,这些东西对于Request和Reponse是一样的。
    对于GET方式发送的请求,其内容类型为:null
    对于POST方式发送的请求,其内容类型为:application/x-www-form-urlencoded

    POST方式发送请求的内容类似于:username=xuejava.
  • RequestDispatcher.forward()和HttpServletResponse.sendRedirect()的区别是什么?
    RequestDispatcher.forward()是在服务器端运行;HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成。
    所以RequestDispatcher.forward()对于浏览器来说是“透明的”;而HttpServletResponse.sendRedirect()则不是。
    另外,还要注意RequestDispatcher.forward()在调用的时候Response不能已经Commit了(Response.isCommitted())。
  • ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)的区别是什么?为什么?
    ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径;而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。
    因为ServletRequest具有相对路径的概念;而ServletContext对象无次概念。
  • 如何把请求转移到另外一个Web App中的某个地址?
    ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)只能把请求转移到同一个Web App中的地址。
    如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
    1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).
    2. 调用ServletContext.getRequestDispatcher(String url)方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值