Servlet开发(三)之ServletConfig,ServletContext

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> Servlet开发(三)之ServletConfig,ServletContext

点击蓝字“程序员考拉”欢迎关注!

  1. ServletConfig

Servlet是开发动态web的技术,而web.xml是Tomcat工程中最基础也最重要的配置文件,Tomcat启动项目的时候会加载并读取这个文件,其中web.xml中在servlet标签中会有关于servlet的配置,可以使用一个或多个init-param/init-param标签为servlet配置一些初始化参数。

 

1.1  如下为servlet配置一些初始化参数。

Servlet开发(三)之ServletConfig,ServletContext

 

1.2 通过ServletConfig获取servlet的初始化参数

可以通过如下程序获得servlet的初始化参数。

1234567891011121314151617181920212223242526272829303132333435363738394041
package MyServletDemo;import java.io.IOException;import java.util.Enumeration;import javax.servlet.ServletConfig;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 MyServletDemo*///@WebServlet("/ServletDemo1")public class ServletDemo1 extends HttpServlet { private ServletConfig config; @Override   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/");       //获取所有的初始化参数       EnumerationString e = config.getInitParameterNames();       while(e.hasMoreElements()){           String name = e.nextElement();           String value = config.getInitParameter(name);           response.getWriter().print(name + "=" + value + "br/");       }   }    public void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       this.doGet(request, response);   }}

package MyServletDemo;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
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 MyServletDemo

*/
//@WebServlet(“/ServletDemo1”)
public class ServletDemo1 extends HttpServlet {
 private ServletConfig config;
 @Override
   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/“);
       //获取所有的初始化参数
       EnumerationString e = config.getInitParameterNames();
       while(e.hasMoreElements()){
           String name = e.nextElement();
           String value = config.getInitParameter(name);
           response.getWriter().print(name + “=” + value + “br/“);
       }
   }

   public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       this.doGet(request, response);
   }
}

 

当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

运行后显示以下结果。

Servlet开发(三)之ServletConfig,ServletContext

需要注意,在servlet3.0之后的版本,可以通过@WebServlet直接映射,即@WebServlet相当于web.xml中的servlet-mapping/servlet-mapping这个标签的作用。在上面的获取servlet的初始化参数的例子中,已经在web.xml中有了servlet-mapping/servlet-mapping这个映射标签,所以ServletDemo1.java这个程序中就不需要再使用@WebServlet(“/ServletDemo1”)了。

  1. ServletContext对象

2.1 为什么需要ServletContext?

①访问某个网站时候,往往都会看到网站的首页面显示您是第几位浏览者(网站计数器),这是如何实现的?

②显示论坛在线人数

2.2 什么是ServletContext?

要理解ServletContext先来和cookie,session做一个对比。

  • cookie:是指存储在客户端上的信息,浏览器一旦关闭cookie就被清除。每个用户都有独立的存储cookie的空间,互相独立,只能自己访问自己的cookie。
  • session:是指存储在服务器端的信息。每个用户都有独立的存储session的空间,互相独立,只能自己访问自己的session。
  • servletContext:servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是通过 getServletContext()方法获得的,由于HttpServlet继承GenericServlet的关系,GenericServlet类和HttpServlet类同时具有该方法。所有用户都可以访问servletContext,比如说我用谷歌给ServletContext 传递了信息,不仅在谷歌页面能获得ServletContext中的信息,在IE浏览器输入正确的网址也能得到ServletContext中的信息。

 

下图可以很好的说明他们之间的关系。

Servlet开发(三)之ServletConfig,ServletContext

 

什么是ServletContext?

  • ServletContext是一个用于和servlet容器交流的servlet类,它定义一个方法的集合,例如获得文件的类型,分派调遣request对象或者写一个日志文件。
  • web容器在启动时,它会为每个web应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
  • ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig().getServletContext()方法获得ServletContext对象。
  • 由于一个web应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

 

 2.3 ServletContext的应用

2.3.1 多个servlet通过ServletContext对象实现数据共享

 

上面提到过servlet之间可以通过ServletContext对象来实现通讯,下面两个程序ServletContextDemo1和ServletContextDemo2通过ServletContext对象实现了数据共享。

 

ServletContextDemo1.java

12345678910111213141516171819202122
package MyServletDemo;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class ServletContextDemo1 extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)       throws ServletException,IOException{   String data = "servletcontextdemodata";   //通过ServletConfig.getServletContext方法获得ServletContext对象       ServletContext context = this.getServletConfig().getServletContext();     //将data存储到ServletContext对象中       context.setAttribute("data", data);   }  public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         doGet(request, response);     }}

package MyServletDemo;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextDemo1 extends HttpServlet{
 public void doGet(HttpServletRequest request,HttpServletResponse response)
       throws ServletException,IOException{
   String data = “servletcontextdemodata”;
   //通过ServletConfig.getServletContext方法获得ServletContext对象
       ServletContext context = this.getServletConfig().getServletContext();
     //将data存储到ServletContext对象中
       context.setAttribute(“data”, data);  
 }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         doGet(request, response);
     }
}

 ServletContextDemo2.java

1234567891011121314151617181920212223
package MyServletDemo;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class ServletContextDemo2 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       ServletContext context = this.getServletContext();       //从ServletContext对象中取出数据       String data = (String) context.getAttribute("data");       response.getWriter().print("data="+data);   }    public void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       doGet(request, response);   }}

package MyServletDemo;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextDemo2 extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       ServletContext context = this.getServletContext();
       //从ServletContext对象中取出数据
       String data = (String) context.getAttribute(“data”);
       response.getWriter().print(“data=”+data);
   }

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

先运行ServletContextDemo1,将数据data存储到ServletContext对象中,然后运行ServletContextDemo2就可以从ServletContext对象中取出数据了,这样就实现了数据共享,如下图所示:

Servlet开发(三)之ServletConfig,ServletContext

 

注意要在web.xml中进行配置:

Servlet开发(三)之ServletConfig,ServletContext

 

2.3.2 通过ServletContext获取web应用的初始化参数

在web.xml文件使用context-param标签配置web应用的初始化参数,

Servlet开发(三)之ServletConfig,ServletContext

编写代码ServletContextDemo3获得web的初始化参数,

12345678910111213141516171819202122232425
package MyServletDemo;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class ServletContextDemo3 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {        ServletContext context = this.getServletContext();       //获取整个web站点的初始化参数       String contextInitParam = context.getInitParameter("URL链接");       response.getWriter().print(contextInitParam);   }    public void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       doGet(request, response);   } }

package MyServletDemo;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextDemo3 extends HttpServlet {

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

       ServletContext context = this.getServletContext();
       //获取整个web站点的初始化参数
       String contextInitParam = context.getInitParameter(“URL链接”);
       response.getWriter().print(contextInitParam);
   }

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

}

 在web.xml中进行配置servlet,

Servlet开发(三)之ServletConfig,ServletContext

运行后显示如下结果:

Servlet开发(三)之ServletConfig,ServletContext

2.3.3 用ServletContext实现请求转发

ServletContextDemo4.java

12345678910111213141516171819202122232425262728
package MyServletDemo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class ServletContextDemo4 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       String data = "h1font color='red'servletcontextDemo4/font/h1";       response.getOutputStream().write(data.getBytes());       //获取ServletContext对象       ServletContext context = this.getServletContext();       //获取请求转发对象(RequestDispatcher)       RequestDispatcher rd = context.getRequestDispatcher("/ServletContextDemo5");      //调用forward方法实现请求转发       rd.forward(request, response);   }    public void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {   }}

package MyServletDemo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextDemo4 extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       String data = “h1font color=’red’servletcontextDemo4/font/h1”;
       response.getOutputStream().write(data.getBytes());
       //获取ServletContext对象
       ServletContext context = this.getServletContext();
       //获取请求转发对象(RequestDispatcher)
       RequestDispatcher rd = context.getRequestDispatcher(“/ServletContextDemo5”);
      //调用forward方法实现请求转发
       rd.forward(request, response);
   }

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

 

ServletContextDemo5.java

1234567891011121314151617181920
package MyServletDemo;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class ServletContextDemo5 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       response.getOutputStream().write("servletDemo5".getBytes());   }    public void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {       this.doGet(request, response);   } }

package MyServletDemo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextDemo5 extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       response.getOutputStream().write(“servletDemo5”.getBytes());
   }

   public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       this.doGet(request, response);
   }

}

 

先来看看ServletContextDemo4.java代码中的RequestDispatcher,看API帮助文档。

Servlet开发(三)之ServletConfig,ServletContext

也就是说,RequestDispatcher定义一个对象,该对象接收来自客户端的请求,并将它们发送到服务器上的任何资源(例如servlet,HTML文件或JSP文件)。 servlet容器创建RequestDispatcher对象,该对象用作位于特定路径或由特定名称给定的服务器资源的包装器。此接口旨在包装servlet,但servlet容器可以创建RequestDispatcher对象来包装任何类型的资源。

上述代码中通过RequestDispatcher rd = context.getRequestDispatcher(“/servlet/ServletContextDemo5”);获取请求转发内容。

Servlet开发(三)之ServletConfig,ServletContext

getRequestDispatcher()返回一个RequestDispatcher对象,该对象充当位于给定路径的资源的包装器。 RequestDispatcher对象可用于将请求转发到资源或将资源包含在响应中。 资源可以是动态的也可以是静态的。

路径名必须以/开头,并且被解释为相对于当前上下文根。 使用getContext为外部上下文中的资源获取RequestDispatcher。如果ServletContext无法返回RequestDispatcher,则此方法返回null。

运行之前还是要在web.xmLl中进行配置。

运行ServletContextDemo5.java,显示如下结果:

Servlet开发(三)之ServletConfig,ServletContext

 

运行ServletContextDemo4.java,实现了通过访问ServletContextDemo4,浏览器显示ServletContextDemo5的内容,也就是实现了请求转发的功能。

Servlet开发(三)之ServletConfig,ServletContext

 

未完,待续。

Servlet开发(三)之ServletConfig,ServletContext

 

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> Servlet开发(三)之ServletConfig,ServletContext


 上一篇
面试中的 Singleton 面试中的 Singleton
作者:loveis715出处:https://www.cnblogs.com/loveis715 出处:https://www.cnblogs.com/loveis715 引子“请写一个Singleton。”面试官微笑着和我说。 “这可
下一篇 
servlet开发(四)之ServletContext servlet开发(四)之ServletContext
点击蓝字“程序员考拉”欢迎关注! 接上一篇。 2.3.4 利用ServletContext对象读取资源文件比如我们要读取web项目中的配置文件。项目目录结构如下:     使用ServletContext对象读取资源文件的示例代码如下: