ServletContext
web容器在啟動的時候,它會為每個web程式都建立一個對應的ServletContext物件,它代表了當前的web應用;
1、共享資料
在這個Servlet中儲存了資料,就可以在另外一個servlet取到
首先將資料存入HelloServlet中,然後可以在另外一個Servlet類中取出
@WebServlet("/hello")
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 name = "逍遙子";//資料
context.setAttribute("name",name);//將一個資料儲存在了 ServletContext 中
System.out.println("資料已經存放!!!!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
@WebServlet("/getc")
public class ReadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//資料
ServletContext context = this.getServletContext();
String name = (String) context.getAttribute("name");
//響應,設定編碼
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2、獲取初始化引數
可以獲得在xml中初始化儲存的引數
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置一些web應用的初始化引數 -->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
</web-app>
@WebServlet("/gp")
public class ServletDeom03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//資料
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3、請求轉發
轉發並不會改變請求的路徑,重定向才會改變請求的路徑
轉發是間接獲取到資源,重定向是直接拿到資源
@WebServlet("/sd4")
public class Servletdemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//資料
System.out.println("進入了demo04頁面");
ServletContext context = this.getServletContext();
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//轉發路徑
//requestDispatcher.forward(req,resp);//呼叫forward實現請求轉發
context.getRequestDispatcher("/gp").forward(req,resp);//請求轉發
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4、讀取資原始檔
Properties
- 在java目錄下新建properties
- 在resource目錄下新建properties
發現:都被打包到了同一路徑下:classes,我們俗稱這個路徑為classpath
注:在java目錄下建立的properties檔案,若想匯出成功,還需配置xml檔案
<!-- 在build中配置resource,來防止我們資源匯出失敗的問題-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
讀檔案思路:需要一個檔案流;
@WebServlet("/sd5")
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 1、匯入properties流,根據專案的相對地址
* 2、建立properties物件
* 3、將剛剛的流載入到properties物件中
* 4、成功引入properties檔案
*/
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print("username = "+username);
resp.getWriter().print("password = "+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}