JSP隐含对象介绍
2015-12-23 21:52
736 查看
jsp中一共有9个隐含对象,它们是:
1.application,它是一个实现了ServletContext接口的类的实例,下面是这个接口的代码:
2.config,它是一个实现了ServletConfig接口的类的实例,ServletConfig接口的代码如下:
3.out,它是一个继承了JspWriter抽象类的类的实例,JspWriter抽象类的代码如下:
4.pageContex,它是一个继承了PageContext抽象类的类的实例,PageContext抽象类的代码如下:
5.page,类似this指针,是object类的对象,开发中不常用
6.response,它是一个实现了ServletResponse接口的类(其实就是HttpServletResponse)的实例,ServletResponse接口的代码如下:
7.session,它是一个实现了HttpSession接口的类的实例,HttpSession接口的代码如下:
8.exception,它用来处理页面出现的异常,是java.lang.Throwable类的实例
9.request,它是一个实现了ServletRequest接口的类(其实就是HttpServletRequest)的实例,ServletRequest接口的代码如下:
request对象是最常用的内置对象之一,下面介绍一下它的几个常用的方法
假定你的工程名称为projects,你在浏览器中输入请求路径:
http://127.0.0.1:8080/projects/pages/newForm.jsp
(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
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; }
4.pageContex,它是一个继承了PageContext抽象类的类的实例,PageContext抽象类的代码如下:
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接口的类(其实就是HttpServletRequest)的实例,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(); }
request对象是最常用的内置对象之一,下面介绍一下它的几个常用的方法
假定你的工程名称为projects,你在浏览器中输入请求路径:
http://127.0.0.1:8080/projects/pages/newForm.jsp
(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
相关文章推荐
- 每次都觉得很神奇的JS
- json和jsonp
- JS小知识点----基本包装类型和引用类型
- canvas+js绘制饼状统计图
- JS实现表单输入Enter键转换焦点框
- cJSON源码学习总结
- javascript生成表格增删改查
- JS链接页面
- javascript 表格增删改
- (mark 笔记)json数据解析
- BZOJ 1015: [JSOI2008]星球大战starwar 并查集
- js 动态生成宽度固定的table控件
- Extjs Widget
- jstl中格式化时间戳
- JavaScript中concat不起作用
- 測试JSON RPC远程调用(JSONclient)
- 轻松学习JavaScript二十五:DOM编程学习之操作CSS样式(二)
- browserify.js 的模块加载
- 常用JS验证函数总结
- js 同步和异步共存的示例