一、獲取請求參數通用方式
不論get還是post請求方式都可以使用下列方法來獲取請求參數
(1)String getParameter(String name):根據參數名稱獲取參數值
(2)String[] getParameterValues(String name):根據參數名稱獲取參數值的數組
類似:獲取複選框的值 hobby=xx&hobby=game
(3)Enumeration<String> getParameterNames():獲取所有請求的參數名稱
(4)Map<String,String[]> getParameterMap():獲取所有參數的map集合
@WebServlet("/requestDemo")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post 獲取請求參數
//根據參數名稱獲取參數值
String username = request.getParameter("username");
/* System.out.println(username);*/
//根據參數名稱獲取參數值的數組
String[] hobbies = request.getParameterValues("hobby");
/*for (String hobby : hobbies) {
System.out.println(hobby);
}*/
//獲取所有請求的參數名稱
Enumeration<String> parameterNames = request.getParameterNames();
/*while(parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = request.getParameter(name);
System.out.println(value);
System.out.println("----------------");
}*/
// 獲取所有參數的map集合
Map<String, String[]> parameterMap = request.getParameterMap();
//遍歷
Set<String> keyset = parameterMap.keySet();
for (String name : keyset) {
//獲取鍵獲取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get 獲取請求參數
/*
//由於獲取請求方式是通用的,所以這裏如果是Get方式,可以直接跳轉到 Post 執行
this.doPost(request,response);
}
}
* 中文亂碼問題:
* get方式:tomcat 8 已經將get方式亂碼問題解決了
* post方式:會亂碼
* 解決:在獲取參數前,設置request的編碼request.setCharacterEncoding("utf-8");
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
解決:
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設置流的編碼
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
二、 請求轉發
一種在服務器內部的資源跳轉方式
1. 步驟:
(1)通過request對象獲取請求轉發器對象:RequestDispatcher getRequestDispatcher(String path)
(2) 使用RequestDispatcher對象來進行轉發:forward(ServletRequest request, ServletResponse response)
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.print("requestDemo 被訪問了。。。");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/servletDemo");
requestDispatcher.forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
2. 特點:
(1)瀏覽器地址欄路徑不發生變化
(2)只能轉發到當前服務器內部資源中如同這裏 servletDemo 和 requestDemo在同一個Tomcat服務器內
(3)轉發是一次性請求,使用抓包工具可以發現,只轉發了一次請求
三、 共享數據:
* 域對象:一個有作用範圍的對象,可以在範圍內共享數據
* request域:代表一次請求的範圍,一般用於請求轉發的多個資源中共享數據
* 方法:
(1)void setAttribute(String name,Object obj):存儲數據
(2)Object getAttitude(String name):通過鍵獲取值
(3)void removeAttribute(String name):通過鍵移除鍵值對
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("requestDemo 被訪問了。。。");
// 存儲數據到 request 域中
request.setAttribute("msg", "hello");
//進行轉發
request.getRequestDispatcher("/servletDemo").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
四、獲取 ServletContext
ServletContext getServletContext()
@WebServlet("/requestDemo")
public class RequestDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}