Java Web學習Day02 ServletContext物件
阿新 • • 發佈:2021-07-17
Java Web學習Day02 ServletContext物件
1.ServletContext
web容器在啟動的時候,它會為每個web程式都建立一個對應的ServletContext物件,它代表了我當前的web應用
1、共享資料
-
我在這個Servlet儲存的應用,可以在另一個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 System.out.println("Hello!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
//拿取資料 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); } }
<!--HelloServlet註冊--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.lantian.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!--GetServlet註冊--> <servlet> <servlet-name>getc</servlet-name> <servlet-class>com.lantian.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getc</servlet-name> <url-pattern>/getc</url-pattern> </servlet-mapping>
測試訪問結果:直接訪問getc會返回null 因為此時hello中setAttribute還會設定資料;訪問hello後再此訪問getc得到返回資料 名字藍天
2、獲取初始化引數
<!--配置一些web應用初始化引數-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<!--ServletDemo03註冊-->
<servlet>
<servlet-name>demo3</servlet-name>
<servlet-class>com.lantian.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //建立ServletContext物件
String url = context.getInitParameter("url"); //獲取初始化引數
resp.getWriter().print(url);
}
}
3、請求轉發
public class ServletDemo04 extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("進入方法Demo04");
/* RequestDispatcher redis = context.getRequestDispatcher("/demo3"); //轉發的請求路徑
redis.forward(req,resp); //呼叫forward實現請求轉發;
*/
context.getRequestDispatcher("/demo3").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4、讀取資原始檔
Properties
- 在java目錄下新建properties
- 在resources目錄下新建properties
發現: 都被打包到同一路徑下:classes, 稱這個路徑為 類路徑
思路: 需要一個檔案流
username=root
password=123456
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
InputStream is =this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print("使用者名稱: "+user+"\n"+"密碼:"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
訪問測試