IDEA好用的Servlet模板

不会再IDEA中创建模板点击这里看教程


这是模板内容,直接创建自己的模板复制用即可

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
#set( $packageName1 = "#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != '')${PACKAGE_NAME}.#end#parse('File Header.java')" )
#set( $packageName2 = "#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != '')${PACKAGE_NAME}/#end#parse('File Header.java')" )

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @auther LiuWeirui
 * @date ${DATE} ${TIME}
 */
 //更改@WebServlet中value的值,可以修改访问该Servlet文件的名称,规范value = "/visit name"
@WebServlet(name = "${NAME}", value = "/${NAME}")
public class ${NAME} extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public ${NAME}() {
        super();
    }

    /**
     * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
*

* 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 { doPost(request, response); } /** * The doPost method of the servlet.
*

* 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 { //设置请求和响应数据的编码 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //页面内容 out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } /** * web.xml配置文件书写 * * ${NAME} * $packageName1${NAME} * * * ${NAME} * /$packageName2${NAME} * */ }

 

优点:

1.设置好的请求和响应数据的编码

IDEA好用的Servlet模板_第1张图片

2.处理好的doGet()和doPost()方法

3.记录日期和创建者(创建者自己改,这里我用的本人的)

IDEA好用的Servlet模板_第2张图片

4.导入好的包和设置好的包名

IDEA好用的Servlet模板_第3张图片

5.设置好的@WebServlet属性,设置@WebServlet属性可以替代配置web.xml

6.设置好的配置web.xml文件的内容,复制好即可用

IDEA好用的Servlet模板_第4张图片

 

问题:

1.web配置文件的内容有些问题,如图:

这是正常情况,在包下创建文件

IDEA好用的Servlet模板_第5张图片

这是缺省状态下创建的文件

IDEA好用的Servlet模板_第6张图片

 

的内容在缺省状态下会出现PACKAGE_NAME.,这是以下这段代码导致的问题

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != '')${PACKAGE_NAME}.#end#parse('File Header.java')

这段代码貌似不能出现在注释内容里,不然就会出问题,在包中创建正常显示,但在缺省状态下就会显示PACKAGE_NAME

使用前还需自己修改


如各位有解决办法,请务必留言或私信,谢谢各位了,我花了一天时间都没有解决他这个问题,这是IDEA模板里的相关问题

 

你可能感兴趣的:(idea,servlet,idea,intellij,idea,web.xml)