📄 pi3pagecontext.java
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/jsp/core/Pi3PageContext.java,v $
* $Date: 2003/05/13 18:42:20 $
*
Description:
Implementation of jsp API PageContext interface.
\*____________________________________________________________________________*/
package org.pi3.jsp.core;
import java.io.IOException;
import java.lang.IllegalArgumentException;
import java.lang.reflect.Array;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import org.pi3.servlet.core.Pi3HttpServletRequest;
import org.pi3.servlet.core.Pi3HttpSession;
/**
* <p>
* A PageContext instance provides access to all the namespaces associated with
* a JSP page, provides access to several page attributes, as well as a layer above the
* implementation details.
* <p>
* An instance of an implementation dependent subclass of this abstract base
* class is created by a JSP implementation class at the begining of it's
* <code> _jspService() </code> method via an implementation default
* <code> JspFactory </code>, as follows:
*</p>
*<p>
*<p>
*<code>
*<pre>
* public class foo implements Servlet {
*
* // ...
*
*public void _jspService(HttpServletRequest request,
* HttpServletResponse response)
* throws IOException, ServletException {
*
* JspFactory factory = JspFactory.getDefaultFactory();
* PageContext pageContext = factory.getPageContext(
this,
request,
response,
null, // errorPageURL
false, // needsSession
JspWriter.DEFAULT_BUFFER,
true // autoFlush
);
*
* // initialize implicit variables for scripting env ...
*
* HttpSession session = pageContext.getSession();
* JspWriter out = pageContext.getOut();
* Object page = this;
*
* try {
* // body of translated JSP here ...
* } catch (Exception e) {
* out.clear();
* pageContext.handlePageException(e);
* } finally {
* out.close();
* factory.releasePageContext(pageContext);
* }
*}
*</pre>
* </code>
* </p>
* <p>
* The <code> PageContext </code> class is an abstract class, designed to be
* extended to provide implementation dependent implementations thereof, by
* conformant JSP engine runtime environments. A PageContext instance is
* obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().
* </p>
* <p>
* The PageContext provides a number of facilities to the page/component author and
* page implementor, including:
* <td>
* <li>a single API to manage the various scoped namespaces
* <li>a number of convenience API's to access various public objects
* <li>a mechanism to obtain the JspWriter for output
* <li>a mechanism to manage session usage by the page
* <li>a mechanism to expose page directive attributes to the scripting environment
* <li>mechanisms to forward or include the current request to other active components in the application
* <li>a mechanism to handle errorpage exception processing
* </td>
* </p>
*
*/
public class Pi3PageContext extends PageContext {
private Servlet srl;
private ServletRequest req;
private ServletResponse res;
private String errPage;
private boolean nSess = true;
private JspWriter out;
private Hashtable attr = new Hashtable();
/**
* <p>
* The initialize method is called to initialize an uninitialized PageContext
* so that it may be used by a JSP Implementation class to service an
* incoming request and response wihtin it's _jspService() method.
* </p>
* <p>
* This method is typically called from JspFactory.getPageContext() in
* order to initialize state.
* </p>
*
* @param servlet The Servlet that is associated with this PageContext
* @param request The currently pending request for this Servlet
* @param response The currently pending response for this Servlet
* @param errorPageURL The value of the errorpage attribute from the page directive or null
* @param needsSession The value of the session attribute from the page directive
* @param bufferSize The value of the buffer attribute from the page directive
* @param autoFlush The value of the autoflush attribute from the page directive
*
* @throws IOException during creation of JspWriter
* @throws IllegalStateException if out not correctly initialized
*/
public void initialize(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) throws IOException, IllegalStateException, IllegalArgumentException {
srl = servlet;
req = request;
res = response;
errPage = errorPageURL;
nSess = needsSession;
out = new Pi3JspWriter(res.getOutputStream(), bufferSize, autoFlush);
}
/**
* <p>
* This method shall "reset" the internal state of a PageContext, releasing
* all internal references, and preparing the PageContext for potential
* reuse by a later invocation of initialize(). This method is typically
* called from JspFactory.releasePageContext().
* </p>
*
* <p> subclasses shall envelope this method </p>
*
*/
public void release() {
srl = null;
req = null;
res = null;
errPage = null;
nSess = true;
out = null;
attr = null;
}
/**
* register the name and object specified with page scope semantics
*
* @throws NullPointerException if the name or object is null
*/
public void setAttribute(String name, Object attribute) {
attr.put(name, attribute);
};
/**
* register the name and object specified with appropriate scope semantics
*
* @param name the name of the attribute to set
* @param o the object to associate with the name
* @param scope the scope with which to associate the name/object
*
* @throws NullPointerException if the name or object is null
* @throws IllegalArgumentException if the scope is invalid
*
*/
public void setAttribute(String name, Object o, int scope) {
switch (scope) {
case 1 /* PAGE_SCOPE */ :
attr.put(name, o); break;
case 2 /* REQUEST_SCOPE */ :
req.setAttribute(name, o); break;
case 3 /* SESSION_SCOPE */ :
if (getSession() != null) getSession().putValue(name, o); break;
case 4 /* APPLICATION_SCOPE */ :
getServletContext().setAttribute(name, o); break;
default: throw new IllegalArgumentException("Invalid scope.");
};
};
/**
* <p>return the object associated with the name in the page scope or null </p>
*
* @param name the name of the attribute to get
*
* @throws NullPointerException if the name is null
* @throws IllegalArgumentException if the scope is invalid
*/
public Object getAttribute(String name) {
return attr.get(name);
};
/**
* <p>return the object associated with the name in the specifed scope or null </p>
*
* @param name the name of the attribute to set
* @param scope the scope with which to associate the name/object
*
* @throws NullPointerException if the name is null
* @throws IllegalArgumentException if the scope is invalid
*/
public Object getAttribute(String name, int scope) {
switch (scope) {
case 1 /* PAGE_SCOPE */ :
return attr.get(name);
case 2 /* REQUEST_SCOPE */ :
return req.getAttribute(name);
case 3 /* SESSION_SCOPE */ :
return (getSession() != null) ? getSession().getValue(name) : null;
case 4 /* APPLICATION_SCOPE */ :
return getServletContext().getAttribute(name);
default: throw new IllegalArgumentException("Invalid scope.");
};
};
/**
* <p>
* Searches for the named attribute in page, request, session (if valid),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -