欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
使用XML文件來(lái)實(shí)現對Servlet的配置
使用XML文件來(lái)實(shí)現對Servlet的配置
我們在Web應用中可以使用xml來(lái)配置servlet,給其提供初始化參數,如下例:
我們創(chuàng )建的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中我們定義了兩個(gè)屬性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 !!!
注意:此處我們不能同時(shí)給ds設置初始值,因為web.xml的DTD中約定了只能定義一個(gè)屬性也就是在配置文件中只允許聲明一個(gè)參數值對。這樣,在我們的servlet中就可以這樣來(lái)訪(fǎng)問(wèn)此屬性:
this.getServletConfig().getInitParameter("message")。
但是,有時(shí)候我們需要同時(shí)對多個(gè)屬性用XML來(lái)初始化,那么我們就需要自己來(lái)寫(xiě)XML文件,同時(shí)自己來(lái)解析了。


使用XML來(lái)配置Servlet的好處:

如果不在XML中對Servlet配置,那么我們修改Servlet的屬性的話(huà)就要重新啟動(dòng)服務(wù)器,而如果使用XML來(lái)配置的話(huà)就不需要重新啟動(dòng)服務(wù)器而可以自動(dòng)生效。服務(wù)器可以自動(dòng)監視其改變而重新裝入文檔。對企業(yè)來(lái)說(shuō),系統的連續運營(yíng)是很重要的。

XML來(lái)配置Servlet主要用在初始化參數在運行過(guò)程中需要改變的情況下。

(如果可以實(shí)現配置多屬性的話(huà),那么不是有點(diǎn)象Spring中的動(dòng)態(tài)注入???)
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
javaweb學(xué)習之Servlet
Servlet一(web基礎學(xué)習筆記二十)
spring boot web相關(guān)配置
servet取得web.xml的初始值
Filter的執行順序與實(shí)例
Servlet的三種創(chuàng )建方式及servlet解析
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久