• [技巧篇]01.Servlet的优化模版代码


    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">
    /**
     * 处理Get请求的方法
     * 一般用于:跳转到添加,跳转到更新和删除操作,一般用于传递主键
     */    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
            //设置响应内容的类型和编码
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            
            //捕获异常使用
            try {
                //运行JS脚本
                //out.write("<script type='text/javascript'></script>");
                
                //解决路径问题,标准写法
                //请求转发--服务端请求
                //request.getRequestDispatcher("/文件或者文件夹").forward(request, response);
                //重定向--客户端请求        
                //response.sendRedirect(request.getContextPath()+"/文件或者文件夹");
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    </aw:method>
    
    <aw:method name="doPost">
        /**
         * 处理Post请求的编码
         * 一般用于:保存数据更新数据
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //设置POST请求编码
            request.setCharacterEncoding("UTF-8");
            
            //设置响应内容的类型和编码
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            
            //捕获异常使用
            try {
                //运行JS脚本
                //out.write("<script type='text/javascript'></script>");
                
                //解决路径问题,标准写法
                //请求转发--服务端请求
                //request.getRequestDispatcher("/文件或者文件夹").forward(request, response);
                //重定向--客户端请求        
                //response.sendRedirect(request.getContextPath()+"/文件或者文件夹");
                
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    </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">
        /**
         * Servlet鲁玫脢录禄炉:
         *        脰禄脢脟脭脷碌脷脪禄麓脦碌梅脫脙碌脛脢卤潞貌鲁玫脢录禄炉脪禄麓脦
         */
        public void init() throws ServletException {
            
        }
    
    </aw:method>
    
    <aw:method name="destroy">
        /**
         * Servlet脧煤禄脵:
         *        脰禄脢脟脭脷路镁脦帽脝梅鹿脴卤脮碌脛脢卤潞貌脧煤禄脵
         */
        public void destroy() {
            
        }
    
    </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>
  • 相关阅读:
    Nokia Qt SDK开发环境使用
    iPad不久将能使用Android系统
    原生IDE新军:JRuby阵营的RedCar,JavaScript阵营的Cloud9(自己写个IDE出来一定很复杂吧)
    手机产品的信息架构
    百万开发者拥戴!七大.NET著名开源项目
    Prism 4.0发布最终版,支持Windows Phone 7
    Qt Symbian 开发环境安装
    Qt 4.7.1 和 Mobility 1.1.0 已发布
    WSI闭关,这对WS*意味着什么?(个人觉得Web Services是个好东西)
    ERROR: unknown virtual device name解决方法
  • 原文地址:https://www.cnblogs.com/pangxiansheng/p/4567166.html
Copyright © 2020-2023  润新知