Servlet簡介和ServletContext
阿新 • • 發佈:2021-01-01
0x01: 什麼是Servlet?
- 是sun公司開發動態web的技術
- 實現了servlet介面的Java程式
0x02: Servlet的實現類有哪些?
Servlet介面預設有兩個實現類
- HttpServlet
- GenericServlet
HttpServlet 繼承自 GenericServlet, 一般我們自己寫類只需要繼承HttpServlet,重寫方法就可以了
public class HelloServlet extends HttpServlet { //由於get或者post只是請求實現的不同的方式,可以相互呼叫,業務邏輯都一樣; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //ServletOutputStream outputStream = resp.getOutputStream(); PrintWriter writer = resp.getWriter(); //響應流 writer.print("Hello,Serlvet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp);
0x03:Servlet的原理
瀏覽器傳送http請求給web容器,web容器產生Request和Response物件,這兩個物件呼叫servlet介面的Service方法,Service方法再將返回的Responce資訊返回給Responce物件,web容器從Responce物件讀取將其響應給客戶端。(注意,如果web容器是首次被訪問,需要先把我們的java類變成class位元組碼檔案)
什麼是ServletContext
Web容器在啟動時,為每個web程式建立一個對應的ServletContext,它代表當前web應用
ServletContext的一些用法和特性
在同一個web應用中,不同的servlet可以共享資料
public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //this.getInitParameter() 初始化引數 //this.getServletConfig() Servlet配置 //this.getServletContext() Servlet上下文 ServletContext context = this.getServletContext(); String username = "黎星澄"; //資料 context.setAttribute("username",username); //將一個數據儲存在了ServletContext中,名字為:username 。值 username } }
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
獲取初始化引數
<!--在web.xml配置一些web應用初始化引數-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url"); //得到name為url的引數
resp.getWriter().print(url);
}
請求轉發
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("進入了ServletDemo04");
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/xxx"); //轉發的請求路徑
//requestDispatcher.forward(req,resp); //呼叫forward實現請求轉發;
context.getRequestDispatcher("/gp").forward(req,resp);
}
//請求轉發和重定向的區別?
請求轉發:像當於在伺服器內部將請求轉給請求的資源,然後由伺服器相應給客戶端
重定向:伺服器將請求資源作為響應資訊響應給客戶端,客戶端根據這個資訊對伺服器發起請求
讀取資原始檔
在java目錄下新建properties
在resources目錄下新建properties
都被打包到同一路徑:classes
username=admin
password=123456
public class Servlet_Properties extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hu/servlet/test.properties"); //輸入流
Properties prop = new Properties(); //建立一個Properties物件
prop.load(is); //將讀取到的載入到prop裡
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}