溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

發(fā)布時(shí)間:2021-06-26 09:07:07 來(lái)源:億速云 閱讀:350 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目”,在日常操作中,相信很多人在idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

1. new Project

File > new > Project…

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

2. 填寫(xiě) GroupID\ArtifactID

GroupID 是項(xiàng)目組織唯一的標(biāo)識(shí)符,實(shí)際對(duì)應(yīng)JAVA的包的結(jié)構(gòu),是main目錄里java的目錄結(jié)構(gòu)。

ArtifactID 是項(xiàng)目的唯一的標(biāo)識(shí)符,實(shí)際對(duì)應(yīng)項(xiàng)目的名稱(chēng),就是項(xiàng)目根目錄的名稱(chēng)。

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

接下來(lái)一路Next 然后 Finish 完成創(chuàng)建。

創(chuàng)建完成后如下圖所示:

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

3.創(chuàng)建 java 目錄

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目 

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

main 目錄上右擊,選擇 New Folder

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

java 目錄標(biāo)記為 Source

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

同樣的我們可以再來(lái)創(chuàng)建一個(gè) resource 文件夾,標(biāo)記為 resource 類(lèi)型:

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

完成之后:

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

4. 創(chuàng)建 Servlet

首先引入 Servlet 需要的依賴(lài):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    </dependency>

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

編寫(xiě) MyServlet 繼承 HttpServlet 實(shí)現(xiàn) service 方法:

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

public class MyServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //設(shè)置字符編碼
        request.setCharacterEncoding("utf8");
        //從 request 對(duì)象中獲取username,password
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        request.setAttribute("username",username);
        request.setAttribute("password",password);
        request.getRequestDispatcher( "/new.jsp").forward(request, response);;
    }
}

new.jsp

<html>
<body>
<h3>This is new Page</h3>
username: <%=request.getParameter("username") %>
<br>
password: <%=request.getParameter("password") %>
</body>
</html>

5. 配置 web.xml

配置 web.xml 后,才能讓別人調(diào)用:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>club.sscai.demo.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

</web-app>

6. 配置 Tomcat

直接看圖:

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目 

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目 

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

訪(fǎng)問(wèn):http://localhost:8080/MyServlet?password=1111&username=222

idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目

servlet成功處理請(qǐng)求響應(yīng),至此,maven創(chuàng)建項(xiàng)目成功

到此,關(guān)于“idea如何搭建可運(yùn)行Servlet的Web項(xiàng)目”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI