Java學習路線-48:Servlet學習
課時1 Servlet是什么
Servlet作用是處理請求
接收請求
處理請求
完成響應
課時2 實現(xiàn)Servlet方式
依賴
1
2
3
4
5
6
實現(xiàn)方式
// 實現(xiàn)接口: javax.servlet.Servlet // 繼承類: javax.servlet.GenericServlet // 繼承類: javax.servlet.HttpServlet
1
2
3
4
5
6
7
8
繼承示例
AServlet.java
import javax.servlet.*; import java.io.IOException; public class AServlet implements Servlet{ // 創(chuàng)建時執(zhí)行 @Override public void init(ServletConfig servletConfig) throws ServletException { } // 獲取配置信息 @Override public ServletConfig getServletConfig() { return null; } // 處理請求 @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } // 獲取servlet信息 @Override public String getServletInfo() { return null; } // 銷毀前調(diào)用 @Override public void destroy() { } }
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
32
33
34
35
36
課時3 Servlet的生命周期
生命周期
init 實例化調(diào)用 service 每次處理請求都調(diào)用 destroy 銷毀調(diào)用
1
2
3
特性
(1)單例,每個類只有一個對象
(2)線程不安全,效率最高
Servlet類由用戶自定義,對象由服務器來創(chuàng)建,并有服務器調(diào)用對應的方法
瀏覽器訪問Servlet
給Servlet配置一個路徑
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
目錄結(jié)構(gòu)
webapp └── WEB-INF ├── classes │ └── AServlet.class └── web.xml
1
2
3
4
5
課時4 ServletConfig介紹
interface ServletConfig{ // 獲取servlet-name中的內(nèi)容 String getServletName() // 獲取Servlet上下文對象 ServletContext getServletContext() // 獲取指定初始化參數(shù)值 String getInitParameter(String name) // 獲取所有初始化參數(shù)值 Enumeration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
獲取參數(shù)
@Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println(servletConfig.getInitParameter("name")); }
1
2
3
4
課時5 ServletRequest和Servletresponse對象
interface ServletRequest; interface ServletResponse;
1
2
3
課時6 GenericServlet介紹
GenericServlet可以只覆寫service方法,不用全寫
基本原理
import javax.servlet.*; import java.io.IOException; public class AServlet implements Servlet { private ServletConfig config = null; @Override public void init(ServletConfig servletConfig) { this.config = servletConfig; this.init(); } // 實現(xiàn)此方法,初始化后可以調(diào)用 public void init() { } @Override public ServletConfig getServletConfig() { return this.config; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
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
32
33
34
35
36
37
38
使用示例
import javax.servlet.*; import java.io.IOException; public class AServlet extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException { servletResponse.getWriter().write("
hello"); } }
1
2
3
4
5
6
7
8
9
10
11
課時7 HttpServlet介紹
源碼
package javax.servlet.http; public abstract class HttpServlet extends GenericServlet { // 重寫 protected void doGet(HttpServletRequest req, HttpServletResponse resp); // 重寫 protected void doPost(HttpServletRequest req, HttpServletResponse resp); protected void service(HttpServletRequest req, HttpServletResponse resp); public void service(ServletRequest req, ServletResponse res){ if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; this.service(request, response); } else { throw new ServletException("non-HTTP request or response"); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
使用示例: 接收GET請求
import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class AServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("
hello
"); } }1
2
3
4
5
6
7
8
9
10
11
12
13
課時8 Servlet的細節(jié)
1、非線程安全
一個Servlet類只有一個實例對象,不是線程安全的,工作效率高
(1)不要在Servlet中創(chuàng)建成員,創(chuàng)建局部變量即可
(2)可以創(chuàng)建無狀態(tài)成員
(3)可以創(chuàng)建有狀態(tài)的成員,但是狀態(tài)必須為只讀
2、創(chuàng)建時啟動
默認情況下,服務器會在第一次訪問Servlet時創(chuàng)建實例對象
給load-on-startup設置一個非負整數(shù)
正數(shù)的值越小,啟動該servlet的優(yōu)先級越高
可以配置創(chuàng)建時啟動
1
2
3
4
3、url-pattern
可以有多個訪問路徑
可以使用前綴或者后綴通配符*
1
2
3
4
5
6
7
8
9
課時9 在conf下的web.xml文件內(nèi)容介紹
${CATALINA_HOME}/conf/web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
課時10 Servlet與反射
Servlet通過反射,獲取配置文件的類名,進行實例化和方法調(diào)用
課時11 ServletContext概述
一個項目只有一個ServletContext對象
可以在多個Servlet中傳遞對象
Tomcat啟動時創(chuàng)建,關(guān)閉時銷毀
課時12 獲取ServletContext對象
獲取ServletContext
class ServeletConfig{ ServletContext getServletContext(); } class GenericServlet{ ServletContext getServletContext(); }
1
2
3
4
5
6
7
8
課時13 演示ServletContext
域?qū)ο? 在多個Servlet中傳遞數(shù)據(jù)
class ServletContext{ // 設置 多次調(diào)用會覆蓋 void setAttribute(String name, Object vlaue); // 獲取 Object getAttribute(String name); // 移除 void removeAttribute(String name); // 獲取全部屬性名稱 Enumeration getAttributeNames(); }
1
2
3
4
5
6
7
8
9
10
11
例如: AServlet的數(shù)據(jù)可以傳遞到BServlet
AServlet.java
package com.pengshiyu; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class AServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String name = request.getParameter("name"); ServletContext context = getServletContext(); context.setAttribute("name", name); response.getWriter().write("
" + name +"
"); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BServlet.java
package com.pengshiyu; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class BServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { ServletContext context = getServletContext(); String name = (String)context.getAttribute("name"); response.getWriter().println(name); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
課時14 ServletContext獲取公共的初始化參數(shù)
Servlet只能獲取自己的初始化參數(shù)
ServletContext獲取公共的初始化參數(shù)
1
2
3
4
5
6
package com.pengshiyu; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class BServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { ServletContext context = getServletContext(); String name = context.getInitParameter("key"); response.getWriter().println(name); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
課時15 ServletContext獲取資源相關(guān)方法
以webapp文根目錄
ServletContext context = getServletContext(); // 獲取絕對路徑 String path = context.getRealPath("/index.html"); // 獲取文件后轉(zhuǎn)為輸入流 InputStream in = context.getResourceAsStream("/index.html"); // 獲取目錄下的文件 Set
1
2
3
4
5
6
7
8
9
10
11
課時16 網(wǎng)站訪問量統(tǒng)計小案例
使用ServletContext對象共享統(tǒng)計數(shù)據(jù)
package com.pengshiyu; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class BServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { ServletContext context = getServletContext(); Integer count = (Integer) context.getAttribute("count"); if (count == null) { count = 1; // 第一次訪問 } else { count++; } response.setContentType("text/html; charset=UTF-8"); response.getWriter().println("
第"+count+"次訪問
"); context.setAttribute("count", count); } }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
課時17 獲取類路徑下的資源
依賴
1
2
3
4
5
代碼實例
package com.pengshiyu; import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Demo { public static void main(String[] args) throws IOException { Class
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
課時18 BaseServlet
一個Servlet中可以有多個請求處理方法
BaseServlet.java
package com.pengshiyu; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; public class BaseServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String methodName = req.getParameter("method"); if(methodName == null || methodName.trim().isEmpty()){ throw new RuntimeException("method is null or empty"); } System.out.println(methodName); // 通過參數(shù)獲取方法 Class clazz = this.getClass(); Method method = null; try { method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); } catch (NoSuchMethodException e) { throw new RuntimeException("方法獲取失敗"); } // 調(diào)用方法 try { method.invoke(this, req, resp); } catch (Exception e) { throw new RuntimeException("調(diào)用失敗"); } } }
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
32
33
34
35
36
37
38
39
40
AServlet.java
package com.pengshiyu; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class AServlet extends BaseServlet { public void getAge(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().print("getAge"); } public void getName(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().print("getName"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
調(diào)用時傳入查詢參數(shù)
http://localhost:8080/demo/hello?method=getAge
Java Servlet
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。