servlet是用java語(yǔ)言編寫的,是一個(gè)java類。主要功能是用來(lái)接受、處理客戶端的請(qǐng)求,并把處理結(jié)果返回到客戶端顯示。Jsp是servlet發(fā)展后期的產(chǎn)物。在沒(méi)有jsp之前,servlet利用輸出流動(dòng)態(tài)生成整個(gè)HTML頁(yè)面,輸出內(nèi)容包括每一個(gè)HTML標(biāo)簽和每個(gè)在HTML頁(yè)面中出現(xiàn)的內(nèi)容。HTML文件包含大量標(biāo)簽和大量靜態(tài)文本及格式等,以及所有的表現(xiàn)邏輯,包括布局、色彩及圖像等。這些內(nèi)容都必須耦合在java代碼中,這樣就導(dǎo)致servlet開(kāi)發(fā)效率低下,令人不勝其煩。jsp出現(xiàn)后彌補(bǔ)了不足,因?yàn)閖sp文件是通過(guò)在標(biāo)準(zhǔn)的HTML頁(yè)面中插入java代碼形成的。其靜態(tài)的部分無(wú)需java程序控制,只有那些需要從數(shù)據(jù)庫(kù)讀取并根據(jù)程序動(dòng)態(tài)生成信息時(shí),才使用java腳本控制。所以jsp技術(shù)出現(xiàn)后,主要用jsp文件來(lái)動(dòng)態(tài)生成HTML文件,然后返回客戶端顯示。現(xiàn)在的servlet,當(dāng)需要將整個(gè)頁(yè)面作為結(jié)果返回時(shí),不再由其自己去處理,而是調(diào)用jsp文件。
下面開(kāi)發(fā)部署一個(gè)簡(jiǎn)單的servlet程序來(lái)展示:
1.創(chuàng)建處理請(qǐng)求的servlet文件:
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
|
package com.servlet.study; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorldServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super .doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType( "text/html;charset=UTF-8" ); req.setCharacterEncoding( "UTF-8" ); String userName = req.getParameter( "username" ); String passWord = req.getParameter( "password" ); PrintWriter out = resp.getWriter(); out.print( "<html>" ); out.print( "<head>" ); out.print( "<title>Helloworld</title>" ); out.print( "</head>" ); out.print( "<body>" ); out.print( "<hr>" ); out.println( "The username is " +userName); out.println( "The password is " +passWord); out.print( "</body>" ); out.print( "</html>" ); } } |
2.創(chuàng)建HTML文件:
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
|
<!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >用戶登錄頁(yè)面</ title > </ head > < body > < h1 align = "center" >登錄系統(tǒng)</ h1 >< hr > < form action = "helloworld_servlet" method = "post" >//表單的action其實(shí)指明了servlet的url < table > < tr > < td >用戶名</ td > < td >< input type = "text" name = "username" ></ td > </ tr > < tr > < td >密碼</ td > < td >< input type = "password" name = "password" ></ td > </ tr > < tr > < td >< input type = "reset" value = "重填" ></ td > < td >< input type = "submit" value = "提交" ></ td > </ tr > </ table > </ form > </ body > </ html > |
3.在web.xml中配置servlet:
1
2
3
4
5
6
7
|
< servlet > < servlet-name >HelloWorldServlet</ servlet-name > < servlet-class >com.servlet.study.HelloWorldServlet</ servlet-class >//實(shí)現(xiàn)類4</ servlet > < servlet-mapping >//映射 < servlet-name >HelloWorldServlet</ servlet-name > < url-pattern >/helloworld_servlet</ url-pattern >//“/”是必須的 </ servlet-mapping > |
注:servlet類必須繼承HttpServlet類,而且得重寫doGet、doPost方法,并創(chuàng)建out對(duì)象。doGet方法是HttpServlet類中處理get請(qǐng)求的方法,doPost處理post請(qǐng)求。在表單中聲明method,并在servlet類中編寫相對(duì)應(yīng)方法即可,本例特為post請(qǐng)求。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/Miracle-Maker/p/6429544.html