ServletContext
ServletContext是Servlet容器上下文环境对象,定义一组方法,servlet 使用这些方法与其 servlet 容器进行通信,例如,获取文件的 MIME 类型、分发请求或写入日志文件。
每个web应用都有且仅有一个ServletContext对象,这个对象在所有的Servlet都可以使用。
ServletContext 对象包含在ServletConfig 对象中,ServletConfig 对象在初始化Servlet 时由Web 服务器提供给Servlet。
ServletContext在jsp中用application内置对象来表示,而在Servlet中通过调用方法
ServletContext sc = getServletContext();
因为一个web应用就一个ServletContext 对象,因此可以使用ServletContext 来做一些全局性的属性设置等。比如网站访问计数器等等。下面给出一个简单的页面计数器:
<body>
<%
Integer x = (Integer) application.getAttribute("num");
if (x == null) {
application.setAttribute("num", 1);
} else {
application.setAttribute("num", ++x);
}
out.println("您是第"+x+"位访客!");
%>
</body>
<%
Integer x = (Integer) application.getAttribute("num");
if (x == null) {
application.setAttribute("num", 1);
} else {
application.setAttribute("num", ++x);
}
out.println("您是第"+x+"位访客!");
%>
</body>
ServletContext与ServletConfig的区别:

获取资源绝对路径:
getServletContext().getRealPath("/");
上面的代码可以返回应用程序部署绝对路径。
Servlet
Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。
有关Servlet介绍可以参看:
1、Servlet配置

2、Servlet的工作过程概念图

3、不同的任务所发生的位置

4、J2EE应用服务器的概念图

5、Web服务器工作原理图

6、Java Web应用部署环境

7、MVC思想对比

8、HTTP请求响应详细过程


9、Servlet生命周期







10、ServletConfig与ServletContext的区别

11、HTTP请求分类详解

12、GET与POST请求的区别




13、Servlet请求的高级用法






下载一个jar文件












Servlet的初始化参数



注意:JSP中无法得到Servlet的初始化参数。




1、Servlet的init()和构造方法仅仅调用一次,仅仅会在第一次请求时候被调用一次,或者当Servlet配置为启动时候最先加载则会在启动时候调用,配置方法是:<load-on-startup>0</load-on-startup>
2、由于Servlet只会调用构造方法一次,因此Servlet是单例的,在Servlet容器中,一个Servlet类只有一个Servlet实例存在。
3、Servlet处理请求是方式是多线程的,也就是说,一个请求会开启一个独立线程来处理。因此,在Servlet的处理方法应该注意线程安全。
4、Servlet可以配置参数,在Servlet类里可以读取参数。
<init-param>
<param-name>n1</param-name>
<param-value>v1</param-value>
</init-param>
<init-param>
<param-name>n2</param-name>
<param-value>v2</param-value>
</init-param>
<param-name>n1</param-name>
<param-value>v1</param-value>
</init-param>
<init-param>
<param-name>n2</param-name>
<param-value>v2</param-value>
</init-param>
ServletConfig cfg = getServletConfig();
String p1 = cfg.getInitParameter("n1");
String p2 = cfg.getInitParameter("n2");
String p1 = cfg.getInitParameter("n1");
String p2 = cfg.getInitParameter("n2");
5、Servlet有输出流,输出流分两种,一种是字节流,一种是字符流,也可以获取输入流,比如上传文件时候就有输入流,输入流的只能是字节流。
ServletOutputStream os = response.getOutputStream();
PrintWriter out = response.getWriter();
ServletInputStream is = request.getInputStream();
PrintWriter out = response.getWriter();
ServletInputStream is = request.getInputStream();
6、一个Servlet的配置对象ServletConfig对象只有一个,配置的参数也只读一次,在调用init()时候读取Servlet配置,并构建ServletConfig对象。
7、ServletConfig对象只能在Servlet中访问,不能被JSP访问。ServletContext对象可以被JSP访问。
8、一个Servlet有且只有一个ServletConfig对象,一个Web应用有且仅有一个ServletContext对象,ServletContext对象在JSP中通过application内置对象来访问。
9、ServletContext也可也配置自己参数,在web.xml中配置,在任何JSP或Servlet中均能获取到。
<context-param>
<param-name>cn1</param-name>
<param-value>cv1</param-value>
</context-param>
<context-param>
<param-name>cn2</param-name>
<param-value>cv2</param-value>
</context-param>
<param-name>cn1</param-name>
<param-value>cv1</param-value>
</context-param>
<context-param>
<param-name>cn2</param-name>
<param-value>cv2</param-value>
</context-param>
ServletContext ctx= getServletContext();
String c1 = ctx.getInitParameter("cn1");
String c2 = ctx.getInitParameter("cn2");
String c1 = ctx.getInitParameter("cn1");
String c2 = ctx.getInitParameter("cn2");