Modify MyEclipse default servlet and JSP code templates

Source: Internet
Author: User

First, modify the default template code for the servlet

When using MyEclipse to create a servlet, the servlet code generated from the default servlet template is as follows:

 1 package gacl.servlet.study; 2 3 Import java.io.IOException; 4 Import Java.io.PrintWriter; 5 6 Import Javax.servlet.ServletException; 7 Import Javax.servlet.http.HttpServlet; 8 Import Javax.servlet.http.HttpServletRequest;     9 Import javax.servlet.http.httpservletresponse;10 One public class Servletdefaulttemplatecode extends HttpServlet {12 13 /**14 * The Doget method of the servlet. <br>15 *16 * This method was called when a form have its tag value method equals to get.17 * 18 * @param request the request send by the client to the SERVER19 * @param response The response send by the server to th E client20 * @throws servletexception If an error occurred21 * @throws IOException If an error occurred22 *  /23 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> ");         . println ("

In the actual development, these generated code and comments are generally not available to us, each time we have to manually delete these comments and code, it is cumbersome, so you can adapt to the actual development of the servlet template code, to meet the actual development requirements of the template code. The following is an example of myeclipse 10 to modify the template code of a servlet

The steps are as follows: Locate the \common\plugins folder in the MyEclipse installation directory, for example: D:\MyEclipse10\Common\plugins, and then find Com.genuitec.eclipse.wizards_ 9.0.0.me201108091322.jar This jar file, in order to find Com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar this jar file conveniently, it is recommended to use " Searcheverything "such as the File Finder tool, as shown in:

  

  

  

With the compression tool open, Note that it is open not to unpack this jar package , as shown in:

  

  

Open com.genuitec.eclipse.wizards_9.0.0. Me201108091322.jar This jar file, you can see inside there is a templates folder, into the Templates folder, you can see there is a Servlet.java file, as shown in:

  

Open the Servlet.java file, you can see the template code inside:

  1 #---------------------------------------------# 2 # <aw:description>template for servlet</aw:description&  Gt 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> Ten <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" >/** * Constru ctor of the object. */Public <aw:className/> () () ()w:constructor> <aw:method name= "Doget" >/** * The Doget method of the servlet. <br> * + * This method was called when a form have its tag value method equals to get. * @param request the request send by the client to the server * @param response the response SE  nd by the server to the client, * @throws servletexception If an error occurred PNS * @throws IOException if an Error occurred * * * doget (httpservletrequest request, httpservletresponse response) Hrows servletexception, IOException {response.setcontenttype ("text/html"); PrintWriter out = re Sponse.getwriter (); Out.println (<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("

Modify the template to make changes according to your own situation, such as

  

Delete Doget and dopost inside the code and method comments, in the Dopost method call Doget, which is modified according to the actual situation of the template code, modified, save, restart MyEclipse, using MyEclipse to create the servlet, This is done with the template you just modified to generate the code is as follows:

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 public class Servletnewtemplatecode extends HttpServlet {One to ten public     void Doget (HttpServletRequest request, Http Servletresponse response)             throws Servletexception, IOException {}16] public     void DoPost ( HttpServletRequest request, HttpServletResponse response)             throws Servletexception, IOException {doget         ( request, response);     }21 22}
Second, modify the default JSP template

Also find Com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar This jar file, open with the compression tool, go to the templates\jsp folder, You can see the JSP templates that MyEclipse comes with, as shown in:

  

These JSP templates are myeclipse, we can also leaf out, according to the actual situation in the project development, create a JSP template, and then add to the JSP directory, the following steps:

  1, casually copy a Jsp template out (such as: JSP.VTL), copied to the system's desktop or the system's other disks for storage

  

  2, modify the copied template, using Notepad or EditPlus open JSP.VTL, you can see the JSP.VTL template code, as follows:

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 (); string basepath = Request.g Etscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%>12 DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >14 

This is JSP.VTL template original code, this template code for me is not quite in line with the actual situation in the project development, need to modify, the modified template is as follows:

1 #*---------------------------------------------# 2 # Template for a JSP 3 # @version: 1.2 4 # @author: Aloof and Pale Wolf 5 #--------- ------------------------------------# 6 *#<%@ page language= "java" pageencoding= "UTF-8"%> 7 <! DOCTYPE html> 8 

To avoid overwriting the original JSP.VTL template, rename the modified Jsp.vtl template, such as renaming to GACL.VTL.

  3. Copy the gacl.vtl template and paste it into the templates\jsp folder inside the Com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar package.

  

4. From the interface to view the extracted files, return to the root directory (i.e. the com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar directory), Locate the template configuration file Templates.xml, as shown in:
  
Similarly, copy the Templates.xml file to the desktop and use Notepad or editplus to open it for modification.

Modify the following: Add the following elements to the <templateLibrary>:

1 <template2         context= "com.genuitec.eclipse.wizards.jsp" 3         script= "TEMPLATES/JSP/GACL.VTL" 4         name= " gacl-jsp template "/>

which
1. TEMPLATES/JSP/GACL.VTL: The relative path for the newly added JSP template.
2, gacl-jsp Template: For MyEclipse in the name of the template to be identified, myeclipse the new JSP file by this name to select the corresponding template.
3, context= "com.genuitec.eclipse.wizards.jsp" This must exist, and with other JSP template settings, replication can.

Templates.xml the modified content as shown in the following:

  

5, after the completion of the modification, will Com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar Delete the Templates.xml file in the package, and then copy and paste the modified templates.xml to Com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar package, as shown in the following:

  

This is where our JSP templates are created.

6. Start MyEclipse, and then create a new JSP page, you can use our customized JSP page template, as shown in:

  

Through the above two ways, we in the development, we can according to our own development habits to customize the servlet and JSP template, for the development of more or less efficiency, not every time the myeclipse generated a lot of code to delete.

Modify MyEclipse default servlet and JSP code templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.