轉發
調用HttpServletRequest
對象的方法
request.getRequestDispatcher("test.jsp").forward(req, resp);
重定向
調用HttpServletResponse
對象的方法
response.sendRedirect("loginsuccess.jsp");
轉發:URL沒有變化 重定向:URL會發生變化
轉發:請求1次 重定向:請求2次
轉發:服務器行爲 重定向:客戶端行爲
轉發: 直接傳遞請求數據 重定向: 必須經過session/application全局中間數據緩存
JSP
:只有當客戶端第一次請求JSP時,才須要將其轉換、編譯以及實例化
Servlet
: 只有當服務器啓動時(web.xml中配置load-on-startup=1,默認爲0)或者第一次請求該servlet時,纔會加載和實例化
ServletConfig、 ServletContext、 HttpServletRequest、 HttpServletResponse
ServletConfig:表明當前Servlet的配置信息(web.xml) 獲取ServletConfig方法: ServletConfig sc = this.getServletConfig(); ServletContext:表明當前Application 獲取ServletContext方法: ServletContext sc = this.getServletContext(); HttpServletRequest:表明請求信息 HttpServletResponse:表明響應信息
Tomcat 啓動時,
爲每一個web項目建立對應的ServletContext對象
ServletContext對象什麼時候銷燬?
第一種:把web應用移除;第二種:把Tomcat服務器中止
做用:是管理WEB資源,讀取資源文件等 (請不要使用java文件方式去讀取
)
2種方法: 一、使用ServletRequest對象 request.getRequestDispatcher(); 二、使用ServletContext對象 context.getRequestDispatcher(); 區別: ServletContext.getRequestDispatcher(String path)方法的參數必須以斜槓(/)開始, 被解釋爲相對於當前上下文根(context root)的路徑。 例如:/myservlet是合法的路徑,而../myservlet是不合法的路徑。 ServletRequest.getRequestDispatcher(String path)方法的參數不但能夠使相對於上下文根的路徑, 並且能夠是相對於當前Servlet的路徑。如/myservlet和myservlet都是合法的路徑。 若是路徑以斜槓(/)開始,則被解釋爲相對於當前上下文根的路徑; 若是沒有以斜槓(/)開始,則被解釋爲相對於當前Servlet的路徑。
一、context(表明整個應用均可以使用,servletContext) 二、request 三、session 四、page
方式1: InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/config/db.properties"); Properties props = new Properties(); props.load(in); 方式2: String path= this.getServletContext().getRealPath("/WEB-INF/config/db.properties"); FileInputStream fis = new FileInputStream(path); Properties props = new Properties(); props.load(fis);
方式1(類加載器 加載與讀) InputStream in = UserServlet.class.getClassLoader().getResourceAsStream("db.properties"); Properties props = new Properties(); props.load(in); 方式2 (類加載器 只加載 用傳統的方式讀) String path = UserServlet.class.getClassLoader().getResource("db.properties").getPath(); FileInputStream fis = new FileInputStream(path); Properties props = new Properties(); props.load(fis);
response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setHeader("Content-Type", "text/html"); String str = "中國"; OutputStream os = response.getOutputStream(); os.write("<meta charset=\"utf-8\" />".getBytes()); os.write(str.getBytes("utf-8"));
字符流
與字節流
的區別(字節流是字符流的基礎)
字節流應用更普遍:二進制數據(視頻、音頻、圖片、文本)
字符流應用更專一:文本(字符串)
獲取字節流: OutputStream os = response.getOutputStream(); 獲取字符流: PrintWriter pw = response.getWriter(); 設置服務器編碼方式: 字節: str.getBtyes("UTF-8") //告訴服務器使用UTF-8編碼字符 字符: response.setCharacterEncoding("utf-8"); //告訴服務器使用UTF-8編碼字符 設置客戶端編碼方式: response.setContentType("text/html;charset=utf-8");
response.setHeader("content-disposition", "attachment;filename=" + filename); 若是下載的是中文文件,上面這種寫法會出行亂碼而且下載不了,因此必須使用下面這種: response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
1、爲何要用URLEncoder
客戶端在進行網頁請求的時候,網址中可能會包含非ASCII碼形式的內容,好比中文。java
而直接把中文放到網址中請求是不容許的,因此須要用URLEncoder編碼地址,web
將網址中的非ASCII碼內容轉換成能夠傳輸的字符緩存
不會被編碼的內容服務器
1.大寫字母A-Zsession
2.小寫字母a-zapp
3.數字 0-9jsp
4.標點符 - _ . ! ~ * ' (和 ,)this
(只能獲取文件真實路徑)
String filePath = this.getServletContext().getRealPath("/WEB-INF/upload/xxx.jpg"); InputStream is = new FileInputStream(filePath);
OutputStream out = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while((len =is.read(buffer) ) > 0){ out.write(buffer, 0 ,len); }
若是須要先訪問Servlet再跳轉到Jsp的話,須要把JSP頁面放在WEB-INF中 若是JSP能夠直接訪問,那直接放置在WEB-INF目錄外層