ServletConfig是一个由Tomcat服务器在初始化Servlet的时候创建并传递进来的一个对象。

该对象主要描述的时候一个servlet的配置信息。

如:

<servlet>         配置一个servlet
<servlet-name>helloservlet</servlet-name>  指定servlet的名字(任意)
 指定servlet具体的类的全限定名(包名.类名)
<servlet-class>cn.itcast.servlets.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>  映射一个servlet
<servlet-name>helloservlet</servlet-name>  需要映射的servlet名
<url-pattern>/helloservlet</url-pattern>  浏览器需要访问的路径
</servlet-mapping>

以上配置就是一个servlet的配置信息,如果开发者需要获取其中的数据,那么tomcat已经创建好的ServletConfig接口对象来方面开发者获取信息。

举例1:获取指定servlet的配置的名字。

public class Demo1 extends HttpServlet {
private ServletConfig config = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取servlet的配置名
String name = this.config.getServletName();
response.getOutputStream().write(name.getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// 重写init方法
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
}

举例2:获取servlet的配置参数。

在实际的编程过程中需要将所有可变的数据封装到配置文件中去,这样便于软件的后期的维护。Android。

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

// 获取servlet的配置名

String name = this.config.getServletName();

response.getOutputStream().write(name.getBytes());

// 获取配置参数

String ip = this.config.getInitParameter("ip");

String dbtype = this.config.getInitParameter("dbtype");

response.getOutputStream().write("<br/>".getBytes());

response.getOutputStream().write((ip+" , "+dbtype).getBytes());

// 使用循环获取所有的配置参数

Enumeration<String> names = this.config.getInitParameterNames();

while(names.hasMoreElements()){

String ini_name = names.nextElement();

String ini_value = this.config.getInitParameter(ini_name);

response.getOutputStream().write((ini_name+"="+ini_value+"<br/>").getBytes());

}

}

总结:发现tomcat创建并传递过来的servletconfig对象接收比较繁琐。

// 获取servletconfig对象
ServletConfig config = this.getServletConfig();
System.out.println(this.config == config);

为了方便开发者获取servletconfig对象,那么HttpServlet直接提供了getServletConfig(),

两种方式获取的对象是同一个对象。

ServletContext接口

ServletContext接口对象主要实现的是servlet之间的通信。该对象可以和web服务器进行通信。转发请求。

ServletContext接口是对网站的抽象描述。一个ServletContext代表一个web应用(网站)。

该对象是由Tomcat创建出来连同ServletConfig对象一并通过初始化方法传递过来的。

1实现servlet的通信

1. 放数据

  // 获取ServletContext对象
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
ServletContext context1 = this.getServletConfig().getServletContext();
ServletContext context2 = this.getServletContext();
System.out.println(context1 == context2); // true
// 主要作用之一是实现servler之间的数据通信
context1.setAttribute("news_title", "北京的灰霾天气");
}

2. 取数据

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String name = (String) this.getServletContext().getAttribute("news_title");
response.getOutputStream().write(name.getBytes());
}

2 实现服务器通信

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 和tomcat进行通信获取MIME类型
ServletContext context = this.getServletContext();
String type = context.getMimeType("/jnb.doc");
System.out.println(type);
}

1  获取网站资源(重点)

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 获取网站对象
ServletContext context = this.getServletContext();
// 获取网站中的静态资源
InputStream in = context.getResourceAsStream("/123.bmp");
// 获取输出流
FileOutputStream out = new FileOutputStream(new File("d:/123.bmp"));
// 边读 边写
byte[] bs = new byte[];
int len = ;
while((len = in.read(bs)) != -){
out.write(bs, , len);
out.flush();
}
// 释放资源
in.close();
out.close();
}

3请求转发

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 获取网站对象
ServletContext context = this.getServletContext();
// 转发
System.out.println("不好意思没钱!,找老汤去....");
context.getRequestDispatcher("/lt").forward(request, response);
}

请求转发:请求转发是服务器自己做的事情,那么在客户端是展现不出来的,因此浏览器的地址栏是不发生变化的。

最新文章

  1. 关于ACM的总结
  2. Alamofire 的使用
  3. oracle 11gr2 官方文档下载
  4. SQL: See the TSQL underneath the sp_execute calls
  5. 关于TCP/IP的三次握手和四次挥手解释
  6. NGif, Animated GIF Encoder for .NET
  7. (四)Ubuntu 14.04 文件服务器--samba的安装和配置
  8. [实变函数]2.2 聚点 (cluster point), 内点 (interior point), 界点 (boundary point)
  9. 五个小例子教你搞懂 JavaScript 作用域问题
  10. JS(三)
  11. react redux 相关技术
  12. UVa1003-Cutting sticks
  13. 一天搞定CSS:表格(table)--19
  14. java基础之集合框架(1)
  15. 数据库及SQL----常用知识点总结
  16. Jquery实现按钮点击遮罩加载,处理完后恢复
  17. nova创建虚拟机源码分析系列之三 PasteDeploy
  18. 使用单元素的枚举类型实现Singleton
  19. 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
  20. 996.ICU与死亡因素

热门文章

  1. IronPython Architecture
  2. SAP middb主键加索引
  3. FILTER:progid:DXImageTransform.Microsoft.Gradient使用
  4. Criterion - 一个简单可扩展的 C 语言测试框架
  5. SqlMapConfig.xml配置文件中的mapper映射器标签
  6. React Native 常用插件案例
  7. javascript+php实现根据用户时区显示当地时间的方法
  8. centos7之salt命令随笔笔记
  9. http://angular.github.io/router/
  10. Ural 1519 Formula 1 (DP)