您的位置:首页 > 编程语言 > Java开发

myeclipse的servlet模板替换

2015-01-27 15:54 253 查看
    用myeclipse的创建servlet时,生成的类看起来让人很郁闷,里面的doPost方法和doGet方法有注释,而且里面也已经写了一些内容了,怎样修改呢?

    首先,在myeclipse的安装目录下搜索com.genuitec.eclipse.wizards,搜索到的结果如图所示:



com.genuitec.eclipse.wizards_11.0.0.me201303311935.jar(不同版本的myeclipse这个文件名的最后有可能不同)这个jar文件就是我们要找的文件,然后打开这个文件,找到这个jar文件的templates目录下的Servlet.java文件,这个就是创建servlet时,myeclipse所使用的模板文件,接下来就是对这个文件进行修改,建议修改前先对这个文件进行备份。将这个文件拷贝出来,然后用编辑器打开,大致看一下这个文件,文件开始以#开始的行应该是一些注释信息,<aw:import>表示的是要导入的包,<aw:parentClass>表示该servlet继承的父类,<aw:constructor表示的是构造器,<aw:method
表示的是方法的声明,知道这些以后,OK!开始修改吧多找到<aw:method name="doGet">将doGet方法里面的内容替换成doPost(request, response); 并把上面的JavaDoc删掉,然后找到<aw:method name="doPost">把方法体的内容去掉,并去掉javaDoc,最后清理下导入包。我把我最后修改的结果贴出来,如果有需要可以自行修改一些东西,下面贴出来主要是为了方便大家查看:

文件名: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>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 {

doPost(request, response);
}

</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 {

}

</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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息