Servlet課程0424(一) 通過實現Servlet介面來開發Servlet

來源:互聯網
上載者:User

標籤:

//這是我的第一個Servlet,使用實現Servlet介面的方式來開發package com.tsinghua;import javax.servlet.*;import java.io.*;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import java.io.IOException;public class Hello implements Servlet{    /**     * Method init     *     *     * @param parm1     *     @throws ServletException     *     */     //該函數用於初始化該Servlet(類似於類的建構函式)     //該函數只會被調用一次(當使用者第一次訪問該Servlet時被調用)    public void init(ServletConfig parm1) throws ServletException {        // TODO: 在這添加你的代碼        System.out.println("init it");    }    /**     * Method getServletConfig     *     *     * @return     *     */    public ServletConfig getServletConfig() {        // TODO: 在這添加你的代碼        return null;    }    /**     * Method service     *     *     * @param parm1     * @param parm2     *     @throws ServletException     @throws IOException     *     */     //這個函數用於處理商務邏輯     //程式員應當把商務邏輯代碼寫在這裡     //當使用者每訪問該Servlet時都會被調用     //req用於獲得用戶端(瀏覽器)的資訊     //res用於向 用戶端(瀏覽器)返回資訊    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {        // TODO: 在這添加你的代碼        //向控制台列印        System.out.println("serviece it");        //從res中得到PrintWriter,向瀏覽器列印資訊        PrintWriter pw = res.getWriter();        pw.println("hello World!");    }    /**     * Method getServletInfo     *     *     * @return     *     */    public String getServletInfo() {        // TODO: 在這添加你的代碼        return "";    }    /**     * Method destroy     *     *     */          //銷毀Servlet執行個體,釋放記憶體     //1.reload該Servlet(webApps) //2.關閉tomcat //3.關機    public void destroy() {        System.out.println("destroy!");        // TODO: 在這添加你的代碼                    }        }

 

Servlet課程0424(一) 通過實現Servlet介面來開發Servlet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.