浅谈IDEA实用的Servlet模板

网友投稿 287 2023-01-12

浅谈IDEA实用的Servlet模板

一、前言

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

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

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${http://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.qcRTtwjava')" )

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("

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.设置好的请求和响应数据的编码

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

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

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

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

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

四、问题

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

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

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

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

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

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

使用前还需自己修改

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 将Excel转为SVG的方法
下一篇:福建顺通快递物流查询单号(闽顺物流单号查询系统)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~