• 关于如何设置IDEA中的servlet的模板


    关于如何设置IDEA中的servlet的模板

    1. 点击左上角的File: Setting --> Editor --> File and Code Templates --> Other --> web -->Servlet Annotated Class.java

    2. 进行如图中的设置

      image-20200901213151465

    由于我已经设置了java文件的作者信息相关格式,所以不需要在这里面再加入文件头信息

    模板代码如下:

    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
    #parse("File Header.java")
    @javax.servlet.annotation.WebServlet(name = "${Entity_Name}", urlPatterns="/${Entity_Name}")
    public class ${Class_Name} extends javax.servlet.http.HttpServlet {
        @Override
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            // 处理post请求乱码问题
            request.setCharacterEncoding("utf-8");
            // 处理响应乱码问题:字节流需getBytes("UTF-8")
            response.setContentType("text/html;charset=utf-8"); 
        }
        
        @Override
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            doPost(request,response);
        }
    }
    
    1. 测试的效果如下:新建servlet时,选择 Create New Servlet

      image-20200901213510299

      这样生成的一个servlet的代码模板如下:

      package com.luwanglin.servlet;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      /**
       * @author luwanglin
       * @email 1769862620@qq.com
       * @Date 2020/9/1 21:51
       * @Version 1.0
       */
      @WebServlet(name = "HelloServlet", urlPatterns = "/HelloServlet")
      public class HelloServlet extends HttpServlet {
          @Override
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              // 处理post请求乱码问题
              request.setCharacterEncoding("utf-8");
              // 处理响应乱码问题:字节流需getBytes("UTF-8")
              response.setContentType("text/html;charset=utf-8");
          }
      
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              doPost(request, response);
          }
      }
      

    参考文献

    IDEA中修改servlet模板

    保持对优秀的热情
  • 相关阅读:
    POJ-3468 A Simple Problem with Integers(线段树、段变化+段查询、模板)
    CSUOJ-1978 LXX的图论题(最短路、Bellman-Ford判断负圈)
    CSU-1975 机器人搬重物(BFS)
    POJ-3522 Slim Span(最小生成树)
    POJ-1733 Parity game (并查集)
    libmysqlclient.so.16未找到方法
    python 不同目录间的模块调用
    python模块详解 logging
    python模块详解 re
    python模块详解 hashlib
  • 原文地址:https://www.cnblogs.com/luckforefforts/p/13642690.html
Copyright © 2020-2023  润新知