使用IDEA創(chuàng)建Servlet程序的詳細步驟
在學習servlet過程中,參考的教程是用eclipse完成的,而我在練習的過程中是使用IDEA的,在創(chuàng)建servlet程序時遇到了挺多困難,在此記錄一下如何用IDEA完整創(chuàng)建一個servlet程序。
1.打開IDEA,創(chuàng)建一個普通的Java項目
2.給項目添加Framwork支持

可以看到我們的項目多了個web文件夾
3.配置項目,在WEB-INF下創(chuàng)建兩個文件夾分別是lib,classes,要自己準備好servlet-api.jar,把它放到lib下,然后打開項目設(shè)置
打開project structure
在上面的窗體,繼續(xù)將jar包添加進來
4.編寫servlet程序
新建一個servlet
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 簡述: * * @author:LiYansheng * @date:2021/08/11 22:56 * @version: */ @WebServlet(name = "DemoServlet") public class DemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設(shè)置網(wǎng)頁響應類型 response.setContentType("text/html"); response.getWriter().println("my first servlet code"); } }
5.在web.xml中添加映射
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>DemoServlet</servlet-name> <servlet-class>DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app>
6.運行程序
點擊這個配置tomcat服務(wù)器
啟動服務(wù)器
在瀏覽器輸入地址,就可以看到如下內(nèi)容了
這樣一個Servlet程序就用idea搭建運行出來了
到此這篇關(guān)于使用IDEA創(chuàng)建Servlet程序的詳細步驟的文章就介紹到這了,更多相關(guān)IDEA創(chuàng)建Servlet程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Failed to execute goal org...的解決辦法
這篇文章主要介紹了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06關(guān)于?Math.random()生成指定范圍內(nèi)的隨機數(shù)的公式推導問題
在 java 中,用于生成隨機數(shù)的 Math 方法 random()只能生成 0-1 之間的隨機數(shù),而對于生成指定區(qū)間,例如 a-b 之間的隨機數(shù),卻只能用相關(guān)計算公式,今天通過本文給大家介紹Math.random()生成隨機數(shù)的公式推導問題,感興趣的朋友一起看看吧2022-09-09springboot?filter配置多個時,執(zhí)行順序問題
這篇文章主要介紹了springboot?filter配置多個時,執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot后端服務(wù)重定向的實現(xiàn)示例
本文主要介紹了SpringBoot后端服務(wù)重定向的實現(xiàn)示例,通過重定向、路徑匹配、反向代理和直接調(diào)用Controller層接口等方法來實現(xiàn),感興趣的可以了解一下2025-01-01RocketMQ4.5.X 實現(xiàn)修改生產(chǎn)者消費者日志保存路徑
這篇文章主要介紹了RocketMQ4.5.X 實現(xiàn)修改生產(chǎn)者消費者日志保存路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07