request:獲取請求資料
response:設定響應資料
ServletRequest——Java提供的請求物件根介面
HttpServletRequest——Java提供的對http協定封裝的請求物件介面
RequestFacade——tomcat實現定義類
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//String method() 獲取請求方式
String method = req.getMethod();
System.out.println(method);
//String getContextPath() 獲取虛擬目錄(專案存取路徑)
String contextPath = req.getContextPath();
System.out.println(contextPath);
//StringBuffer getRequestURL() 獲取URL(統一資源定位符)
StringBuffer url = req.getRequestURL();
System.out.println(url.toString());
//String getRequestURI() 獲取URI(統一資源識別符號)
String uri = req.getRequestURI();
System.out.println(uri);
//String getQueryString() 獲取請求引數(get方式)
String queryString = req.getQueryString();
System.out.println(queryString);
//------------
//獲取請求頭
String agent = req.getHeader("user-agent");
System.out.println(agent);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取post請求體:請求引數
// 1. 獲取字元輸入流
BufferedReader br = req.getReader();
// 2. 讀取資料
String line = br.readLine();
System.out.println(line);
}
}
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//get請求邏輯
// System.out.println("get...");
// 1.獲取所有引數的map集合
Map<String, String[]> map = req.getParameterMap();
for (String key : map.keySet()) {
//username:zhangsan
System.out.print(key+":");
//獲取值
String[] values = map.get(key);
for (String value : values) {
System.out.print(value + " ");
}
System.out.println();
}
System.out.println("----------");
// 2.根據key獲取引數值,陣列
String[] hobbies = req.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
// 3. 根據key獲取單個引數值
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//post請求邏輯
this.doGet(req,resp);
// System.out.println("post...");
// // 1.獲取所有引數的map集合
// Map<String, String[]> map = req.getParameterMap();
// for (String key : map.keySet()) {
// //username:zhangsan
// System.out.print(key+":");
//
// //獲取值
// String[] values = map.get(key);
// for (String value : values) {
// System.out.print(value + " ");
// }
//
// System.out.println();
// }
//
// System.out.println("----------");
//
// // 2.根據key獲取引數值,陣列
// String[] hobbies = req.getParameterValues("hobby");
// for (String hobby : hobbies) {
//
// System.out.println(hobby);
// }
//
// // 3. 根據key獲取單個引數值
// String username = req.getParameter("username");
// String password = req.getParameter("password");
// System.out.println(username);
// System.out.println(password);
}
}
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 中文亂碼問題解決方案
*/
@WebServlet("/req3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 解決亂碼 POST 獲取引數方式:getReader()讀取資料流
request.setCharacterEncoding("utf-8"); //設定字元輸入流的編碼
// 2. 獲取username
String username = request.getParameter("username");
System.out.println("解決亂碼前:"+username);
// 3. 解決亂碼 GET 獲取引數方式:getQueryString
// 亂碼原因:tomcat進行url解碼,預設的字元集ISO-8859-1
// // 3.1 先對亂碼資料進行編碼:轉為位元組陣列
// byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
// // 3.2 位元組陣列解碼
// username = new String(bytes, StandardCharsets.UTF_8);
username = new String(username.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
System.out.println("解決亂碼後"+username);
}
}
請求轉發(forward):一種在伺服器內部的資源跳轉方式。
請求轉發特點:
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 請求轉發
*/
@WebServlet("/req4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo4");
//儲存資料
request.setAttribute("msg","hello");
//請求轉發
request.getRequestDispatcher("/req5").forward(request,response);
}
}
req5
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/req5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo5");
//獲取資料
Object msg = request.getAttribute("msg");
System.out.println(msg);
}
}
重定向:一種資源跳轉的方式
resp1
package com.itheima.web.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1...");
// // 重定向
// // 1. 設定狀態碼 302
// response.setStatus(302);
// // 2. 設定響應頭 Location
// response.setHeader("Location","/tomcat-demo2/resp2");
// 簡化方式完成重定向
response.sendRedirect("/tomcat-demo2/resp2");
}
}
resp2
package com.itheima.web.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp2...");
}
}
重定向特點:
資源路徑問題(什麼時候加虛擬目錄)
瀏覽器使用:需要加虛擬目錄
伺服器端使用:不需要加虛擬目錄
動態獲取虛擬目錄
//動態獲取虛擬目錄
String contextPath = request.getContextPath();
response.sendRedirect(contextPath + "/resp2");
package com.itheima.web.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 響應字元資料:設定字元資料的響應體
*/
@WebServlet("/resp3")
public class ResponseDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//獲取字元輸出流
PrintWriter writer = response.getWriter();
//content-type
//response.setHeader("content-type","text/html");
writer.write("你好");
writer.write("<h1>aaa</h1>");
//細節:流不需要關閉
}
}
package com.itheima.web.response;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 響應位元組資料:設定位元組資料的響應體
*/
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 讀取檔案
FileInputStream fis = new FileInputStream("e://a.jpg");
// 2. 獲取response位元組輸出流
ServletOutputStream os = response.getOutputStream();
// 3. 完成流的copy
// byte[] buff = new byte[1024];
// int len = 0;
// while ((len = fis.read(buff)) != -1){
// os.write(buff,0,len);
// }
IOUtils.copy(fis,os);//使用工具類
fis.close();
}
}
pom檔案匯入工具類
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>