最近觀看 android 開發(fā)視頻,里面使用的集成開發(fā)工具為 eclipse 。使用 eclipse 可以很快捷的編寫 web 項(xiàng)目,而我使用的 androi studio 因?yàn)閷I(yè)就把建立其他工程的功能給閹割了。所以,不能忍受只能聽老師講而不能實(shí)際操作時(shí)望洋興嘆般的尷尬,我選擇了使用 intellij idea 來(lái)替代 eclipse 模擬網(wǎng)絡(luò)請(qǐng)求。下面結(jié)合一個(gè)簡(jiǎn)單網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn),來(lái)介紹 intellij idea 的使用。
首先當(dāng)然是下載 intellij idea 集成工具,這個(gè) google/baidu 一下,很容易就能獲得。
接下來(lái)配置 tomcat 服務(wù)器,以 mac 電腦為例,參考:mac上tomcat服務(wù)器安裝配置 。
然后打開 intellij idea ,選擇右邊的 java enterprise 項(xiàng)目類型,選擇剛裝的 tomcat 服務(wù)器,勾選 web application 選項(xiàng)。
新建工程
點(diǎn)選 next,輸入自定義工程名稱 demo:
工程
然后我們就能看到新建工程的全貌:
工程
至此,一個(gè) web 應(yīng)用工程的框架已經(jīng)做好。但是要順利部署到 tomcat 服務(wù)器,還需要我們添加處理服務(wù)的對(duì)象 servlet。點(diǎn)擊 src 文件夾,添加 servlet:
servlet
servlet 類中能看到默認(rèn)生成的 doget 和 dopost 方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcharacterencoding( "utf-8" ); response.setcontenttype( "text/html" ); response.getwriter().print( "收到 post 請(qǐng)求" ); string username = request.getparameter( "username" ); string pwd = request.getparameter( "password" ); if ( "admin" .equals(username) && "abc123" .equals(pwd)) { response.sendredirect( "/2.html" ); } } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcharacterencoding( "utf-8" ); //設(shè)置 response.setcontenttype( "text/html" ); string username = request.getparameter( "username" ); string pwd = request.getparameter( "password" ); if ( "admin" .equals(username) && "abc123" .equals(pwd)) { response.sendredirect( "/2.html" ); } } |
要想使用新建的 servlet 類,還需要在 web.xml 中進(jìn)行配置:
1
2
3
4
5
6
7
8
9
10
|
<web-app ...> <servlet> <servlet-name>servlet</servlet-name> <servlet- class >demo.servlet</servlet- class > </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app> |
其中 servlet-mapping 標(biāo)簽設(shè)置對(duì)外訪問(wèn)的路徑。
然后在 web 目錄下添加前端頁(yè)面文件,比如命名 1.html 作為起始頁(yè)面,2.html 作為跳轉(zhuǎn)的結(jié)果頁(yè)面。
頁(yè)面
在 1.html 中編輯頁(yè)面布局,設(shè)置 head 標(biāo)簽,在 body 標(biāo)簽中添加 form表單。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
<!doctype html> <html lang= "en" > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>myfirst</title> <script type= "text/javascript" > </script> </head> <body> <h1>登錄頁(yè)面(get)</h1> <form action= "/demo" method= "get" > <table> <tr> <td> 用戶名: </td> <td> <input type= "text" name= "username" > </td> </tr> <tr> <td> 密碼: </td> <td> <input type= "text" name= "password" type= "hidden" > </td> </tr> <tr> <td colspan= "2" style= "align-items: center" > <input type= "submit" value= "登錄" > </td> </tr> </table> </form> <h1>登錄頁(yè)面(post)</h1> <form action= "/demo" method= "post" > <table> <tr> <td> 用戶名: </td> <td> <input type= "text" name= "username" > </td> </tr> <tr> <td> 密碼: </td> <td> <input type= "text" name= "password" type= "hidden" > </td> </tr> <tr> <td colspan= "2" > <input type= "submit" value= "登錄" > </td> </tr> </table> </form> </body> </html> |
2.html中編輯頁(yè)面:
1
2
3
4
5
6
7
8
9
10
11
12
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body> <h1 style= "color: red" > 登錄成功?。?! </h1> </body> </html> |
最后點(diǎn)擊 debug 進(jìn)行運(yùn)行,部署到自己的 tomcat 服務(wù)器上:
debug
最后在瀏覽器輸入網(wǎng)址: http://localhost:8080/1.html ,就能訪問(wèn)我們部署的網(wǎng)站了。
網(wǎng)站
打開 chrome 的開發(fā)者工具,能夠看到發(fā)送請(qǐng)求的詳細(xì)情況:
發(fā)送請(qǐng)求
完工!
流程很簡(jiǎn)單,以后就可以使用 idea 來(lái)學(xué)習(xí)后端開發(fā)的基本知識(shí)了,比如可以在后端獲取提交的文件,對(duì)成功的請(qǐng)求進(jìn)行跳轉(zhuǎn),請(qǐng)求失敗時(shí)要告知客戶端等等,都可以進(jìn)行模擬,更多知識(shí)點(diǎn)等你來(lái)發(fā)現(xiàn)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/1784640be85d