servlet是用java語言編寫的,是一個java類。主要功能是用來接受、處理客戶端的請求,并把處理結(jié)果返回到客戶端顯示。Jsp是servlet發(fā)展后期的產(chǎn)物。在沒有jsp之前,servlet利用輸出流動態(tài)生成整個HTML頁面,輸出內(nèi)容包括每一個HTML標(biāo)簽和每個在HTML頁面中出現(xiàn)的內(nèi)容。HTML文件包含大量標(biāo)簽和大量靜態(tài)文本及格式等,以及所有的表現(xiàn)邏輯,包括布局、色彩及圖像等。這些內(nèi)容都必須耦合在java代碼中,這樣就導(dǎo)致servlet開發(fā)效率低下,令人不勝其煩。jsp出現(xiàn)后彌補(bǔ)了不足,因?yàn)閖sp文件是通過在標(biāo)準(zhǔn)的HTML頁面中插入java代碼形成的。其靜態(tài)的部分無需java程序控制,只有那些需要從數(shù)據(jù)庫讀取并根據(jù)程序動態(tài)生成信息時,才使用java腳本控制。所以jsp技術(shù)出現(xiàn)后,主要用jsp文件來動態(tài)生成HTML文件,然后返回客戶端顯示?,F(xiàn)在的servlet,當(dāng)需要將整個頁面作為結(jié)果返回時,不再由其自己去處理,而是調(diào)用jsp文件。
下面開發(fā)部署一個簡單的servlet程序來展示:
1.創(chuà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 >用戶登錄頁面</ 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對象。doGet方法是HttpServlet類中處理get請求的方法,doPost處理post請求。在表單中聲明method,并在servlet類中編寫相對應(yīng)方法即可,本例特為post請求。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/Miracle-Maker/p/6429544.html