使用MyEclipse創建Servlet時,根據默認的Servlet模板生成的Servlet代碼如下:
package com.web.servlet;
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 HttpServletDemo 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文件,可以看到裏面的模板代碼:
1 #---------------------------------------------#
2 # <aw:description>Template for Servlet</aw:description>
3 # <aw:version>1.1</aw:version>
4 # <aw:date>04/05/2003</aw:date>
5 # <aw:author>Ferret Renaud</aw:author>
6 #---------------------------------------------#
7
8 <aw:import>java.io.IOException</aw:import>
9 <aw:import>java.io.PrintWriter</aw:import>
10
11 <aw:import>javax.servlet.ServletException</aw:import>
12 <aw:import>javax.servlet.http.HttpServlet</aw:import>
13 <aw:import>javax.servlet.http.HttpServletRequest</aw:import>
14 <aw:import>javax.servlet.http.HttpServletResponse</aw:import>
15
16 <aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>
17
18 <aw:constructor name="c1">
19 /**
20 * Constructor of the object.
21 */
22 public <aw:className/>() {
23 super();
24 }
25
26 </aw:constructor>
27
28 <aw:method name="doGet">
29 /**
30 * The doGet method of the servlet. <br>
31 *
32 * This method is called when a form has its tag value method equals to get.
33 *
34 * @param request the request send by the client to the server
35 * @param response the response send by the server to the client
36 * @throws ServletException if an error occurred
37 * @throws IOException if an error occurred
38 */
39 public void doGet(HttpServletRequest request, HttpServletResponse response)
40 throws ServletException, IOException {
41
42 response.setContentType("text/html");
43 PrintWriter out = response.getWriter();
44 out.println(
45 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
46 out.println("<HTML>");
47 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
48 out.println(" <BODY>");
49 out.print(" This is ");
50 out.print(this.getClass());
51 out.println(", using the GET method");
52 out.println(" </BODY>");
53 out.println("</HTML>");
54 out.flush();
55 out.close();
56 }
57
58 </aw:method>
59
60 <aw:method name="doPost">
61 /**
62 * The doPost method of the servlet. <br>
63 *
64 * This method is called when a form has its tag value method equals to post.
65 *
66 * @param request the request send by the client to the server
67 * @param response the response send by the server to the client
68 * @throws ServletException if an error occurred
69 * @throws IOException if an error occurred
70 */
71 public void doPost(HttpServletRequest request, HttpServletResponse response)
72 throws ServletException, IOException {
73
74 response.setContentType("text/html");
75 PrintWriter out = response.getWriter();
76 out.println(
77 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
78 out.println("<HTML>");
79 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
80 out.println(" <BODY>");
81 out.print(" This is ");
82 out.print(this.getClass());
83 out.println(", using the POST method");
84 out.println(" </BODY>");
85 out.println("</HTML>");
86 out.flush();
87 out.close();
88 }
89
90 </aw:method>
91
92 <aw:method name="doPut">
93 /**
94 * The doPut method of the servlet. <br>
95 *
96 * This method is called when a HTTP put request is received.
97 *
98 * @param request the request send by the client to the server
99 * @param response the response send by the server to the client
100 * @throws ServletException if an error occurred
101 * @throws IOException if an error occurred
102 */
103 public void doPut(HttpServletRequest request, HttpServletResponse response)
104 throws ServletException, IOException {
105
106 // Put your code here
107 }
108
109 </aw:method>
110
111 <aw:method name="doDelete">
112 /**
113 * The doDelete method of the servlet. <br>
114 *
115 * This method is called when a HTTP delete request is received.
116 *
117 * @param request the request send by the client to the server
118 * @param response the response send by the server to the client
119 * @throws ServletException if an error occurred
120 * @throws IOException if an error occurred
121 */
122 public void doDelete(HttpServletRequest request, HttpServletResponse response)
123 throws ServletException, IOException {
124
125 // Put your code here
126 }
127
128 </aw:method>
129
130 <aw:method name="init">
131 /**
132 * Initialization of the servlet. <br>
133 *
134 * @throws ServletException if an error occurs
135 */
136 public void init() throws ServletException {
137 // Put your code here
138 }
139
140 </aw:method>
141
142 <aw:method name="destroy">
143 /**
144 * Destruction of the servlet. <br>
145 */
146 public void destroy() {
147 super.destroy(); // Just puts "destroy" string in log
148 // Put your code here
149 }
150
151 </aw:method>
152
153 <aw:method name="getServletInfo">
154 /**
155 * Returns information about the servlet, such as
156 * author, version, and copyright.
157 *
158 * @return String information about this servlet
159 */
160 public String getServletInfo() {
161 return "This is my default servlet created by Eclipse";
162 }
163
164 </aw:method>
修改該模板,根據自己的實際情況進行修改,比如
刪除doGet和doPost裏面的代碼和方法註釋,在doPost方法裏面調用doGet,這是根據實際情況修改成的模板代碼,修改好之後,保存,重啓MyEclipse,使用MyEclipse創建Servlet,此時就是用剛纔修改過的模板進行生成了,生成的代碼如下:
1 package gacl.servlet.study;
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 ServletNewTemplateCode extends HttpServlet {
11
12 public void doGet(HttpServletRequest request, HttpServletResponse response)
13 throws ServletException, IOException {
14
15 }
16
17 public void doPost(HttpServletRequest request, HttpServletResponse response)
18 throws ServletException, IOException {
19 doGet(request, response);
20 }
21
22 }
二、修改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的模板代碼,如下所示:
1 #*---------------------------------------------#
2 # Template for a JSP
3 # @version: 1.2
4 # @author: Ferret Renaud
5 # @author: Jed Anderson
6 #---------------------------------------------#
7 *#<%@ page language="java" import="java.util.*" pageEncoding="$encoding"%>
8 <%
9 String path = request.getContextPath();
10 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
11 %>
12
13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
14 <html>
15 <head>
16 <base href="<%=basePath%>">
17
18 <title>My JSP '$title' starting page</title>
19
20 #parse( "templates/jsp/JSPMetaTags.vtl" )
21 </head>
22
23 <body>
24 This is my JSP page. <br>
25 </body>
26 </html>
這是Jsp.vtl的模板原始代碼,這個模板的代碼對於我來說不太符合項目開發中的實際情況,需要修改,修改後的模板如下:
1 #*---------------------------------------------#
2 # Template for a JSP
3 # @version: 1.2
4 # @author: 孤傲蒼狼
5 #---------------------------------------------#
6 *#<%@ page language="java" pageEncoding="UTF-8"%>
7 <!DOCTYPE HTML>
8 <html>
9 <head>
10 <title></title>
11 </head>
12
13 <body>
14
15 </body>
16 </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,如下圖所示:
同樣,將templates.xml文件複製到桌面,使用記事本或者editplus打開進行修改。
修改如下:在<templateLibrary>裏添加如下元素:
1 <template
2 context="com.genuitec.eclipse.wizards.jsp"
3 script="templates/jsp/gacl.vtl"
4 name="gacl-JSP template"/>
其中:
1、templates/jsp/gacl.vtl:爲新添加的jsp模板的相對路徑。
2、gacl-JSP template:爲MyEclipse中所要標識的模版名稱,MyEclipse新建JSP文件時通過這個名字來選擇對應的模版。
3、context="com.genuitec.eclipse.wizards.jsp" 這個一定要存在,並且跟其他jsp模板的設置一樣,複製就可以。
templates.xml修改後的內容如下圖所示:
5、修改完成後,將com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar包中的templates.xml文件刪除掉,然後將修改過後的templates.xml複製,粘貼到com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar包中,如下圖所示:
到此,我們的Jsp模板就算是創建好了。
6.啓動MyEclipse,然後新創建一個Jsp頁面,此時就可以使用我們自定義的那個Jsp頁面模板了,如下圖所示:
通過以上兩種方式,我們在開發的時候,就可以根據我們自己的開發習慣來定製servlet和jsp的模板了,對於開發效率上或多或少有一點提高吧,不用每次都把MyEclipse生成的很多用不到的代碼刪掉。