servletconfig和servletcontext学习
servletconfig
java.lang.String getInitParameter(java.lang.String name) //根据参数名获取参数值
java.util.Enumeration getInitParameterNames() //获取所有参数
servletcontext
java.lang.String getContextPath() --得到当前web应用的路径
java.lang.String getInitParameter(java.lang.String name) --得到web应用的初始化参数
java.util.Enumeration getInitParameterNames()
void setAttribute(java.lang.String name, java.lang.Object object) --域对象有关的方法
java.lang.Object getAttribute(java.lang.String name)
void removeAttribute(java.lang.String name)
RequestDispatcher getRequestDispatcher(java.lang.String path) --转发(类似于重定向)
java.lang.String getRealPath(java.lang.String path) --得到web应用的资源文件
java.io.InputStream re(java.lang.String path)
区别
一个是把servlet配置封装到servlet中(servletconfig)
一个web.xml整个文件信息封装(servletcontext)
一个web项目可以存在多个servletconfig但是只能存在一个servletcontext
servletconfig应用场景配置信息
列如:读文件的路径改变时需要频繁修改信息
解决方案:把配置文件写入到servlet中
<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<init-param>
<param-name>path</param-name>//这是参数名
<param-value>c:/a.txt</param-value>//可以在这里修改路径
</init-param>
</servlet>
调用
写一个servlet(Demo)继承httpservlet
this.getServletConfig("path");//this代表了当前类(Demo) getsetvletconfig是sun公司封装config方法 返回的是value
servletcontext
servletContext被sun公司封装在servleconfig中所以调用的时候为servletcontext contest = config.getServletContext();或者servletcontext contest =this.servletcontext
Sun公司设计
1)创建ServletContext对象 ServletContext context = new ServletContext()
2)创建ServletConfig对象
ServetConfig config = new ServletConfig();
config.setServletContxt(context);
class ServletConfig{
ServletContext context;
public ServletContext getServletContxt(){
return contxt;
}
}
public void init( ServletConfig config ){
得到ServletConfig对象
从ServletConfig对象中得到ServletContext对象
SerlvetContext context = config.getServletContext();
}
获取路径
String str = this.getServletContext().getContextPath();//str为 /+当前项目名称(应用与请求重定向资源路径中)
contextpath应用一(转发)
this.getServletContext().getRequestDispatcherType("path").forward(request,response);
应用与域对象保存分享数据(但是会覆盖原来的数据)
加载外部资源
String path = this.getServletContext().getRealPath("资源路径");// "/WEB-INF/class/db.properties"
加载外部资源比较
例如src下有一个外部文件db.properties
java项目 获取路径为"./src/db.properties"
web项目 用函数getRealPath()或者getResourseAsStream();
为什么不能用 " . "获取
因为 . 代表的是java当前运行的目录(servlet中在tomcat服务器的bin目录下面)所以不建议
servletconfig和servletcontext学习的更多相关文章
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- JavaWeb学习笔记:ServletConfig()和ServletContext()
ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...
- Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext
Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- 谈谈 ServletConfig 和 ServletContext
目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...
- ServletConfig与ServletContext
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
- ServletConfig和ServletContext
ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- ServletConfig、ServletContext 的应用
一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...
随机推荐
- 在fedora「27」下,安装mysql 问题总结
有时会出现,没有mysql.sock,不存在的问题, Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. ...
- http请求后台报406错误
1.springMVC的项目,通过浏览器访问后台方法遇到了报406的错误,找了很多原因,最终发现是因为缺少spring-mvc的json配置. 2.添加依赖:jackson-databind.jack ...
- [SCOI2008]奖励关_状压动归_数学期望
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 20; dou ...
- Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导
Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...
- 安装sublimeServer插件
1.安装目的 做练习在谷歌浏览器中遇到报错信息:axios.min.js:8 Failed to load file:///E:/%E8%87%AA%E5%AD%A6/vue%E5%AD%A6%E4% ...
- C语言基础 (10) 变量作用域,生命周期 内存结构
01 课程回顾 1.指针数组 注意: 对于数组来说,在使用sizeof的时候a和&a[0]是不一样的, 虽然以%x打印出来他们都是地址 2.值传递 int a; fun(a); int *** ...
- GDI Bezier 样条曲线(7)
Bezier 样条曲线 Bezier 样条曲线使用四个点来定义:两个端点(起点和终点)和两个控点(用于使其不同程度地弯曲). 绘制 Bezier 样条曲线 使用 PolyBezier 函数和 Poly ...
- IOS开发:官方自带的JSON使用
apple在ios5的时候提供了json的相关处理api,其中最主要的是NSJSONSerialization类. + (BOOL)isValidJSONObject:(id)obj; + (NSDa ...
- 自动化测试之firebug、firepath、IDE的使用
1firebug安装-firefox添加组件-firebug 如图 firepath依赖于firebug 展示路径用,安装和firebug一样
- Jquery 根据HTML内容选择元素
选择所有包含 "is" 的 元素: $("p:contains(is)")