jsp中一共有9个隐含对象,它们是:
1.application,它是一个实现了ServletContext接口的类的实例,下面是这个接口的代码:
public interface ServletContext
{
public abstract String getContextPath();
public abstract ServletContext getContext(String s);
public abstract int getMajorVersion();
public abstract int getMinorVersion();
public abstract String getMimeType(String s);
public abstract Set getResourcePaths(String s);
public abstract URL getResource(String s)
throws MalformedURLException;
public abstract InputStream getResourceAsStream(String s);
public abstract RequestDispatcher getRequestDispatcher(String s);
public abstract RequestDispatcher getNamedDispatcher(String s);
/**
* @deprecated Method getServlet is deprecated
*/
public abstract Servlet getServlet(String s)
throws ServletException;
/**
* @deprecated Method getServlets is deprecated
*/
public abstract Enumeration getServlets();
/**
* @deprecated Method getServletNames is deprecated
*/
public abstract Enumeration getServletNames();
public abstract void log(String s);
/**
* @deprecated Method log is deprecated
*/
public abstract void log(Exception exception, String s);
public abstract void log(String s, Throwable throwable);
public abstract String getRealPath(String s);
public abstract String getServerInfo();
public abstract String getInitParameter(String s);
public abstract Enumeration getInitParameterNames();
public abstract Object getAttribute(String s);
public abstract Enumeration getAttributeNames();
public abstract void setAttribute(String s, Object obj);
public abstract void removeAttribute(String s);
public abstract String getServletContextName();
}
2.config,它是一个实现了ServletConfig接口的类的实例,ServletConfig接口的代码如下:
public interface ServletConfig
{
public abstract String getServletName();
public abstract ServletContext getServletContext();
public abstract String getInitParameter(String s);
public abstract Enumeration getInitParameterNames();
}
3.out,它是一个继承了JspWriter抽象类的类的实例,JspWriter抽象类的代码如下:
public abstract class JspWriter extends Writer
{
protected JspWriter(int bufferSize, boolean autoFlush)
{
this.bufferSize = bufferSize;
this.autoFlush = autoFlush;
}
public abstract void newLine()
throws IOException;
public abstract void print(boolean flag)
throws IOException;
public abstract void print(char c)
throws IOException;
public abstract void print(int i)
throws IOException;
public abstract void print(long l)
throws IOException;
public abstract void print(float f)
throws IOException;
public abstract void print(double d)
throws IOException;
public abstract void print(char ac[])
throws IOException;
public abstract void print(String s)
throws IOException;
public abstract void print(Object obj)
throws IOException;
public abstract void println()
throws IOException;
public abstract void println(boolean flag)
throws IOException;
public abstract void println(char c)
throws IOException;
public abstract void println(int i)
throws IOException;
public abstract void println(long l)
throws IOException;
public abstract void println(float f)
throws IOException;
public abstract void println(double d)
throws IOException;
public abstract void println(char ac[])
throws IOException;
public abstract void println(String s)
throws IOException;
public abstract void println(Object obj)
throws IOException;
public abstract void clear()
throws IOException;
public abstract void clearBuffer()
throws IOException;
public abstract void flush()
throws IOException;
public abstract void close()
throws IOException;
public int getBufferSize()
{
return bufferSize;
}
public abstract int getRemaining();
public boolean isAutoFlush()
{
return autoFlush;
}
public static final int NO_BUFFER = 0;
public static final int DEFAULT_BUFFER = -1;
public static final int UNBOUNDED_BUFFER = -2;
protected int bufferSize;
protected boolean autoFlush;
}
public abstract class PageContext extends JspContext
{
public PageContext()
{
}
public abstract void initialize(Servlet servlet, ServletRequest servletrequest, ServletResponse servletresponse, String s, boolean flag, int i, boolean flag1)
throws IOException, IllegalStateException, IllegalArgumentException;
public abstract void release();
public abstract HttpSession getSession();
public abstract Object getPage();
public abstract ServletRequest getRequest();
public abstract ServletResponse getResponse();
public abstract Exception getException();
public abstract ServletConfig getServletConfig();
public abstract ServletContext getServletContext();
public abstract void forward(String s)
throws ServletException, IOException;
public abstract void include(String s)
throws ServletException, IOException;
public abstract void include(String s, boolean flag)
throws ServletException, IOException;
public abstract void handlePageException(Exception exception)
throws ServletException, IOException;
public abstract void handlePageException(Throwable throwable)
throws ServletException, IOException;
public BodyContent pushBody()
{
return null;
}
public ErrorData getErrorData()
{
return new ErrorData((Throwable)getRequest().getAttribute("javax.servlet.error.exception"), ((Integer)getRequest().getAttribute("javax.servlet.error.status_code")).intValue(), (String)getRequest().getAttribute("javax.servlet.error.request_uri"), (String)getRequest().getAttribute("javax.servlet.error.servlet_name"));
}
public static final int PAGE_SCOPE = 1;
public static final int REQUEST_SCOPE = 2;
public static final int SESSION_SCOPE = 3;
public static final int APPLICATION_SCOPE = 4;
public static final String PAGE = "javax.servlet.jsp.jspPage";
public static final String PAGECONTEXT = "javax.servlet.jsp.jspPageContext";
public static final String REQUEST = "javax.servlet.jsp.jspRequest";
public static final String RESPONSE = "javax.servlet.jsp.jspResponse";
public static final String CONFIG = "javax.servlet.jsp.jspConfig";
public static final String SESSION = "javax.servlet.jsp.jspSession";
public static final String OUT = "javax.servlet.jsp.jspOut";
public static final String APPLICATION = "javax.servlet.jsp.jspApplication";
public static final String EXCEPTION = "javax.servlet.jsp.jspException";
}
5.page,类似this指针,是object类的对象,开发中不常用
6.response,它是一个实现了ServletResponse接口的类(其实就是
HttpServletResponse)的实例,
ServletResponse接口的代码如下:
public interface ServletResponse
{
public abstract String getCharacterEncoding();
public abstract String getContentType();
public abstract ServletOutputStream getOutputStream()
throws IOException;
public abstract PrintWriter getWriter()
throws IOException;
public abstract void setCharacterEncoding(String s);
public abstract void setContentLength(int i);
public abstract void setContentType(String s);
public abstract void setBufferSize(int i);
public abstract int getBufferSize();
public abstract void flushBuffer()
throws IOException;
public abstract void resetBuffer();
public abstract boolean isCommitted();
public abstract void reset();
public abstract void setLocale(Locale locale);
public abstract Locale getLocale();
}
7.session,它是一个实现了HttpSession接口的类的实例,HttpSession接口的代码如下:
public interface HttpSession
{
public abstract long getCreationTime();
public abstract String getId();
public abstract long getLastAccessedTime();
public abstract ServletContext getServletContext();
public abstract void setMaxInactiveInterval(int i);
public abstract int getMaxInactiveInterval();
/**
* @deprecated Method getSessionContext is deprecated
*/
public abstract HttpSessionContext getSessionContext();
public abstract Object getAttribute(String s);
/**
* @deprecated Method getValue is deprecated
*/
public abstract Object getValue(String s);
public abstract Enumeration getAttributeNames();
/**
* @deprecated Method getValueNames is deprecated
*/
public abstract String[] getValueNames();
public abstract void setAttribute(String s, Object obj);
/**
* @deprecated Method putValue is deprecated
*/
public abstract void putValue(String s, Object obj);
public abstract void removeAttribute(String s);
/**
* @deprecated Method removeValue is deprecated
*/
public abstract void removeValue(String s);
public abstract void invalidate();
public abstract boolean isNew();
}
8.exception,它用来处理页面出现的异常,是java.lang.Throwable类的实例
9.request,它是一个实现了ServletRequest接口的类(其实就是Http
ServletRequest)的实例,ServletRequest接口的代码如下:
public interface ServletRequest
{
public abstract Object getAttribute(String s);
public abstract Enumeration getAttributeNames();
public abstract String getCharacterEncoding();
public abstract void setCharacterEncoding(String s)
throws UnsupportedEncodingException;
public abstract int getContentLength();
public abstract String getContentType();
public abstract ServletInputStream getInputStream()
throws IOException;
public abstract String getParameter(String s);
public abstract Enumeration getParameterNames();
public abstract String[] getParameterValues(String s);
public abstract Map getParameterMap();
public abstract String getProtocol();
public abstract String getScheme();
public abstract String getServerName();
public abstract int getServerPort();
public abstract BufferedReader getReader()
throws IOException;
public abstract String getRemoteAddr();
public abstract String getRemoteHost();
public abstract void setAttribute(String s, Object obj);
public abstract void removeAttribute(String s);
public abstract Locale getLocale();
public abstract Enumeration getLocales();
public abstract boolean isSecure();
public abstract RequestDispatcher getRequestDispatcher(String s);
/**
* @deprecated Method getRealPath is deprecated
*/
public abstract String getRealPath(String s);
public abstract int getRemotePort();
public abstract String getLocalName();
public abstract String getLocalAddr();
public abstract int getLocalPort();
}
假定你的
工程名称为projects,你在浏览器中输入请求路径:
(1)request.getContextPath(),得到当前应用的根目录,输出/projects
(2)request.getServletPath(),输出/pages/newForm.jsp
(3)request.getRequestURI(),输出/projects/pages/newForm.jsp
(4)request.getRequestURL(),输出http://127.0.0.1:8080/projects/pages/newForm.jsp
(5)request.getRealPath("/"),输出C:\Tomcat5.0\webapps\projects