亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談IDEA實(shí)用的Servlet模板

 更新時(shí)間:2021年05月28日 14:32:01   作者:neihan2021  
今天給大家分享一下IDEA實(shí)用的Servlet模板,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)小伙伴們很有幫助哦,需要的朋友可以參考下

一、前言

不會(huì)再I(mǎi)DEA中創(chuàng)建模板點(diǎn)擊這里看教程

二、這是模板內(nèi)容,直接創(chuàng)建自己的模板復(fù)制用即可

#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的值,可以修改訪問(wèn)該Servlet文件的名稱,規(guī)范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 {
        //設(shè)置請(qǐng)求和響應(yīng)數(shù)據(jù)的編碼
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //頁(yè)面內(nèi)容
        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配置文件書(shū)寫(xiě)
    * <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>
    */
}

三、優(yōu)點(diǎn)

1.設(shè)置好的請(qǐng)求和響應(yīng)數(shù)據(jù)的編碼

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

3.記錄日期和創(chuàng)建者(創(chuàng)建者自己改,這里我用的本人的)

4.導(dǎo)入好的包和設(shè)置好的包名

5.設(shè)置好的@WebServlet屬性,設(shè)置@WebServlet屬性可以替代配置web.xml

6.設(shè)置好的配置web.xml文件的內(nèi)容,復(fù)制好即可用

四、問(wèn)題

web配置文件的內(nèi)容有些問(wèn)題,如圖:

這是正常情況,在包下創(chuàng)建文件

這是缺省狀態(tài)下創(chuàng)建的文件

<servlet-class>的內(nèi)容在缺省狀態(tài)下會(huì)出現(xiàn)PACKAGE_NAME.,這是以下這段代碼導(dǎo)致的問(wèn)題

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

這段代碼貌似不能出現(xiàn)在注釋內(nèi)容里,不然就會(huì)出問(wèn)題,在包中創(chuàng)建正常顯示,但在缺省狀態(tài)下就會(huì)顯示PACKAGE_NAME

使用前還需自己修改

到此這篇關(guān)于淺談IDEA實(shí)用的Servlet模板的文章就介紹到這了,更多相關(guān)實(shí)用的Servlet模板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論