欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaEE中關(guān)于ServletConfig的小結(jié)

 更新時(shí)間:2014年10月26日 10:31:44   投稿:hebedich  
ServletConfig是針對(duì)特定的Servlet的參數(shù)或?qū)傩?。ServletConfig是表示單獨(dú)的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個(gè)servlet被實(shí)例化后,對(duì)任何客戶端在任何時(shí)候訪問有效,但僅對(duì)本servlet有效,一個(gè)servlet的ServletConfig對(duì)象不能被另一個(gè)servlet訪問

     在Servlet的配置文件中,可以使用一個(gè)或多個(gè)<init-param>標(biāo)簽為servlet配置一些初始化參數(shù)。當(dāng)servlet配置了初始化參數(shù)后,web容器在創(chuàng)建servlet實(shí)例對(duì)象時(shí),會(huì)自動(dòng)將這些初始化參數(shù)封裝到ServletConfig對(duì)象中,并在調(diào)用servlet的init方法時(shí),ServletConfig對(duì)象傳遞給servlet。進(jìn)而,程序員通過ServletConfig對(duì)象就可以得到當(dāng)前servlet的初始化參數(shù)信息。
示例代碼如下:

復(fù)制代碼 代碼如下:

 package com.yyz.servletconfig;
 import java.io.IOException;
 import java.util.Enumeration;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 public class ServletConfigDemo1 extends HttpServlet {
     ServletConfig config;
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
       //獲取指定的初始化參數(shù)
         String value = config.getInitParameter("xxx");
         response.getOutputStream().write(value.getBytes());
         //獲取所有的初始化參數(shù)
         Enumeration e = cofig.getInitParameterNames();
         while(e.hasMoreElements()){
             String name = (String) e.nextElement();
             value = config.getInitParameter(name);
             response.getOutputStream().write((name+"="+value+"<br/>").getBytes());
         }
     }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         doGet(request,response);
     }
     @Override
     public void init(ServletConfig config) throws ServletException {
         this.config = config;
     }
 }

相應(yīng)的web.xml如下:

復(fù)制代碼 代碼如下:

測(cè)試結(jié)果如下:

 在上面的代碼中,ServletConfigDemo1對(duì)象中有一個(gè)ServletConfig對(duì)象,其實(shí)這是不必要的。因?yàn)镾ervletConfigDemo1繼承了HttpServlet,HttpServlet又繼承了GenericServlet 。GenericServlet 已經(jīng)在內(nèi)部維護(hù)了一個(gè)ServletConfig對(duì)象。相關(guān)實(shí)現(xiàn)如下:

復(fù)制代碼 代碼如下:

 public abstract class GenericServlet
     implements Servlet, ServletConfig, java.io.Serializable
 {
     …    …
 private transient ServletConfig config;
 public ServletConfig getServletConfig() {
           return config;
     }
 }

因而我們可以通過我們寫的Servlet對(duì)象的getServletConfig()方法直接拿到ServletConfig對(duì)象,示例代碼如下:

復(fù)制代碼 代碼如下:

 package com.yyz.servletconfig;
 
 import java.io.IOException;
 import java.io.PrintWriter;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 public class ServletConfigDemo2 extends HttpServlet {
 
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
 
         String value = this.getServletConfig().getInitParameter("name");
         System.out.println(value);
 }
 
     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
 
         doGet(request, response);
     }
 
 }

web.xml文件:

復(fù)制代碼 代碼如下:

相關(guān)文章

最新評(píng)論