HttpServletRequest
方法 |
說明 |
示例 |
setCharacterEncoding("編碼類型") |
請求數據的字符編碼設置爲“編碼類型” |
request.setCharacterEncoding("utf-8") |
getParameter("name") |
請求獲取名稱爲“name”的數據 |
request.getParameter("name") |
getParameterValues("name") |
按照“name”請求獲取名稱爲“name”的所有數據,並組成列表 |
request.getParameterValues("name") |
getRealPath("相對地址") |
將相對地址轉換成絕對地址 |
request.getRealPath("./page/shopCar.jsp") |
getRequestDispatcher("地址").forward(request,response) |
轉發(服務器行爲) |
request.getRequestDispatcher("./newTest.jsp").forward(request, response) |
HttpServletResponse
方法 |
說明 |
示例 |
setCharacterEncoding("編碼類型") |
響應數據的字符編碼設置爲“編碼類型” |
response.setCharacterEncoding("utf-8") |
setContentType("內容類型") |
響應的數據類型設置爲“內容類型” 六中內容類型: text/html :html代碼 text/plain :字符串 application/json :json數據 image/jpeg :圖像 application/javascript : JS代碼 text/css :CSS樣式 |
response.setContentType("text/html") |
sendRedirect("地址") |
重定向(客戶端行爲) |
response.sendRedirect("newTest.jsp") |
getWriter() |
設置響應輸出 |
response.getWriter().println("輸出") |
示例
public class ServletTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().print("3秒後跳轉到註冊頁面");
request.getRequestDispatcher("http://www.baidu.com").forward(request,response)
response.sendRedirect("http://www.baidu.com");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}