request對象
request對象和response對象都是由服務器建立的,咱們是來使用他們
request對象是用來獲取請求消息的,response對象是用來設置響應消息的html
1. 獲取請求消息數據
1. 獲取請求行數據java
GET /day14/demo1?name=zhangsan HTTP/1.1數組
方法:
1. 獲取請求方式 :GET
* String getMethod()
2. (*)獲取虛擬目錄:/day14
* String getContextPath()
3. 獲取Servlet路徑: /demo1
* String getServletPath()
4. 獲取get方式請求參數:name=zhangsan
* String getQueryString()
5. (*)獲取請求URI:/day14/demo1
* String getRequestURI(): /day14/demo1
* 6.獲取url
* StringBuffer getRequestURL() :http://localhost/day14/demo1瀏覽器
* URL:統一資源定位符 : http://localhost/day14/demo1 中華人民共和國 * URI:統一資源標識符 : /day14/demo1 共和國 **6. 獲取協議及版本**:HTTP/1.1 * String getProtocol() **7. 獲取客戶機的IP地址**: * String getRemoteAddr() **2. 獲取請求頭數據** * **方法**: * (*)String getHeader(String name):經過請求頭的名稱獲取請求頭的值 * Enumeration<String> getHeaderNames():獲取全部的請求頭名稱 **3. 獲取請求體數據**: * 請求體:只有POST請求方式,纔有請求體,在請求體中封裝了POST請求的請求參數 * 步驟: **1. 獲取流對象** * BufferedReader getReader():獲取字符輸入流,只能操做字符數據 * ServletInputStream getInputStream():獲取字節輸入流,能夠操做全部類型數據 * 在文件上傳知識點後講解 **2. 再從流對象中拿數據** **2. 其餘功能**: **1. 獲取請求參數通用方式**:不論get仍是post請求方式均可以使用下列方法來獲取請求參數 1. String getParameter(String name):根據參數名稱獲取參數值 username=zs&password=123 2. String[] getParameterValues(String name):根據參數名稱獲取參數值的數組 hobby=xx&hobby=game 3. Enumeration<String> getParameterNames():獲取全部請求的參數名稱 4. Map<String,String[]> getParameterMap():獲取全部參數的map集合 * **中文亂碼問題**: * **get方式**:tomcat 8 已經將get方式亂碼問題解決了 * **post方式**:會亂碼 * **解決**:在獲取參數前,設置request的編碼request.setCharacterEncoding("utf-8");
什麼是請求轉發?
請求轉發是服務器內部資源跳轉的一種方式
步驟tomcat
2. 特色:服務器
代碼
這個案例是以index.jsp頁面向Servletimp請求,而後Servletimp跳轉到Bservletjsp
Servletimpide
import javax.servlet.RequestDispatcher; 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("/Servletimp") public class Servletimp extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Bservlet"); requestDispatcher.forward(req,resp); System.out.println("Servletimp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req,resp); } }
Bservletpost
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("/Bservlet") public class Bservlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { System.out.println("Bservlet"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } }
<%-- Created by IntelliJ IDEA. User: 86151 Date: 2020/7/29 Time: 11:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <FORM action="/Servletimp" method="post"> <input name="username"> <input type="submit" value="提交"> </FORM> </body> </html>
咱們先來了解什麼是域對象?
域對象:一個有做用範圍的對象,能夠在範圍內共享數據
request:表明一次請求的範圍,通常用於請求轉發中多個資源的共享數據this
方法
獲取ServletContext
ServletContext getServletContext
簡單的代碼實現
import javax.servlet.RequestDispatcher; 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("/Servletimp") public class Servletimp extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Bservlet"); req.setAttribute("id",1);//存入鍵名爲id值爲1的數據 requestDispatcher.forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } }
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("/Bservlet") public class Bservlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { Object id = req.getAttribute("id");//獲取鍵名爲id的鍵值 System.out.println(id); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } }