实用的Servlet模板 浅谈IDEA实用的Servlet模板
neihan2021 人气:0想了解浅谈IDEA实用的Servlet模板的相关内容吗,neihan2021在本文为您仔细讲解实用的Servlet模板的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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. <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
使用前还需自己修改
加载全部内容