国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用XML文件來實現對Servlet的配置

2019-11-18 12:48:48
字體:
來源:轉載
供稿:網友

  我們在Web應用中可以使用xml來配置servlet,給其提供初始化參數,如下例:
  
  我們創建的Servlet為:ServletDemo.java,代碼如下:
  
  /*
  * Created on 2005-8-29
  *
  * TODO To change the template for this generated file go to
  * Window - PReferences - Java - Code Style - Code Templates
  */
  package zy.pro.wd.servlet;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.sql.DataSource;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
  * @author zhangyi
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
  public class ServletDemo extends HttpServlet {
  
  String message;
  DataSource ds;
  /**
  * ConstrUCtor of the object.
  */
  public ServletDemo() {
  super();
  }
  
  /**
  * Destruction of the servlet. <br>
  */
  public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
  }
  
  /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  
  response.setContentType("text/Html");
  PrintWriter out = response.getWriter();
  out
  .println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
  out.println("<HTML>");
  out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println(" <BODY>");
  out.print("  This is ");
  out.print(this.getClass());
  out.println(", using the GET method<br>");
  
  out.println(this.getServletConfig().getInitParameter("message"));
  
  out.println(" </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
  }
  
  /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
  
  public void init() throws ServletException {
  // Put your code here
  }
  
  }
  
  在此Servlet中我們定義了兩個屬性message和ds。我們現在在web.xml中作如下配置:
  <servlet>
  <description>
  This is the description of my J2EE component
  </description>
  <display-name>
  This is the display name of my J2EE component
  </display-name>
  <servlet-name>ServletDemo</servlet-name>
  <servlet-class>zy.pro.wd.servlet.ServletDemo</servlet-class>
  
  <init-param>
  <description>initialize the field of message</description>
  <param-name>message</param-name>
  <param-value>
  welcome here ,thank you for visiting !!!
  </param-value>
  </init-param>
  
  </servlet>
  
  <servlet-mapping>
  <servlet-name>ServletDemo</servlet-name>
  <url-pattern>/servlet/ServletDemo</url-pattern>
  </servlet-mapping>
  
  加粗的部分是我們要作的配置。在其中我們給message屬性設置了初始值:
  
  welcome here ,thank you for visiting !!!
  
  注重:此處我們不能同時給ds設置初始值,因為web.xml的DTD中約定了只能定義一個屬性也就是在配置文件中只答應聲明一個參數值對。這樣,在我們的servlet中就可以這樣來訪問此屬性:
  
  this.getServletConfig().getInitParameter("message")。
  
  但是,有時候我們需要同時對多個屬性用XML來初始化,那么我們就需要自己來寫XML文件,同時自己來解析了。
  
  使用XML來配置Servlet的好處:
  
  假如不在XML中對Servlet配置,那么我們修改Servlet的屬性的話就要重新啟動服務器,而假如使用XML來配置的話就不需要重新啟動服務器而可以自動生效。服務器可以自動監視其改變而重新裝入文檔。對企業來說,系統的連續運營是很重要的。
  
  XML來配置Servlet主要用在初始化參數在運行過程中需要改變的情況下。
  
 ?。偃缈梢詫崿F配置多屬性的話,那么不是有點象Spring中的動態注入???)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海兴县| 香格里拉县| 始兴县| 清涧县| 昌图县| 宜城市| 分宜县| 凌海市| 乐都县| 阿图什市| 五原县| 新绛县| 郸城县| 灌阳县| 金华市| 县级市| 正宁县| 玛曲县| 泸州市| 台中市| 高淳县| 屯留县| 盐亭县| 南川市| 拜泉县| 伽师县| 易门县| 大城县| 五华县| 涿州市| 平果县| 海城市| 外汇| 蒙城县| 井陉县| 永定县| 沈阳市| 宜宾市| 晋州市| 沙田区| 宝丰县|