A Web Module That Uses JavaServer Faces Technology: The hello2 Example
hello2詳解
1.GreetingServlet.java(顯示問(wèn)候頁(yè)面表單)
此servlet重寫(xiě)該doGet方法,實(shí)現(xiàn)GETHTTP方法。servlet顯示一個(gè)簡(jiǎn)單的HTML問(wèn)候表單,其提交按鈕就像hello1指定其操作的響應(yīng)頁(yè)面一樣。以下摘錄以@WebServlet注釋開(kāi)頭,注釋指定相對(duì)于上下文根的URL模式:
1 package javaeetutorial.hello2;2 3 import java.io.IOException; //IOException表示發(fā)生某種I/O異常的信號(hào)。此類(lèi)是由失敗或中斷的I/O操作產(chǎn)生的一般異常類(lèi)。4 import java.io.PrintWriter; //io常用類(lèi),包裝流PrintWriter除了可以包裝字節(jié)流OutputStream之外,還能包裝字符流Writer。5 import javax.servlet.RequestDispatcher; //定義一個(gè)對(duì)象,該對(duì)象接收來(lái)自客戶(hù)端的請(qǐng)求,并將它們發(fā)送到服務(wù)器上的任何資源(例如servlet,HTML文件或JSP文件)。6 import javax.servlet.ServletException; //定義servlet在遇到困難時(shí)可以拋出的一般異常。7 import javax.servlet.annotation.WebServlet; //web服務(wù)中的,在Glassfish下lib中的包。8 import javax.servlet.http.HttpServlet; //提供要進(jìn)行子類(lèi)化的抽象類(lèi),以創(chuàng)建適用于Web站點(diǎn)的HTTP Servlet。9 import javax.servlet.http.HttpServletRequest; //擴(kuò)展ServletRequest接口以提供HTTP Servlet的請(qǐng)求信息。 10 import javax.servlet.http.HttpServletResponse; //擴(kuò)展ServletResponse接口以在發(fā)送響應(yīng)時(shí)提供特定于HTTP的功能。 11 12 /** 13 * This is a simple example of an HTTP Servlet. It responds to the GET method of 14 * the HTTP protocol. 15 */ 16 @WebServlet("/greeting") //設(shè)置標(biāo)注@webserverlet,容器會(huì)自動(dòng)讀取里面的信息。此標(biāo)注告訴容器,如果請(qǐng)求的UEL是“/greeting”,則由GreetingServelet的實(shí)例提供服務(wù)。 17 public class GreetingServlet extends HttpServlet { //創(chuàng)建一個(gè)公有類(lèi)GreetingServlet繼承父類(lèi)HttpServlet 18 19 @Override //覆蓋標(biāo)注,意思是下面覆蓋HttpServlet中的doGet方法 20 public void doGet(HttpServletRequest request, //參數(shù):—req- HttpServletRequest包含客戶(hù)端對(duì)servlet的請(qǐng)求的對(duì)象 21 HttpServletResponse response) //參數(shù):resp- HttpServletResponse包含servlet發(fā)送給客戶(hù)端的響應(yīng)的對(duì)象 22 throws ServletException, IOException { //拋出:java.io.IOException - 如果在servlet處理GET請(qǐng)求時(shí)檢測(cè)到輸入或輸出錯(cuò)誤;ServletException - 如果無(wú)法處理GET請(qǐng)求 23 24 response.setContentType("text/html"); //發(fā)送給客戶(hù)端的文章類(lèi)型 25 response.setBufferSize(8192); //發(fā)送給客戶(hù)端的響應(yīng)對(duì)象的緩沖大小是8192 26 try (PrintWriter out = response.getWriter()) { //獲取PrintWriter流,用來(lái)在客戶(hù)端輸出 27 out.println("<html lang=\"en\">" //以下是html標(biāo)記語(yǔ)言用來(lái)顯示頁(yè)面 28 + "<head><title>Servlet Hello</title></head>"); 29 30 // then write the data of the response 31 out.println("<body bgcolor=\"#ffffff\">" 32 + "<img src=\"resources/images/duke.waving.gif\" " 33 + "alt=\"Duke waving his hand\">" 34 + "<form method=\"get\">" 35 + "<h2>Hello, my name is Duke. What's yours?</h2>" 36 + "<input title=\"My name is: \" type=\"text\" " 37 + "name=\"username\" size=\"25\"/>" 38 + "<p></p>" 39 + "<input type=\"submit\" value=\"Submit\"/>" 40 + "<input type=\"reset\" value=\"Reset\"/>" 41 + "</form>"); 42 43 String username = request.getParameter("username"); //定義一個(gè)字符串username并對(duì)它賦從request中拿出名字叫userName的值 44 if (username != null && username.length() > 0) { //如果username不為空并且長(zhǎng)度大于0 45 RequestDispatcher dispatcher = 46 getServletContext().getRequestDispatcher("/response"); //獲取jsp上下文里邊存儲(chǔ)了各變量的信息(值),把一個(gè)命令發(fā)送到瀏覽器,讓瀏覽器對(duì)指定的URL提出請(qǐng)求(此處的URL只能使用絕對(duì)路徑) 47 48 if (dispatcher != null) { 49 dispatcher.include(request, response); //如果接收到的客戶(hù)端的請(qǐng)求不為空時(shí),記錄保留request和response,以后不能再修改response里表示狀態(tài)的信息 50 } 51 } 52 out.println("</body></html>"); 53 } 54 } 55 56 @Override //覆蓋 57 public String getServletInfo() { //getServletInfo()方法是一個(gè)可選的方法,它提供有關(guān)servlet的信息,如作者、版本、版權(quán) 58 return "The Hello servlet says hello."; //返回說(shuō)明這個(gè)servelet的信息是says hello 59 } 60 }?
詳細(xì)使用方法(來(lái)源于API文檔):protected void doGet(HttpServletRequest ?req,HttpServletResponse ?resp)拋出ServletException,java.io.IOException
重寫(xiě)此方法以支持GET請(qǐng)求也會(huì)自動(dòng)支持HTTP HEAD請(qǐng)求。HEAD請(qǐng)求是一個(gè)GET請(qǐng)求,它在響應(yīng)中不返回任何主體,只返回請(qǐng)求頭字段。
覆蓋此方法時(shí),請(qǐng)讀取請(qǐng)求數(shù)據(jù),編寫(xiě)響應(yīng)頭,獲取響應(yīng)的編寫(xiě)器或輸出流對(duì)象,最后編寫(xiě)響應(yīng)數(shù)據(jù)。最好包含內(nèi)容類(lèi)型和編碼。使用PrintWriter對(duì)象返回響應(yīng)時(shí),請(qǐng)?jiān)谠L問(wèn)PrintWriter對(duì)象之前設(shè)置內(nèi)容類(lèi)型?。
servlet容器必須在提交響應(yīng)之前寫(xiě)入標(biāo)頭,因?yàn)樵贖TTP中,標(biāo)頭必須在響應(yīng)主體之前發(fā)送。
在可能的情況下,設(shè)置Content-Length標(biāo)頭(使用?ServletResponse.setContentLength(int)方法),以允許servlet容器使用持久連接將其響應(yīng)返回給客戶(hù)端,從而提高性能。如果整個(gè)響應(yīng)適合響應(yīng)緩沖區(qū),則自動(dòng)設(shè)置內(nèi)容長(zhǎng)度。
使用HTTP 1.1分塊編碼(這意味著響應(yīng)具有Transfer-Encoding標(biāo)頭)時(shí),請(qǐng)不要設(shè)置Content-Length標(biāo)頭。
GET方法應(yīng)該是安全的,即沒(méi)有任何副作用,用戶(hù)對(duì)此負(fù)責(zé)。例如,大多數(shù)表單查詢(xún)沒(méi)有副作用。如果客戶(hù)端請(qǐng)求旨在更改存儲(chǔ)的數(shù)據(jù),則該請(qǐng)求應(yīng)使用其他一些HTTP方法。
GET方法也應(yīng)該是冪等的,這意味著它可以安全地重復(fù)。有時(shí)使方法安全也使其成為冪等的。例如,重復(fù)查詢(xún)既安全又冪等,但在線購(gòu)買(mǎi)產(chǎn)品或修改數(shù)據(jù)既不安全也不是冪等。
如果請(qǐng)求格式不正確,則doGet?返回HTTP“錯(cuò)誤請(qǐng)求”消息。
?
2.ResponseServlet.java(響應(yīng)頁(yè)面)
此servlet也覆蓋該doGet方法,僅顯示響應(yīng)。以下摘錄以@WebServlet?注釋開(kāi)頭,注釋指定相對(duì)于上下文根的URL模式:
1 import java.io.IOException;2 import java.io.PrintWriter;3 import javax.servlet.ServletException;4 import javax.servlet.annotation.WebServlet;5 import javax.servlet.http.HttpServlet;6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 /** 10 * This is a simple example of an HTTP Servlet. It responds to the GET 11 * method of the HTTP protocol. 12 */ 13 @WebServlet("/response") 14 public class ResponseServlet extends HttpServlet { 15 16 @Override 17 public void doGet(HttpServletRequest request, 18 HttpServletResponse response) 19 throws ServletException, IOException { 20 try (PrintWriter out = response.getWriter()) { 21 String username = request.getParameter("username"); //同上 22 if (username != null && username.length() > 0) { //如果username不為空且長(zhǎng)度大于0 23 out.println("<h2>Hello, " + username + "!</h2>"); //打印Hello username 24 } 25 } 26 } 27 28 @Override 29 public String getServletInfo() { 30 return "The Response servlet says hello."; 31 32 } 33 }?
轉(zhuǎn)載于:https://www.cnblogs.com/TomFord/p/10736228.html
總結(jié)
以上是生活随笔為你收集整理的A Web Module That Uses JavaServer Faces Technology: The hello2 Example的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 驱动调试 -> devcon faile
- 下一篇: 从底层重学 Java 之两大浮点类型 G