ch05 Request & Response(杜老师)


1. 输出字节流测试(PrintServlet01_du: OutputStream out=response.getOutputStream();out.write(data.getBytes()); )

2. 输出字符流测试(PrintServlet02_du: PrintWriter out = response.getWriter();out.write(data); )

3. 输出字符流、字节流冲突测试(PrintServlet03_du: PrintWriter out = response.getWriter();... OutputStream out2=response.getOutputStream(); )

4. 输出中文字符串乱码[ChineseServlet04_du]

5. 输出中文字符串乱码[ChineseServlet05_du:setCharacterEncoding("utf8");]

6. 输出中文字符串乱码[ChineseServlet06_du:response.setContentType("text/html;charset=utf-8"); ]

7. 输出中文字符串乱码[ChineseServlet07_du:setCharacterEncoding("utf8");response.setHeader("Content-Type","text/html;charset=utf-8"); ]

8. 2秒后刷新并跳转到【杜老师】的首页[RefreshServlet08_du:response.setHeader("Refresh","2;URL=http://www.duzhaojiang.cn"); ]

9. 每隔3秒后刷新输出当前时间[RefreshServlet09_du:response.setHeader("Refresh","3"); ...]

10. 禁止浏览器缓存页面[CacheServlet10_du: response.setDateHeader("Expires",0); response.setHeader("Cache-Control","no-cache"); response.setHeader("Pragma","no-cache"); ]

11. 输出重定向[login11_du:response.sendRedirect("url")]

12. 获取请求行的相关信息[RequestLineServlet12_du: request.get Method|RequestURI|Protocol Scheme|ServerName|ServerPort|ContextPath|ServletPath|QueryString|QueryString PathInfo|PathTranslated]

13. 获取请求消息中所有头字段[RequestHeadersServlet13_du: request.getHeaderNames(); request.getHeader(headerName);]

14. 获取请求消息体的相关方法[RequestHeadersServlet14_du:request.getInputStream();// 获取输入流对象]

  获取请求消息体的相关方法[RequestHeadersServlet14_du_:request.getReader();// 获取输入流对象]

15. 利用Referer请求头防止“盗链”[DownFilter15_du:

     String referer = request.getHeader("referer");
     if (referer == null || !referer.contains(request.getServerName())) { 
            // 如果 链接地址来自其他网站,则返回错误图片     
            request.getRequestDispatcher("/images/error.gif").forward(request, response);}
    

16. 获取请求参数[RequestParamsServlet16_du:request.getParameter("参数名"); request.getParameterValues("参数名");]

17. 请求参数的中文乱码问题[RequestParamsServlet17_du:request.setCharacterEncoding("utf-8"); ]

18. 获取网络连接信息[RequestNetServlet18_du:response.setContentType("text/html;charset=utf-8"); ]

19. 通过request对象传递数据[request.setAttribute("属性名",值); request.getAttribute("属性名"); request.removeAttribute("属性名");request.getAttributeNames();]

20. 派发-请求转发[RequestForwardServlet20_du: RequestDispatcher dispatcher = request.getRequestDispatcher("/ResultServlet20_du"); dispatcher.forward(request, response);]

21. 派发-请求包含[IncludingServlet21_du: RequestDispatcher rd = request.getRequestDispatcher("/IncludedServlet21_du?p1=abc"); rd.include(request, response); ]


返回 制作