Servlet簡介和ServletContext
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);
}
}

浙公網安備 33010602011771號