自定义myeclipse中的servlet模板文件

来源:互联网 发布:成熟电子病历系统源码 编辑:程序博客网 时间:2024/06/07 15:35
在实际开发中,用模板生成的servlet中有很多的注释和有部分代码是我们用不到的,这部分我经常会选择删除,但是每次都这样干很烦。作为一个程序员啦只要一做起重复的事情就想一劳永逸,所以直接把servlet模板进行自定义,省去开发中的一个小麻烦。下面是具体的步骤:1.进入MyEclipse安装目录下的\plugins文件夹,搜索com.genuitec.eclipse.wizards.xxx.jar文件,如图:![搜索到的jar文件](http://img.blog.csdn.net/20160909164821237)2.右键用压缩工具打开,注意不是解压。3.打开之后看到里面有一个templates文件夹,进入templates文件夹,可以看到里面有一个Servlet.java文件,如下图所示:![templates文件夹是最后一个](http://img.blog.csdn.net/20160909165250539)![最下面的servlet.java文件就是servlet模板](http://img.blog.csdn.net/20160909165414668)4.右键打开servlet.java文件,如图:![选择便于工具打开](http://img.blog.csdn.net/20160909165608437)5.根据默认的Servlet模板生成的Servlet代码如下:
#---------------------------------------------## <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>
6.删除doGet和doPost里面的代码和方法注释,在doPost方法里面调用doGet,修改好之后,保存,重启MyEclipse,使用MyEclipse创建Servlet,此时就是用刚才修改过的模板进行生成了,生成的代码如下:
package gacl.servlet.study;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletNewTemplateCode extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

  现在就不用再看到烦人的部分了。

0 0
原创粉丝点击