淺談IDEA實用的Servlet模板
一、前言
二、這是模板內(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的值,可以修改訪問該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è)置請求和響應(yīng)數(shù)據(jù)的編碼
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//頁面內(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配置文件書寫
* <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)點
1.設(shè)置好的請求和響應(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ù)制好即可用

四、問題
web配置文件的內(nèi)容有些問題,如圖:
這是正常情況,在包下創(chuàng)建文件

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

<servlet-class>的內(nèi)容在缺省狀態(tài)下會出現(xiàn)PACKAGE_NAME.,這是以下這段代碼導(dǎo)致的問題
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != '')${PACKAGE_NAME}.#end#parse('File Header.java')
這段代碼貌似不能出現(xiàn)在注釋內(nèi)容里,不然就會出問題,在包中創(chuàng)建正常顯示,但在缺省狀態(tài)下就會顯示PACKAGE_NAME
使用前還需自己修改
到此這篇關(guān)于淺談IDEA實用的Servlet模板的文章就介紹到這了,更多相關(guān)實用的Servlet模板內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Servlet入門級設(shè)置(超詳細(xì) IDEA2020版)
- IDEA創(chuàng)建Servlet并配置web.xml的實現(xiàn)
- IDEA Servlet 模板設(shè)置的方法
- 詳解如何使用IntelliJ IDEA新建一個Servlet項目
- 解決IDEA誤刪out目錄下的文件導(dǎo)致404無法訪問的問題
- 解決idea中maven項目無端顯示404錯誤的方法
- IDEA創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題
- IDEA JavaWeb項目啟動運行后出現(xiàn)404錯誤的解決方法
- 解決idea導(dǎo)入ssm項目啟動tomcat報錯404的問題
- IDEA下Servlet可能出現(xiàn)404的一些情況
相關(guān)文章
打開IDEA配置Spring項目時發(fā)現(xiàn)沒有選擇java?1.8的選項解決方案
這篇文章主要介紹了打開IDEA配置Spring項目時發(fā)現(xiàn)沒有選擇java?1.8的選項的解決方案,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者idea具有一定的參考借鑒價值,需要的朋友可以參考下2025-03-03
如何通過SpringBoot實現(xiàn)商城秒殺系統(tǒng)
這篇文章主要介紹了如何通過SpringBoot實現(xiàn)商城秒殺系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
關(guān)于Java錯誤提示之找不到或無法加載主類的問題及正確處理方法
當(dāng)我們在初學(xué)Java的是時候,類文件中是不設(shè)定包名(package)的,這種情況下注意classpath,基本上沒有問題,?本文主要說明classpath和系統(tǒng)環(huán)境變量PATH都沒問題的情況下出錯原因和正確處理方法,感興趣的朋友一起看看吧2022-01-01
SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn),現(xiàn)在很多創(chuàng)業(yè)公司都開始往springcloud靠了,可能是由于文檔和組件比較豐富的原因吧,畢竟是一款目前來說比較完善的微服務(wù)架構(gòu)2022-11-11

