Eclipse使用总结——修改(My)Eclipse默认的Servlet和jsp代码模板_eclipse怎么修改servlet的自动生成的代码

总结

阿里十分注重你对源码的理解,对你所学,所用东西的理解,对项目的理解。

最新阿里蚂蚁金服四面(已拿offer)Java技术面经总结

最新阿里蚂蚁金服四面(已拿offer)Java技术面经总结

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

最新阿里蚂蚁金服四面(已拿offer)Java技术面经总结

43 PrintWriter out = response.getWriter();
44 out.println(
45 “”);
46 out.println(“”);
47 out.println(" A Servlet“);
48 out.println(” “);
49 out.print(” This is “);
50 out.print(this.getClass());
51 out.println(”, using the GET method");
52 out.println(" “);
53 out.println(”“);
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.

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 “”);
78 out.println(”“);
79 out.println(” A Servlet“);
80 out.println(” “);
81 out.print(” This is “);
82 out.print(this.getClass());
83 out.println(”, using the POST method");
84 out.println(" “);
85 out.println(”");
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.

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.

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.

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.

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>



![复制代码](http://common.cnblogs.com/images/copycode.gif)


  修改该模板,根据自己的实际情况进行修改,比如


   ![]()


   删除doGet和doPost里面的代码和方法注释,在doPost方法里面调用doGet,这是根据实际情况修改成的模板代码,修改好之后,保存,重启MyEclipse,使用MyEclipse创建Servlet,此时就是用刚才修改过的模板进行生成了,生成的代码如下:




![复制代码](http://common.cnblogs.com/images/copycode.gif)


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 }



![复制代码](http://common.cnblogs.com/images/copycode.gif)


###  二、修改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的模板代码,如下所示:**




![复制代码](http://common.cnblogs.com/images/copycode.gif)


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=" e n c o d i n g " 8 < 9 S t r i n g p a t h = r e q u e s t . g e t C o n t e x t P a t h ( ) ; 10 S t r i n g b a s e P a t h = r e q u e s t . g e t S c h e m e ( ) + " : / / " + r e q u e s t . g e t S e r v e r N a m e ( ) + " : " + r e q u e s t . g e t S e r v e r P o r t ( ) + p a t h + " / " ; 111213 < ! D O C T Y P E H T M L P U B L I C " − / / W 3 C / / D T D H T M L 4.01 T r a n s i t i o n a l / / E N " > 14 < h t m l > 15 < h e a d > 16 < b a s e h r e f = " < 1718 < t i t l e > M y J S P ′ 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 ' encoding"8<9Stringpath=request.getContextPath();10StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";111213<!DOCTYPEHTMLPUBLIC"//W3C//DTDHTML4.01Transitional//EN">14<html>15<head>16<basehref="<1718<title>MyJSPtitle’ starting page
19
20 #parse( “templates/jsp/JSPMetaTags.vtl” )
21
22
23
24 This is my JSP page.

25
26



![复制代码](http://common.cnblogs.com/images/copycode.gif)


   这是Jsp.vtl的模板原始代码,这个模板的代码对于我来说不太符合项目开发中的实际情况,需要修改,修改后的模板如下:




![复制代码](http://common.cnblogs.com/images/copycode.gif)


1 #*---------------------------------------------#
2 # Template for a JSP
3 # @version: 1.2
4 # @author: 孤傲苍狼
5 #---------------------------------------------#
6 *#<%@ page language=“java” pageEncoding=“UTF-8”%>
7
8
9
10
11
12
13
14
15
16



![复制代码](http://common.cnblogs.com/images/copycode.gif)


   为了避免覆盖原来的Jsp.vtl模板,将修改后的Jsp.vtl模板重命名,例如重命名成gacl.vtl。


   **3.将gacl.vtl模板复制,然后粘贴到com.genuitec.eclipse.wizards\_9.0.0.me201108091322.jar包里面的templates\jsp文件夹里。**


   ![](http://images.cnitblog.com/i/289233/201408/091243360223989.gif)


   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修改后的内容如下图所示:


### 总结

* 框架原理真的深入某一部分具体的代码和实现方式时,要多注意到细节,不要只能写出一个框架。

* 算法方面很薄弱的,最好多刷一刷,不然影响你的工资和成功率😯

* 在投递简历之前,最好通过各种渠道找到公司内部的人,先提前了解业务,也可以帮助后期优秀 offer 的决策。

* 要勇于说不,对于某些 offer 待遇不满意、业务不喜欢,应该相信自己,不要因为当下没有更好的 offer 而投降,一份工作短则一年长则 N 年,为了幸福生活要慎重选择!!!

  **[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**

![](https://img-blog.csdnimg.cn/img_convert/6c250b6200355d0edce85b970db267bd.png)

喜欢这篇文章文章的小伙伴们点赞+转发支持,你们的支持是我最大的动力!



  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值