您的位置:首页 > 其它

ServletContext基础(还在更新中)

2016-04-08 13:08 369 查看
Web 容器在启动时会加载每一个web 应用程序。

1. ServletContext (Servlet 上下文对象):每个web工程都有自己的ServletContext对象。

范围:应用程序范围。可以被Web 应用程序的所有jsp页面和Servlet 访问。

2. ServletConfig(Servlet 配置对象):每个Servlet 都有自己的ServletConfig对象。

范围:Servlet 范围。

3.

ServletContext 对象是在WEB 应用程序装载时初始化的。

得到ServletContext对象的方法:

1. ServletContext context = getServletContext();

2. ServletContext context = getServletConfig().getServletContext();

得到ServletContext 对象参数的方法:

1. public String getInitParameter(String name); name 为指定的参数值,若不存在,返回null。

2. public Enumeration getInitParameterNames(); 返回所有的参数。

得到ServletContext对象参数值得方法:

ServletContext context = getServletContext();

String value = context.getInitParameter(“name”);

通过ServletContext对象获得资源的方法:

1. public URL getResource(String path);必须以‘/’开头,它相对于Web应用程序的根目录。

2. Public InputStream getResourceAsStream(String path);相当于getResource(path).openStream()。

3. public String getRealPath(String path);返回给定路径的绝对路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: