Servlet簡介:java
Servlet 運行在服務端的Java小程序,是sun公司提供一套規範(接口),用來處理客戶端請求、響應給瀏覽器的動態資源。但servlet的實質就是java代碼,經過java的API 動態的向客戶端輸出內容。web
Servlet快速入門:小程序
實現步驟:瀏覽器
1.建立類實現Servlet接口服務器
2.覆蓋還沒有實現的方法---重點實現service方法oracle
3.在web.xml進行servlet的配置app
public void init(ServletConfig arg0) throws ServletException {
System.out.println("Servlet建立了");
}
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("Servlet被請求了");
}
public void destroy() {
System.out.println("Servlet銷燬了");
}url
<servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.oracle.demo01.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping>
public class MyServlet extends HttpServlet {spa
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello boss...");
}3d
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Servlet的API(生命週期):
init(ServletConfig config):servlet對象建立的時候執行
service(ServletRequest request,ServletResponse response):每次請求都會執行
destroy():servlet銷燬的時候執行
public void init(ServletConfig arg0) throws ServletException { System.out.println("Servlet建立了"); //獲取Servlet配置的初始化參數 System.out.println("初始化參數爲"+arg0.getInitParameter("name")); //獲取servlet名字 System.out.println(arg0.getServletName()); //獲取ServletContext System.out.println(arg0.getServletContext()); } public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("Servlet被請求了"); } public void destroy() { System.out.println("Servlet銷燬了"); }
Servlet基本配置:
url-pattern的配置方式:
1.徹底匹配:訪問的資源與配置的資源徹底相同才能訪問到。
2.目錄匹配:格式:/虛擬的目錄../* *表明任意。
3.擴展名匹配:格式:*.擴展名
注意:目錄匹配和擴展名匹配不要混用。
服務器啓動實例化Servlet配置
缺省Servlet
歡迎頁面:
ServletContext對象:
ServletContext表明是一個web應用的環境(上下文)對象,ServletContext對象 內部封裝是該web應用的信息,ServletContext對象一個web應用只有一個
ServletContext對象的生命週期:
建立:該web應用被加載(服務器啓動或發佈web應用(前提,服務器啓動狀 態))
銷燬:web應用被卸載(服務器關閉,移除該web應用)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext對象 ServletContext context=getServletContext(); //獲取相對於服務器的相對路徑獲取絕對路徑 System.out.println(context.getRealPath("WEB-INF/classses/a.txt")); System.out.println(context.getRealPath("b.txt")); System.out.println(context.getRealPath("WEB-INF/c.txt")); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext對象 ServletContext context=getServletContext(); //往ServletContext域中的值 context.setAttribute("name", "zhangsan"); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext對象 ServletContext context=getServletContext(); //往ServletContext域中的值 String name=(String)context.getAttribute("name"); response.getWriter().write(name); }