您好,登錄后才能下訂單哦!
這篇文章主要介紹“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í)吧!
File > new > Project…
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)。
接下來(lái)一路Next 然后 Finish 完成創(chuàng)建。
創(chuàng)建完成后如下圖所示:
在 main 目錄上右擊,選擇 New Folder
將 java 目錄標(biāo)記為 Source
同樣的我們可以再來(lái)創(chuàng)建一個(gè) resource 文件夾,標(biāo)記為 resource 類(lèi)型:
完成之后:
首先引入 Servlet 需要的依賴(lài):
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>
編寫(xiě) MyServlet 繼承 HttpServlet 實(shí)現(xiàn) service 方法:
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>
配置 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>
直接看圖:
訪(fǎng)問(wèn):http://localhost:8080/MyServlet?password=1111&username=222
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í)用的文章!
免責(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)容。