myeclipse 2017 CI 中如何修改Servlet模板

myeclipse 2017 CI 中如何修改Servlet模板

  在實際開發中,這些生成的代碼和註釋一般我們都用不到的,每次都要手工刪除這些註釋和代碼,很麻煩,因此可以根據開發的實際情況修改Servlet的模板代碼,改成符合實際開發需求的模板代碼。

下面以MyEclipse 2017爲例進行說明如何修改Servlet的模板代碼,具體步驟如下:

  找到 MyEclipse 2017 CI 安裝目錄下的 plugins 文件夾,比如我的:D:\learn\Java\MyEclipse\MyEclipse 2017 CI\plugins,然後找到 com.genuitec.eclipse.wizards_13.0.0.me201612231634.jar 這個jar文件,

  打開 com.genuitec.eclipse.wizards_13.0.0.me201612231634.jar 這個jar文件後,可以看到裏面有一個 templates 文件夾,進入 templates 文件夾,可以看到裏面有一個 Servlet.java 文件。

  修改裏面的代碼:刪除 doGet 和 doPost 裏面的代碼和方法註釋,在 doPost 方法裏面調用 doGet ,這是根據實際情況修改成的模板代碼,修改好之後,保存,重啓 MyEclipse 2017 CI,使用MyEclipse創建Servlet,此時就是用剛纔修改過的模板進行生成了。

  (注意:在 MyEclipse 10 安裝目錄下的 \Common\plugins文件夾  ,注意文件夾的不同哦!)

  <aw:import>  表示的是要導入的包,

  <aw:parentClass>  表示該servlet繼承的父類,

  <aw:constructor  表示的是構造器,

  <aw:method   表示的是方法的聲明,  

  新的 Servlet.java 文件中的內容如下:

#---------------------------------------------#
# <aw:description>Template for Servlet</aw:description>
# <aw:version>1.1</aw:version>
# <aw:date>04/05/2003</aw:date>
# <aw:author>Ferret Renaud</aw:author>
#---------------------------------------------#

<aw:import>java.io.IOException</aw:import>
<aw:import>java.io.PrintWriter</aw:import>

<aw:import>javax.servlet.ServletException</aw:import>
<aw:import>javax.servlet.http.HttpServlet</aw:import>
<aw:import>javax.servlet.http.HttpServletRequest</aw:import>
<aw:import>javax.servlet.http.HttpServletResponse</aw:import>

<aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>

<aw:constructor name="c1">
    
    public <aw:className/>() {
        super();
    }

</aw:constructor> 
 
<aw:method name="doGet">
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     
    }

</aw:method>

<aw:method name="doPost">
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doGet(request,response);
    }

</aw:method>

示例 XxxServlet.java 文件

 1 package com.itheima.product.web.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class PayOnlineServlet extends HttpServlet {
11 
12     @Override
13     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14 
15     }
16 
17     @Override
18     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19         doGet(request, response);
20     }
21 
22 }

附上:舊的 Servlet.java 文件中的內容如下:

#---------------------------------------------#
# <aw:description>Template for Servlet</aw:description>
# <aw:version>1.1</aw:version>
# <aw:date>04/05/2003</aw:date>
# <aw:author>Ferret Renaud</aw:author>
#---------------------------------------------#

<aw:import>java.io.IOException</aw:import>
<aw:import>java.io.PrintWriter</aw:import>

<aw:import>javax.servlet.ServletException</aw:import>
<aw:import>javax.servlet.http.HttpServlet</aw:import>
<aw:import>javax.servlet.http.HttpServletRequest</aw:import>
<aw:import>javax.servlet.http.HttpServletResponse</aw:import>

<aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>

<aw:constructor name="c1">
    /**
     * Constructor of the object.
     */
    public <aw:className/>() {
        super();
    }

</aw:constructor> 
 
<aw:method name="doGet">
    /**
     * 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");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

</aw:method>

<aw:method name="doPost">
    /**
     * 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 doPost(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 POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

</aw:method>

<aw:method name="doPut">
    /**
     * The doPut method of the servlet. <br>
     *
     * This method is called when a HTTP put request is received.
     * 
     * @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 doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        // Put your code here
    }

</aw:method>

<aw:method name="doDelete">
    /**
     * The doDelete method of the servlet. <br>
     *
     * This method is called when a HTTP delete request is received.
     * 
     * @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 doDelete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        // Put your code here
    }

</aw:method>

<aw:method name="init">
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

</aw:method>

<aw:method name="destroy">
    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

</aw:method>

<aw:method name="getServletInfo">
    /**
     * Returns information about the servlet, such as 
     * author, version, and copyright. 
     *
     * @return String information about this servlet
     */
    public String getServletInfo() {
        return "This is my default servlet created by Eclipse";
    }

</aw:method>

  具體操作如下圖所示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章