標籤:list html web.xml 服務 自己 htm 通過 span pack
因為最近在研究公司一套新的架構,發現這套架構的底層是對Struts2,Spring 封裝後的WEB應用程式框架,而我發現如果僅僅是利用這個架構開發,確實很容易快速上手,做業務來說是沒有問題的,但我覺得如果只對上層如何去用熟悉是不行,必須要學習其底層是如何玩的,而任何一套WEB應用程式框架的開發,肯定都是基於Servlet 對象中各個方法的生命週期來進行的,因此對Servlet的研究是有必要,雖然以前學過,但是很多原理都遺忘了,為此決定重新學習一下。
本人的開發工具和環境是:Myeclipse + Tomcat 6.0.45 + JDK1.6 (PS:因為自己的這台筆記本有點老了,所以很多東西都沒有更新了,等搬進新房一定換個蘋果,O(∩_∩)O哈哈~),言歸正傳吧。
一、Servlet 簡介
我們知道Servlet 是sun公司(現已被oracle收購了)提供的一門用於開發動態web資源的技術。如果使用者想要開發一個動態web資源或者說想開發一個java程式,此程式可以在瀏覽器輸出顯示出來,那麼需要經過2個步驟:
1)編寫一個JAVA類,實現Sun公司對外提供的一個API介面,servlet介面。
2)把開發好的這個JAVA類,部署到WEB伺服器中
經過上面兩步,我們就完成了web動態資源的開發。
PS:另外,我們經常說的Servlet容器就是能夠運行Servlet的環境叫Servlet容器,如:Tomcat, Web容器:能夠運行web應用的環境叫做web容器,如Tomcat.
二、在MyEclipse 中開發Servlet
1)因為我建立了一個工作空間,因此切換到這個新的工作空間之後,原來的所有的配置全部清零了,因此重新設定如下。
2)配置JDK
3)配置編碼,建議選擇 UTF-8. 選擇功能表列Windows--preferences,選擇標籤General-->Workspace。
4)指定啟動並執行Tomcat
選擇功能表列Window-->Preferences,選擇標籤MyEclipse-->Servers-->Tomcat,然後:
5)檢查tomcat啟動並執行JDK,並閉MyEclipse內建Tomcat
單擊my eclipse tomcat ,然後右擊,配置tomcat.
6)經過上面的一系列配置,總於進入到建立web項目。
點擊finish,繼續:
選擇no,繼續,這樣我們的web工程就建立完成,我們看資源檢視。
7)WEB工程建立完成,接下來,我們需要建立Servlet.
Servlet介面SUN公司定義了兩個預設實作類別,分別為:GenericServlet 和 HttpServlet.
HttpServlet指的能夠處理HTTP請求的Servlet,它是繼承自GenericServlet ,在此介面上添加了一些與HTTP協議處理相關的方法,比GenericServlet 功能更強大,因此普遍使用這個。
綜合上述,實現Servlet的方式有三種:
1)實現servlet介面(偏向底層);2)繼承GenericServlet(覺得1不好用,就開發了這個,現在基本沒啥用);3)繼承HttpServlet(現在大多數用這個)
HttpServlet在實現Servlet介面的時候覆寫了service方法,在方法體類的代碼會自動判斷使用者的請求方式,如為GET請求,則會調用HttpServlet中的doGet方法,如果為Post請求,則會去調用doPost方法,因此,開發人員在編寫Servlet時候,通常只需要覆寫doGet 或 doPost 方法,而不小區覆寫service方法了。
下面我採用繼承HttpServlet ,建立我們的一個Servlet.
點擊finish完成。
這樣,我們就通過MyEclipse幫我們建立好一個名字為ServletDemo1的Servlet,建立好的ServletDemo01裡面會有如下代碼:
package mdj.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 ServletDemo01 extends HttpServlet {/** * Constructor of the object. */public ServletDemo01() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code here}}
我們在看下產生的web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletDemo01</servlet-name> <servlet-class>mdj.servlet.study.ServletDemo01</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo01</servlet-name> <url-pattern>/servlet/ServletDemo01</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
可以看到:web.xml檔案中也多了<servlet></servlet>和<servlet-mapping></servlet-mapping>兩對標籤,這兩對標籤是配置ServletDemo1的。
二、MyEclipse 發布Servlet 到 Servlet 容器中
前面我們說過,我們的目的就是開發一個java程式,然後讓其能夠在瀏覽器中顯示,從而證明Servlet 是一種開發動態web資源的技術。
因此現在我們開發部署這個Servlet.
點擊Add Deployment
點擊finish 完成。
發布之後,如果想在網頁上看效果,需要先運行Tomcat:
服務已經運行OK。
接下來我們訪問下這個Servlet,看是否能夠在瀏覽器顯示,訪問的地址即為:web應用程式名稱+servlet的URL(web.xml中)
我們發現已經訪問成功了,但訪問的內容是從哪來的呢,我們看下我們的自己實現的Servlet。
response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close();
我可以看到無論請求是get 還是 post 裡面都上述一段代碼,可以看到就是我們頁面顯示的內容,這就證明了Servlet 確實是一種實現動態web資源的一種技術。
下一節,將會將Servlet的運行過程和生命週期和一些相關的知識點.
web開發之Servlet 一