📄 pagecontextimpl.java
字号:
case PAGE_SCOPE:
attributes.put(name, o);
break;
case REQUEST_SCOPE:
request.setAttribute(name, o);
break;
case SESSION_SCOPE:
if (session == null)
throw new IllegalArgumentException("can't access SESSION_SCOPE without an HttpSession");
else
session.setAttribute(name, o);
break;
case APPLICATION_SCOPE:
context.setAttribute(name, o);
break;
default:
}
}
public void removeAttribute(String name, int scope) {
switch (scope) {
case PAGE_SCOPE:
attributes.remove(name);
break;
case REQUEST_SCOPE:
request.removeAttribute(name);
case SESSION_SCOPE:
if (session == null)
throw new IllegalArgumentException("can't access SESSION_SCOPE without an HttpSession");
else
session.removeAttribute(name);
// was:
// session.removeValue(name);
// REVISIT Verify this is correct - akv
break;
case APPLICATION_SCOPE:
context.removeAttribute(name);
break;
default:
}
}
public int getAttributesScope(String name) {
if (attributes.get(name) != null) return PAGE_SCOPE;
if (request.getAttribute(name) != null)
return REQUEST_SCOPE;
if (session != null) {
if (session.getAttribute(name) != null)
return SESSION_SCOPE;
}
if (context.getAttribute(name) != null) return APPLICATION_SCOPE;
return 0;
}
public Object findAttribute(String name) {
Object o = attributes.get(name);
if (o != null)
return o;
o = request.getAttribute(name);
if (o != null)
return o;
if (session != null) {
o = session.getAttribute(name);
if (o != null)
return o;
}
return context.getAttribute(name);
}
public Enumeration getAttributeNamesInScope(int scope) {
switch (scope) {
case PAGE_SCOPE:
return attributes.keys();
case REQUEST_SCOPE:
return request.getAttributeNames();
case SESSION_SCOPE:
if (session != null) {
return session.getAttributeNames();
} else
throw new IllegalArgumentException("can't access SESSION_SCOPE without an HttpSession");
case APPLICATION_SCOPE:
return context.getAttributeNames();
default: return new Enumeration() { // empty enumeration
public boolean hasMoreElements() { return false; }
public Object nextElement() { throw new NoSuchElementException(); }
};
}
}
public void removeAttribute(String name) {
try {
removeAttribute(name, PAGE_SCOPE);
removeAttribute(name, REQUEST_SCOPE);
removeAttribute(name, SESSION_SCOPE);
removeAttribute(name, APPLICATION_SCOPE);
} catch (Exception ex) {
// we remove as much as we can, and
// simply ignore possible exceptions
}
}
public JspWriter getOut() {
return out;
}
public HttpSession getSession() { return session; }
public Servlet getServlet() { return servlet; }
public ServletConfig getServletConfig() { return config; }
public ServletContext getServletContext() {
return config.getServletContext();
}
public ServletRequest getRequest() { return request; }
public ServletResponse getResponse() { return response; }
public Exception getException() { return (Exception)request.getAttribute(EXCEPTION); }
public Object getPage() { return servlet; }
private final String getAbsolutePathRelativeToContext(String relativeUrlPath) {
String path = relativeUrlPath;
if (!path.startsWith("/")) {
String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
if (uri == null)
uri = ((HttpServletRequest) request).getServletPath();
String baseURI = uri.substring(0, uri.lastIndexOf('/'));
path = baseURI+'/'+path;
}
return path;
}
public void include(String relativeUrlPath)
throws ServletException, IOException
{
String path = getAbsolutePathRelativeToContext(relativeUrlPath);
out.flush();
context.getRequestDispatcher(path).include(request, response);
}
public void forward(String relativeUrlPath)
throws ServletException, IOException
{
String path = getAbsolutePathRelativeToContext(relativeUrlPath);
if( out!=null )
out.clearBuffer();
context.getRequestDispatcher(path).forward(request, response);
}
Stack writerStack = new Stack();
public BodyContent pushBody() {
JspWriter previous = out;
writerStack.push(out);
out = new BodyContentImpl(previous);
return (BodyContent) out;
}
public JspWriter popBody() {
out = (JspWriter) writerStack.pop();
return out;
}
public void handlePageException(Exception e)
throws IOException, ServletException {
// set the request attribute with the exception.
request.setAttribute("javax.servlet.jsp.jspException", e);
if (errorPageURL != null && !errorPageURL.equals("")) {
try {
forward(errorPageURL);
} catch (IllegalStateException ise) {
include(errorPageURL);
}
} // Otherwise throw the exception wrapped inside a ServletException.
else {
// Set the exception as the root cause in the ServletException
// to get a stack trace for the real problem
if( e instanceof IOException )
throw (IOException)e;
if( e instanceof ServletException )
throw (ServletException) e;
// e.printStackTrace();
throw new ServletException(e);
}
}
protected JspWriter _createOut(int bufferSize, boolean autoFlush)
throws IOException, IllegalArgumentException
{
try {
return new JspWriterImpl(response, bufferSize, autoFlush);
} catch( Throwable t ) {
t.printStackTrace();
return null;
}
}
/*
* fields
*/
// per Servlet state
protected Servlet servlet;
protected ServletConfig config;
protected ServletContext context;
protected JspFactory factory;
protected boolean needsSession;
protected String errorPageURL;
protected boolean autoFlush;
protected int bufferSize;
// page scope attributes
protected transient Hashtable attributes = new Hashtable(16);
// per request state
protected transient ServletRequest request;
protected transient ServletResponse response;
protected transient Object page;
protected transient HttpSession session;
// initial output stream
protected transient JspWriter out;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -