Eclipse for JavaEE中修改Servlet建立模板__Java

來源:互聯網
上載者:User

在用Eclipse進行web開發時,其在建立Servlet時會有很多注釋,非常的不簡潔,可以自己編寫相對應的模板建立Servlet。
操作圖解

1.首先找到菜單條上的Window並點擊出現下拉列,選中下拉列中的Preferences選項跳出如下圖結果

2.找到其中的java列點開—>找到其中的Editor點開—>找到其中的Templates點擊出現如上圖結果

點擊右側的建立按鈕出現一個類似下圖的視窗

3.在介面上的Name和Description中鍵入如上內容,在下方的Pattern中輸入你自己編寫好的servlet模板

我自己的模板如下

package ${enclosing_package};import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author ${user}  E-mail: example@aliyun.com * @version 建立時間:${date}  ${time} * ${tags} */@WebServlet("/${primary_type_name}")public class ${primary_type_name} extends HttpServlet {    private static final long serialVersionUID = 1L;    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("UTF-8");        response.setContentType("text/html; charset=UTF-8");    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}

4.最後一路點擊下方的OK按鈕,之後回到JavaEE視圖建立一個servlet檔案,使用快速鍵Ctrl+A全選中代碼,鍵入“servlet”字樣,代碼自動提示,然後按一下Enter鍵就得到自己所要的代碼。

注意:參看其他部落格時我運行時總是報404錯誤,可能是由於我個人的原因,後來我使用了註解類,親測可以正常運行。如有需要可以在web.xml中另加配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.