(JavaWeb)HttpServletResponse和HttpServletRequest
生活随笔
收集整理的這篇文章主要介紹了
(JavaWeb)HttpServletResponse和HttpServletRequest
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- HttpServletResponse
- 1.簡單分類
- 2.下載文件
- 3.重定向
- 4.登錄成功時重定向功能
- HttpServletRequest
- 獲取參數,請求轉發
- 請求轉發與重定向的區別
HttpServletResponse
web服務器接收到客戶端的http請求,針對這個請求,分別創建一個代表請求的HttpServletRequest對象,代表響應的一個HttpServletResponse;
- 如果要獲取客戶端請求過來的參數:找HttpServletRequest
- 如果要給客戶端響應一些信息:找HttpServletResponse
1.簡單分類
負責向瀏覽器發送數據的方法
ServletOutputStream getOutputStream() throws IOException; PrintWriter getWriter() throws IOException;負責向瀏覽器發送響應頭的方法
void setCharacterEncoding(String var1);void setContentLength(int var1);void setContentLengthLong(long var1);void setContentType(String var1);void setDateHeader(String var1, long var2);void addDateHeader(String var1, long var2);void setHeader(String var1, String var2);void addHeader(String var1, String var2);void setIntHeader(String var1, int var2);void addIntHeader(String var1, int var2);響應的狀態碼
int SC_CONTINUE = 100;int SC_SWITCHING_PROTOCOLS = 101;int SC_OK = 200;int SC_CREATED = 201;int SC_ACCEPTED = 202;int SC_NON_AUTHORITATIVE_INFORMATION = 203;int SC_NO_CONTENT = 204;int SC_RESET_CONTENT = 205;int SC_PARTIAL_CONTENT = 206;int SC_MULTIPLE_CHOICES = 300;int SC_MOVED_PERMANENTLY = 301;int SC_MOVED_TEMPORARILY = 302;int SC_FOUND = 302;int SC_SEE_OTHER = 303;int SC_NOT_MODIFIED = 304;int SC_USE_PROXY = 305;int SC_TEMPORARY_REDIRECT = 307;int SC_BAD_REQUEST = 400;int SC_UNAUTHORIZED = 401;int SC_PAYMENT_REQUIRED = 402;int SC_FORBIDDEN = 403;int SC_NOT_FOUND = 404;int SC_METHOD_NOT_ALLOWED = 405;int SC_NOT_ACCEPTABLE = 406;int SC_PROXY_AUTHENTICATION_REQUIRED = 407;int SC_REQUEST_TIMEOUT = 408;int SC_CONFLICT = 409;int SC_GONE = 410;int SC_LENGTH_REQUIRED = 411;int SC_PRECONDITION_FAILED = 412;int SC_REQUEST_ENTITY_TOO_LARGE = 413;int SC_REQUEST_URI_TOO_LONG = 414;int SC_UNSUPPORTED_MEDIA_TYPE = 415;int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;int SC_EXPECTATION_FAILED = 417;int SC_INTERNAL_SERVER_ERROR = 500;int SC_NOT_IMPLEMENTED = 501;int SC_BAD_GATEWAY = 502;int SC_SERVICE_UNAVAILABLE = 503;int SC_GATEWAY_TIMEOUT = 504;int SC_HTTP_VERSION_NOT_SUPPORTED = 505;2.下載文件
public class FileServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.要獲取下載文件的路徑String realPath = "C:\\Users\\Administrator\\IdeaProjects\\javaweb-01-maven\\demo02\\src\\main\\resources\\頭像.png";//2.獲取下載的文件名String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);//3.設置瀏覽器支持文件下載,中文文件名URLEncoder.encode編碼,否則有可能亂碼resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));//4.獲取下載文件的輸入流FileInputStream fis = new FileInputStream(realPath);//5.創建緩沖區int len = 0;byte[] buffer = new byte[1024];//6.獲取輸出流對象ServletOutputStream sos = resp.getOutputStream();//7.將FileInputStream流寫入到buffer緩沖區,使用輸出流對象將數據輸出到客戶端while ((len = fis.read(buffer))!=-1){sos.write(buffer,0,len);}fis.close();sos.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }打開網頁輸入servlet目錄,彈出下載框
3.重定向
B一個web資源收到客戶端A請求后,B他會通知A客戶端去訪問另外一個web資源C,這個過程叫重定向
常見場景:
- 用戶登錄
測試:
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/*resp.setHeader("Location","/r/img");resp.setStatus(302);*/resp.sendRedirect("/r/img");//重定向 }4.登錄成功時重定向功能
登錄頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <form action="${pageContext.request.contextPath}/login" method="get">用戶名:<input type="text" name="username"><br>密碼:<input type="password" name="password"><br><input type="submit"> </form> </body> </html>登錄成功頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> <h1>登錄成功</h1> </body> </html>處理請求servlet
public class RequestServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//獲取頁面數據String username = req.getParameter("username");String password = req.getParameter("password");System.out.println("用戶名:"+username);System.out.println("密碼:"+password);//重定向resp.sendRedirect("/demo02_war/success.jsp");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }
點擊提交重定向到登錄成功頁面
HttpServletRequest
HttpServletRequest代表客戶端的請求,用戶通過Http協議訪問服務器,HTTP請求中的所有信息會被封裝到HttpServletRequest,通過這個HttpServletRequest的方法,獲得客戶端的所有信息;
獲取參數,請求轉發
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //后臺接收中文亂碼問題req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");String username = req.getParameter("username");String password = req.getParameter("password");String[] hobbys = req.getParameterValues("hobbys");System.out.println(username);System.out.println(password);System.out.println(Arrays.toString(hobbys));System.out.println(req.getContextPath());//通過請求轉發//這里的 / 代表當前的web應用req.getRequestDispatcher("/success.jsp").forward(req,resp);}請求轉發與重定向的區別
面試題:請你聊聊重定向和轉發的區別?
相同點
- 頁面都會實現跳轉
不同點
- 請求轉發的時候,url不會產生變化
- 重定向時候,url地址欄會發生變化;
總結
以上是生活随笔為你收集整理的(JavaWeb)HttpServletResponse和HttpServletRequest的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (JavaWeb)ServletCont
- 下一篇: (JavaWeb)会话跟踪技术Cooki