ServletContext物件(4)、web專案中路徑使用、ServletConfig,ServletContext方法總結
一、ServletContext物件:
1、web專案中路徑使用:
package sram.path;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PathDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//目標資源: target.html
/**
* 思考: 目標資源是給誰使用的。
* 給伺服器使用的: / 表示在當前web應用的根目錄(webRoot下)
* 給瀏覽器使用的: / 表示在webapps的根目錄下
*/
/**
* 1.轉發
*/
request.getRequestDispatcher("/target.html").forward(request, response);
/**
* 2.請求重定向
*/
response.sendRedirect("/servlet_test2/target.html");
/**
* 3.html頁面的超連線href
*/
response.getWriter().write("<html><body><a href='/servlet_test2/target.html'>超連結</a></body></html>" );
/**
* 4.html頁面中的form提交地址
*/
response.getWriter().write("<html><body><form action='/servlet_test2/target.html'><input type='submit'/></form></body></html>");
}
}
2、ServletContext物件的核心API(作用)
6)java.lang.String getRealPath(java.lang.String path) –得到web應用的資原始檔
7)java.io.InputStream getResourceAsStream(java.lang.String path)
package sram.resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* . 表示相對路徑中的當前路徑代表,相對於java命令執行目錄。java執行命令在哪裡??
* 結論: 1、在java專案中,. 代表在java專案的根目錄下開始
* 2、在web專案中, . 代表在tomcat/bin目錄下開始,所以不能使用這種相對路徑。
*/
//讀取檔案。在web專案下不要這樣讀取。因為.表示在tomcat/bin目錄下
/*File file = new File("./src/db.properties");
FileInputStream in = new FileInputStream(file);*/
/**
* 使用web應用下載入資原始檔的方法
*/
/**
* 1. getRealPath讀取,返回資原始檔的絕對路徑
*/
/*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
System.out.println(path);
File file = new File(path);
FileInputStream in = new FileInputStream(file);*/
/**
* 2. getResourceAsStream() 得到資原始檔,返回的是輸入流
*/
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
//讀取資原始檔
prop.load(in);
String user = prop.getProperty("user");
String password = prop.getProperty("password");
System.out.println("user="+user);
System.out.println("password="+password);
}
}
在web專案中路徑不要再使用”.“了,”.“表示tomcat下的bin目錄,而web專案是在webapps目錄下的。
二、ServletConfig,ServletContext方法總結
1、ServletConfig物件
獲取servlet的初始化引數:
1)getInitParameter(“name”);
2)getInitParameterNames();
2、ServletContext物件
1)得到web應用路徑:
a)context.getContextPath();
b)request.getContextPath(); 等價於上面的程式碼
2)得到web應用引數:
a)context.getInitParameter(“name”);
b)context.getInitParameterNames();
3)域物件:
a)context.setAttribute(“name”,Object): 儲存資料
b)context.getAttribute(“name”) 得到資料
c)context.removeAttribue(“name”) 清除資料
4)轉發
a)context.getRequestDispatcher(“路徑”).forward(request,response);
b)request.getRequestDispacher(“路徑”).forward(request,response); 等價於上面的程式碼
5)得到web應用中的資原始檔
a)context.getRealPath(“路徑”)
b)context.getResourceAsStream(“路徑”);
ServletContext物件終於學完了,累死寶寶了!!!