一、修改Servlet的默认模板代码
使用MyEclipse创建Servlet时,根据默认的Servlet模板生成的Servlet代码如下:
package gacl.servlet.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDefaultTemplateCode extends HttpServlet {
/**
* 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();
}
/**
* 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();
}
}
在实际开发中,这些生成的代码和注释一般我们都用不到的,每次都要手工删除这些注释和代码,很麻烦,因此可以根据开发的实际情况修改Servlet的模板代码,改成符合实际开发需求的模板代码。下面以MyEclipse 10为例进行说明如何修改Servlet的模板代码
具体步骤如下:找到MyEclipse安装目录下的\Common\plugins文件夹,比如:D:\MyEclipse10\Common\plugins,然后找到com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar这个jar文件,为了方便查找com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar这个jar文件,建议使用【SearchEverything】这样的文件查找工具,如下图所示:
用压缩工具打开,注意是打开不是解压这个jar包,如下图所示:
打开com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar这个jar文件后,可以看到里面有一个templates文件夹,进入templates文件夹,可以看到里面有一个Servlet.java文件,如下图所示:
打开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>
修改该模板,根据自己的实际情况进行修改,比如
删除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);
}
}
二、修改jsp的默认模板
同样也是找到com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar这个jar文件,用压缩工具打开,进入templates\jsp文件夹,可以看到MyEclipse自带的那些jsp模板,如下图所示:
这些jsp模板是MyEclipse自带的,我们也可以依样画葫芦,根据平时项目开发中的实际情况,创建一个jsp模板,然后添加到jsp目录中,操作步骤如下:
1、随便复制一个jsp模板出来(如:Jsp.vtl),复制到系统的桌面或者系统的其他盘进行存储
2、修改复制出来的模板,使用记事本或者editplus打开Jsp.vtl,可以看到Jsp.vtl的模板代码,如下所示:
#*---------------------------------------------#
# Template for a JSP
# @version: 1.2
# @author: Ferret Renaud
# @author: Jed Anderson
#---------------------------------------------#
*#<%@ page language="java" import="java.util.*" pageEncoding="$encoding"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '$title' starting page</title>
#parse( "templates/jsp/JSPMetaTags.vtl" )
</head>
<body>
This is my JSP page. <br>
</body>
</html>
这是Jsp.vtl的模板原始代码,这个模板的代码对于我来说不太符合项目开发中的实际情况,需要修改,修改后的模板如下:
#*---------------------------------------------#
# Template for a JSP
# @version: 1.2
# @author: 孤傲苍狼
#---------------------------------------------#
*#<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
为了避免覆盖原来的Jsp.vtl模板,将修改后的Jsp.vtl模板重命名,例如重命名成gacl.vtl。
3.将gacl.vtl模板复制,然后粘贴到com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar包里面的templates\jsp文件夹里。
4、从查看解压文件的界面中,返回到根目录(即com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar目录),找到模版配置文件templates.xml,如下图所示:
来源:http://dwz.date/2UM