一、ServletConfig对象
1.1、配置servlet初始化参数
在servlet的配置文件中web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>ServletDemo6</servlet-name>
<servlet-class>my.servlet.demo.ServletDemo6</servlet-class>
<!--配置ServletConfigDemo1的初始化参数 -->
<init-param>
<param-name>name</param-name>
<param-value>my</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123</param-value>
</init-param>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo6</servlet-name>
<url-pattern>/ServletDemo6</url-pattern>
</servlet-mapping>
</web-app>
1.2、通过ServletConfig获取servlet初始化参数
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
例如:
package my.servlet.demo;
//导入必需的 java 库
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class ServletDemo1 extends HttpServlet {
/**
* 定义ServletConfig对象来接收配置的初始化参数
*/
private ServletConfig config;
/**
* 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
* 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
* 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
* 得到当前servlet的初始化参数信息。
*/
public void init(ServletConfig config) throws ServletException
{
// 执行必需的初始化
this.config=config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//获取在web.xml中配置的初始化参数
String paramVal=this.config.getInitParameter("name");//获取指定的初始化参数
response.getWriter().print(paramVal);
response.getWriter().print("<hr/>");
//获取所有的初始化参数
Enumeration<String> e =config.getInitParameterNames();
while(e.hasMoreElements())
{
String name =e.nextElement();
String value=config.getInitParameter(name);
response.getWriter().print(name+"="+value+"<br/>");
}
}
public void destroy()
{
// 什么也不做
}
}
运行结果如下:
二、ServletContext对象
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContxt对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称为context域对象。
2.1、多个servlet通过ServletContext对象实现数据共享
例子:ServletDemo1和ServletDemo2通过ServletContext对象实现数据共享
package my.servlet.demo;
//导入必需的 java 库
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class ServletDemo1 extends HttpServlet {
private ServletConfig config;
public void init(ServletConfig config) throws ServletException
{
// 执行必需的初始化
this.config=config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String data="itnnn";
ServletContext context=config.getServletContext();
context.setAttribute("data", data);
}
public void destroy()
{
// 什么也不做
}
}
package my.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo2
*/
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context =this.getServletContext();
String data=(String) context.getAttribute("data");
response.getWriter().print("data="+data);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
先运行ServletDemo1,将数据data存储到ServletContext对象中,然后运行ServletDemo2就可以从ServletContext对象中取出数据了,这样就实现了数据共享,运行结果如下图所示:
2.2、获取WEB应用的初始化参数
在web.xml文件中使用<context-param>标签配置WEB应用的初始化参数,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 配置WEB应用的初始化参数 -->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package my.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context=this.getServletContext();
//获取整个web站点的初始化参数
String contextInitParam =context.getInitParameter("url");
response.getWriter().print(contextInitParam );
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
运行结果:
2.3、用ServletContext实现请求转发
ServletDemo4.java
package my.servlet.demo;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo4 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String data="<h1><font color='red'>abc</font></h1>";
response.getOutputStream().write(data.getBytes());
ServletContext context=this.getServletContext();
RequestDispatcher rd =context.getRequestDispatcher("/ServletDemo5");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
ServletDemo5.java
package my.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo5 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getOutputStream().write("servletDemo5".getBytes());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<description>This is the description of my J2EEcomponent</description>
<display-name>This is the display of my J2EEcomponent</display-name>
<servlet-name>ServletDemo4</servlet-name>
<servlet-class>my.servlet.demo.ServletDemo4</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EEcomponent</description>
<display-name>This is the display of my J2EEcomponent</display-name>
<servlet-name>ServletDemo5</servlet-name>
<servlet-class>my.servlet.demo.ServletDemo5</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo4</servlet-name>
<url-pattern>/ServletDemo4</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletDemo5</servlet-name>
<url-pattern>/ServletDemo5</url-pattern>
</servlet-mapping>
</web-app>
运行结果:
访问的是ServletDemo4,浏览器显示的却是ServletDemo5的内容,这就是使用ServletContext实现了请求转发。
2.4、利用ServletContext对象读取资源文件
2.5、在客户端缓存servlet输出