浅谈IDEA实用的Servlet模板
作者:neihan2021
今天给大家分享一下IDEA实用的Servlet模板,文中有非常详细的图文介绍及代码示例,对小伙伴们很有帮助哦,需要的朋友可以参考下
一、前言
二、这是模板内容,直接创建自己的模板复制用即可
#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. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * <p> * 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. <br> * <p> * 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } /** * web.xml配置文件书写 * <servlet> * <servlet-name>${NAME}</servlet-name> * <servlet-class>$packageName1${NAME}</servlet-class> * </servlet> * <servlet-mapping> * <servlet-name>${NAME}</servlet-name> * <url-pattern>/$packageName2${NAME}</url-pattern> * </servlet-mapping> */ }
三、优点
1.设置好的请求和响应数据的编码
2.处理好的doGet()和doPost()方法
3.记录日期和创建者(创建者自己改,这里我用的本人的)
4.导入好的包和设置好的包名
5.设置好的@WebServlet属性,设置@WebServlet属性可以替代配置web.xml
6.设置好的配置web.xml文件的内容,复制好即可用
四、问题
web配置文件的内容有些问题,如图:
这是正常情况,在包下创建文件
这是缺省状态下创建的文件
<servlet-class>的内容在缺省状态下会出现PACKAGE_NAME.,这是以下这段代码导致的问题
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != '')${PACKAGE_NAME}.#end#parse('File Header.java')
这段代码貌似不能出现在注释内容里,不然就会出问题,在包中创建正常显示,但在缺省状态下就会显示PACKAGE_NAME
使用前还需自己修改
到此这篇关于浅谈IDEA实用的Servlet模板的文章就介绍到这了,更多相关实用的Servlet模板内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!