JavaWeb--核心之Servlet

Servlet簡介 

        Servlet是 運行在服務端的Java小程序,是sun公司提供一套規範(接口),用來處理客戶端請求、響應給瀏覽器的動態資源。但servlet的實質就是java代碼,經過java的API 動態的向客戶端輸出內容html

servlet規範:包含三個技術點java

1)servlet技術mysql

2)filter技術---過濾器web

3)listener技術---監聽器sql

請求響應過程

 

Servlet快速入門

實現步驟:小程序

1)建立類實現Servlet接口 瀏覽器

2)覆蓋還沒有實現的方法---重點實現service方法服務器

     在web.xml進行servlet的配置oracle

代碼實現:app

建立類:

 1 public class MyServlet implements Servlet{
 2     public void destroy() {
 3         System.out.println("destory正在運行...");
 4         
 5     }
 6     public ServletConfig getServletConfig() {
 7         return null;
 8     }
 9     public String getServletInfo() {
10         return null;
11     }
12     public void init(ServletConfig arg0) throws ServletException {
13         //1.獲取servlet名稱
14         String servletname=arg0.getServletName();
15         System.out.println(servletname);
16         //2.獲取初始化參數
17         String url=arg0.getInitParameter("url");
18         System.out.println(url);
19         //3.獲取Servletcontext對象
20         ServletContext servletcontext=arg0.getServletContext();
21         System.out.println("init正在運行...");
22         
23     }
24     public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
25         System.out.println("service 正在運行...");    
26     }
27 }

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>WEB02</display-name>
 4   <servlet>
 5     <servlet-name>MyServlet</servlet-name>
 6     <servlet-class>com.oracle.demo01.MyServlet</servlet-class>
 7     <init-param>//初始化參數
 8       <param-name>url</param-name>
 9       <param-value>mysql.jdbc...</param-value>
10     </init-param>
11     <load-on-startup>3</load-on-startup>//啓動Servlet的優先級--數值是優先級的值,值越小越優先執行
12   </servlet>
13   <servlet-mapping>
14     <servlet-name>MyServlet</servlet-name>
15     <url-pattern>/MyServlet</url-pattern>
16   </servlet-mapping>
17   <welcome-file-list>
18     <welcome-file>index.html</welcome-file>
19     <welcome-file>index.htm</welcome-file>
20     <welcome-file>index.jsp</welcome-file>
21     <welcome-file>default.html</welcome-file>
22     <welcome-file>default.htm</welcome-file>
23     <welcome-file>default.jsp</welcome-file>
24   </welcome-file-list>
25 </web-app>

瀏覽器:

運行結果:

int()方法

 service()方法

 

destory()方法

總結:

Servlet接口中的方法的生命週期爲:

1)init(ServletConfig config)

什麼時候執行:開啓服務器,servlet對象建立的時候執行,只執行一次

ServletConfig :表明的是該servlet對象的配置信息

2)serviceServletRequest request,ServletResponse response

什麼時候執行:每次請求都會執行,請求一次執行一次

ServletRequest :表明請求 認爲ServletRequest 內部封裝的是 http請求的信息

ServletResponse :表明響應 認爲要封裝的是響應的信息

3)destroy()

什麼時候執行:servlet銷燬的時候執行,stop關閉服務器的時候執行一次

1、servlet的配置

1.基本配置

 

 

2.缺省Servlet

能夠將url-pattern配置一個/,表明該servlet是缺省的servlet

什麼是缺省的servlet

當你訪問資源地址全部的servlet都不匹配時,缺省的servlet負責處理,沒有缺省的話會找到原始文件web.xml裏的缺省。

其實,web應用中全部的資源的響應都是servlet負責,包括靜態資源

3.歡迎頁面

特色:從最上面開始找,從上至下

2、ServletContext對象

1.什麼是Servletcontext對象

       ServletContext表明是一個web應用的環境(上下文)對象,ServletContext對象 內部封裝是該web應用的信息,一個web應用只有一個ServletContext對象。

特色:

(1)一個web應用只有一個ServletContext對象

(2)ServletContext對象的生命週期是:

   建立:該web應用被加載(服務器啓動或發佈web應用(前提,服務器啓動狀 態))

    銷燬:web應用被卸載(服務器關閉,移除該web應用)

2.怎麼得到ServletContext對象

(1)Servltet裏的init方法裏

(2)HttpServltet裏的doGet方法裏

 3.servletcontext的做用

(1)得到全局參數

1 //獲取全局參數
2 String url=servletContext.getInitParameter("url");

xml文件:

(2)得到web應用中任何資源的絕對路徑(重要 重要 重要)

1 //獲取web03工程下的資源絕對路徑(給一個相對路徑--服務器裏面的路徑,獲取一個絕對路徑)
2 String patha=servletContext.getRealPath("WEB-INF/classes/a.txt");
3 String pathb=servletContext.getRealPath("b.txt");
4 String pathc=servletContext.getRealPath("WEB_INF/c.txt");
  //d文件不能複製,在原工程裏面,在服務器文件夾裏沒有

(3)ServletContext是一個域對象(重要 重要 重要)

概念:存儲數據的區域就是域對象

特色:

 1.ServletContext域對象的做用範圍:整個web應(全部的web資源均可以隨意向 servletcontext域中存取數據,數據能夠共享)

 2.一個web項目就有一個servletcontext對象

域對象的通用的方法:

setAtrribute(String name,Object obj);

getAttribute(String name);

removeAttribute(String name);

//獲取ServletContext對象
ServletContext servletcontext=getServletContext();
//向域中傳值
servletcontext.setAttribute("name", "張三");
//從域中取值--值是obj須要轉型
String name=(String)servletcontext.getAttribute("name"); 

 

 

 

 

 HttpServlet

但在實際開發中,咱們不會直接去實現Servlet接口,由於那樣須要覆蓋的方法太多, 咱們通常建立類繼承HttpServlet

實現步驟:

1)建立類繼承HttpServlet類

2)覆蓋doGet和doPost

3)在web.xml中進行servlet的配置

相關文章
相關標籤/搜索