5.response,request对象

来源:互联网 发布:hadoop java获取文件 编辑:程序博客网 时间:2024/06/05 09:39

1.简介:
这里写图片描述

2.HttpServletResponse
这里写图片描述

response常见应用:

列表内容

这里写图片描述

①向客户端输出中文数据:

OutoutStream:

//在servlet中用outputStream输数据的问题,以及输出中文问题。public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //程序以什么码表输出,就一定要告诉浏览器以什么码表打开。        //细节:html: <meta>标签可以模拟一个http响应头        //如果要输出数字,应该用字符串把数字包起来。        response.setHeader("Content-type", "text/html;charset=UTF-8");//;写成 ,会下载        String data = "中国";        OutputStream out = response.getOutputStream();        //out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>");        out.write(data.getBytes("UTF-8"));//设置编码格式    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

PrintWriter : 设置编码表方式不同。

//通过response的write流输出数据//printwriter: 只是设置编码表方式不同public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //设置response使用的码表,以控制response以什么码表向浏览器写出数据        response.setCharacterEncoding("UTF-8");   //设置编码格式        //指定浏览器以什么码表打开服务器发送的数据        response.setHeader("content-type", "text/html;charset=UTF-8");        //上面两句的简单表示形式://      response.setContentType("text/html;cherset=UTF-8");        String data = "中国";        PrintWriter out = response.getWriter();        out.write(data);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

②文件下载

//文件下载public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String path = this.getServletContext().getRealPath(                "/download/秒速5厘米.jpg");        String filename = path.substring(path.lastIndexOf("\\") + 1);        //如果下载文件是中文文件,则文件名需要经过url编码        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));        InputStream in = null;        OutputStream out = null;        try {            in = new FileInputStream(path);            int len = 0;            byte[] buffer = new byte[11024];            out = response.getOutputStream();            while ((len = in.read(buffer)) != 0) {                out.write(buffer, 0, len);            }        } catch (Exception e) {            new RuntimeException("读写失败!");        } finally {            if (in != null) {                try {                    in.close();                } catch (Exception e) {//                  throw new RuntimeException("关闭失败");                }            }            if (out != null) {                try {                    out.close();                } catch (Exception e) {//                  throw new RuntimeException("关闭失败");                }            }        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

③输出随机图片:

//输出一张随机图片,类 BufferedImage,类ImageIO类public class ResponseDemoP extends HttpServlet {    public static final int WIDTH = 120;    public static final int HEIGHT = 27;    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);        Graphics g = image.getGraphics();         // 1.设置背景色        setBackGround(g);        // 2.设置边框        setBorder(g);        // 4.写随机数        drawRandowNum((Graphics2D)g);  //为了旋转        // 3.画干扰线        drawRandowLine(g);        // 5.图形写给浏览器        response.setContentType("image/jpeg");        //控制浏览器不要缓存             response.setDateHeader("expirse", -1);        response.setHeader("cache-control", "no-cache");        response.setHeader("pragma", "no-cache");        ImageIO.write(image, "jpg", response.getOutputStream());    }    private void setBackGround(Graphics g) {        g.setColor(Color.WHITE);        g.fillRect(0, 0, WIDTH, HEIGHT);    }    private void setBorder(Graphics g) {        g.setColor(Color.BLUE);        g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);    }    private void drawRandowLine(Graphics g) {        g.setColor(Color.GREEN);        for (int i = 0; i < 5; i++) {            int x1 = new Random().nextInt(WIDTH-6)+3;  //让线条不和边框重叠            int y1 = new Random().nextInt(HEIGHT-6)+3;            int x2 = new Random().nextInt(WIDTH-6)+3;            int y2 = new Random().nextInt(HEIGHT-6)+3;            g.drawLine(x1, y1, x2, y2);        }    }    private void drawRandowNum(Graphics2D g) {        g.setColor(Color.RED);        g.setFont(new Font("宋体",Font.BOLD,20));        //常用汉字        String base = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765\u5230\u65f6\u5927\u5730\u4e3a\u5b50\u4e2d\u4f60\u8bf4\u751f\u56fd\u5e74\u7740\u5c31\u90a3\u548c\u8981\u5979\u51fa\u4e5f\u5f97\u91cc\u540e\u81ea\u4ee5\u4f1a\u5bb6\u53ef\u4e0b\u800c\u8fc7\u5929\u53bb\u80fd\u5bf9\u5c0f\u591a\u7136\u4e8e\u5fc3\u5b66\u4e48\u4e4b\u90fd\u597d\u770b\u8d77\u53d1\u5f53\u6ca1\u6210\u53ea\u5982\u4e8b\u628a\u8fd8\u7528\u7b2c\u6837\u9053\u60f3\u4f5c\u79cd\u5f00\u7f8e\u603b\u4ece\u65e0\u60c5\u5df1\u9762\u6700\u5973\u4f46\u73b0\u524d\u4e9b\u6240\u540c\u65e5\u624b\u53c8\u884c\u610f\u52a8\u65b9\u671f\u5b83\u5934\u7ecf\u957f\u513f\u56de\u4f4d\u5206\u7231\u8001\u56e0\u5f88\u7ed9\u540d\u6cd5\u95f4\u65af\u77e5\u4e16\u4ec0\u4e24\u6b21\u4f7f\u8eab\u8005\u88ab\u9ad8\u5df2\u4eb2\u5176\u8fdb\u6b64\u8bdd\u5e38\u4e0e\u6d3b\u6b63\u611f\u89c1\u660e\u95ee\u529b\u7406\u5c14\u70b9\u6587\u51e0\u5b9a\u672c\u516c\u7279\u505a\u5916\u5b69\u76f8\u897f\u679c\u8d70\u5c06\u6708\u5341\u5b9e\u5411\u58f0\u8f66\u5168\u4fe1\u91cd\u4e09\u673a\u5de5\u7269\u6c14\u6bcf\u5e76\u522b\u771f\u6253\u592a\u65b0\u6bd4\u624d\u4fbf\u592b\u518d\u4e66\u90e8\u6c34\u50cf\u773c\u7b49\u4f53\u5374\u52a0\u7535\u4e3b\u754c\u95e8\u5229\u6d77\u53d7\u542c\u8868\u5fb7\u5c11\u514b\u4ee3\u5458\u8bb8\u7a1c\u5148\u53e3\u7531\u6b7b\u5b89\u5199\u6027\u9a6c\u5149\u767d\u6216\u4f4f\u96be\u671b\u6559\u547d\u82b1\u7ed3\u4e50\u8272\u66f4\u62c9\u4e1c\u795e\u8bb0\u5904\u8ba9\u6bcd\u7236\u5e94\u76f4\u5b57\u573a\u5e73\u62a5\u53cb\u5173\u653e\u81f3\u5f20\u8ba4\u63a5\u544a\u5165\u7b11\u5185\u82f1\u519b\u5019\u6c11\u5c81\u5f80\u4f55\u5ea6\u5c71\u89c9\u8def\u5e26\u4e07\u7537\u8fb9\u98ce\u89e3\u53eb\u4efb\u91d1\u5feb\u539f\u5403\u5988\u53d8\u901a\u5e08\u7acb\u8c61\u6570\u56db\u5931\u6ee1\u6218\u8fdc\u683c\u58eb\u97f3\u8f7b\u76ee\u6761\u5462\u75c5\u59cb\u8fbe\u6df1\u5b8c\u4eca\u63d0\u6c42\u6e05\u738b\u5316\u7a7a\u4e1a\u601d\u5207\u600e\u975e\u627e\u7247\u7f57\u94b1\u7d36\u5417\u8bed\u5143\u559c\u66fe\u79bb\u98de\u79d1\u8a00\u5e72\u6d41\u6b22\u7ea6\u5404\u5373\u6307\u5408\u53cd\u9898\u5fc5\u8be5\u8bba\u4ea4\u7ec8\u6797\u8bf7\u533b\u665a\u5236\u7403\u51b3\u7aa2\u4f20\u753b\u4fdd\u8bfb\u8fd0\u53ca\u5219\u623f\u65e9\u9662\u91cf\u82e6\u706b\u5e03\u54c1\u8fd1\u5750\u4ea7\u7b54\u661f\u7cbe\u89c6\u4e94\u8fde\u53f8\u5df4\u5947\u7ba1\u7c7b\u672a\u670b\u4e14\u5a5a\u53f0\u591c\u9752\u5317\u961f\u4e45\u4e4e\u8d8a\u89c2\u843d\u5c3d\u5f62\u5f71\u7ea2\u7238\u767e\u4ee4\u5468\u5427\u8bc6\u6b65\u5e0c\u4e9a\u672f\u7559\u5e02\u534a\u70ed\u9001\u5174\u9020\u8c08\u5bb9\u6781\u968f\u6f14\u6536\u9996\u6839\u8bb2\u6574\u5f0f\u53d6\u7167\u529e\u5f3a\u77f3\u53e4\u534e\u8ae3\u62ff\u8ba1\u60a8\u88c5\u4f3c\u8db3\u53cc\u59bb\u5c3c\u8f6c\u8bc9\u7c73\u79f0\u4e3d\u5ba2\u5357\u9886\u8282\u8863\u7ad9\u9ed1\u523b\u7edf\u65ad\u798f\u57ce\u6545\u5386\u60ca\u8138\u9009\u5305\u7d27\u4e89\u53e6\u5efa\u7ef4\u7edd\u6811\u7cfb\u4f24\u793a\u613f\u6301\u5343\u53f2\u8c01\u51c6\u8054\u5987\u7eaa\u57fa\u4e70\u5fd7\u9759\u963f\u8bd7\u72ec\u590d\u75db\u6d88\u793e\u7b97\u4e49\u7adf\u786e\u9152\u9700\u5355\u6cbb\u5361\u5e78\u5170\u5ff5\u4e3e\u4ec5\u949f\u6015\u5171\u6bdb\u53e5\u606f\u529f\u5b98\u5f85\u7a76\u8ddf\u7a7f\u5ba4\u6613\u6e38\u7a0b\u53f7\u5c45\u8003\u7a81\u76ae\u54ea\u8d39\u5012\u4ef7\u56fe\u5177\u521a\u8111\u6c38\u6b4c\u54cd\u5546\u793c\u7ec6\u4e13\u9ec4\u5757\u811a\u5473\u7075\u6539\u636e\u822c\u7834\u5f15\u98df\u4ecd\u5b58\u4f17\u6ce8\u7b14\u751a\u67d0\u6c89\u8840\u5907\u4e60\u6821\u9ed8\u52a1\u571f\u5fae\u5a18\u987b\u8bd5\u6000\u6599\u8c03\u5e7f\u8716\u82cf\u663e\u8d5b\u67e5\u5bc6\u8bae\u5e95\u5217\u5bcc\u68a6\u9519\u5ea7\u53c2\u516b\u9664\u8dd1\u4eae\u5047\u5370\u8bbe\u7ebf\u6e29\u867d\u6389\u4eac\u521d\u517b\u9999\u505c\u9645\u81f4\u9633\u7eb8\u674e\u7eb3\u9a8c\u52a9\u6fc0\u591f\u4e25\u8bc1\u5e1d\u996d\u5fd8\u8da3\u652f\u6625\u96c6\u4e08\u6728\u7814\u73ed\u666e\u5bfc\u987f\u7761\u5c55\u8df3\u83b7\u827a\u516d\u6ce2\u5bdf\u7fa4\u7687\u6bb5\u6025\u5ead\u521b\u533a\u5965\u5668\u8c22\u5f1f\u5e97\u5426\u5bb3\u8349\u6392\u80cc\u6b62\u7ec4\u5dde\u671d\u5c01\u775b\u677f\u89d2\u51b5\u66f2\u9986\u80b2\u5fd9\u8d28\u6cb3\u7eed\u54e5\u547c\u82e5\u63a8\u5883\u9047\u96e8\u6807\u59d0\u5145\u56f4\u6848\u4f26\u62a4\u51b7\u8b66\u8d1d\u8457\u96ea\u7d22\u5267\u554a\u8239\u9669\u70df\u4f9d\u6597\u503c\u5e2e\u6c49\u6162\u4f5b\u80af\u95fb\u5531\u6c99\u5c40\u4f2f\u65cf\u4f4e\u73a9\u8d44\u5c4b\u51fb\u901f\u987e\u6cea\u6d32\u56e2\u5723\u65c1\u5802\u5175\u4e03\u9732\u56ed\u725b\u54ed\u65c5\u8857\u52b3\u578b\u70c8\u59d1\u9648\u83ab\u9c7c\u5f02\u62b1\u5b9d\u6743\u9c81\u7b80\u6001\u7ea7\u7968\u602a\u5bfb\u6740\u5f8b\u80dc\u4efd\u6c7d\u53f3\u6d0b\u8303\u5e8a\u821e\u79d8\u5348\u767b\u697c\u8d35\u5438\u8d23\u4f8b\u8ffd\u8f83\u804c\u5c5e\u6e10\u5de6\u5f55\u4e1d\u7259\u515a\u7ee7\u6258\u8d76\u7ae0\u667a\u51b2\u53f6\u80e1\u5409\u5356\u575a\u559d\u8089\u9057\u6551\u4fee\u677e\u4e34\u85cf\u62c5\u620f\u5584\u536b\u836f\u60b2\u6562\u9760\u4f0a\u6751\u6234\u8bcd\u68ee\u8033\u5dee\u77ed\u7956\u4e91\u89c4\u7a97\u6563\u8ff7\u6cb9\u65e7\u9002\u4e61\u67b6\u6069\u6295\u5f39\u94c1\u535a\u96f7\u5e9c\u538b\u8d85\u8d1f\u52d2\u6742\u9192\u6d17\u91c7\u6beb\u5634\u6bd5\u4e5d\u51b0\u65e2\u72b6\u4e71\u666f\u5e2d\u73cd\u7ae5\u9876\u6d3e\u7d20\u8131\u519c\u7591\u7ec3\u91ce\u6309\u72af\u62cd\u5f81\u574f\u9aa8\u4f59\u627f\u7f6e\u81d3\u5f69\u706f\u5de8\u7434\u514d\u73af\u59c6\u6697\u6362\u6280\u7ffb\u675f\u589e\u5fcd\u9910\u6d1b\u585e\u7f3a\u5fc6\u5224\u6b27\u5c42\u4ed8\u9635\u739b\u6279\u5c9b\u9879\u72d7\u4f11\u61c2\u6b66\u9769\u826f\u6076\u604b\u59d4\u62e5\u5a1c\u5999\u63a2\u5440\u8425\u9000\u6447\u5f04\u684c\u719f\u8bfa\u5ba3\u94f6\u52bf\u5956\u5bab\u5ffd\u5957\u5eb7\u4f9b\u4f18\u8bfe\u9e1f\u558a\u964d\u590f\u56f0\u5218\u7f6a\u4ea1\u978b\u5065\u6a21\u8d25\u4f34\u5b88\u6325\u9c9c\u8d22\u5b64\u67aa\u7981\u6050\u4f19\u6770\u8ff9\u59b9\u85f8\u904d\u76d6\u526f\u5766\u724c\u6c5f\u987a\u79cb\u8428\u83dc\u5212\u6388\u5f52\u6d6a\u542c\u51e1\u9884\u5976\u96c4\u5347\u7883\u7f16\u5178\u888b\u83b1\u542b\u76db\u6d4e\u8499\u68cb\u7aef\u817f\u62db\u91ca\u4ecb\u70e7\u8bef";        int x = 10;        //已有汉字区间:[\u4e00-\u9fa5]        for(int i=0; i<4; i++){             int degree = new Random().nextInt()%30;  //控制随机旋转弧度,正反旋转              String ch = base.charAt(new Random().nextInt(base.length()))+"";              g.rotate(degree*Math.PI/180,x+10,10);              g.drawString(ch, x, 21);//汉字的实际显示长度是小于20的,所以显示出来和上边存在距离              g.rotate(-degree*Math.PI/180,x+10,10);   //转回去              x += 27;        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}register.html:<html>  <head>    <title>register.html</title>   // <script type="text/javascript">        //function changeImage(img){            //alert("更新已执行");            //img.src = img.src+"?"+new Date().getTime();  //让地址发生变化,不取缓存,实现刷新        //}   // </script>  </head>  <body>    <form action="">        用户名:<input type="text" name="username"><br>        密码:<input type="password" name="password"><br>        验证码:<input type="text" name="checkcode">        <img src="/day06/servlet/ResponseDemoP" onclick="this.src = this.src+'?'+new Date().getTime()" alt="换一张" style="cursor:pointer"><br>        <input type="submit" value="注册">    </form>  </body></html>

④控制浏览器定时刷新网页(响应头:refersh):

//控制浏览器定时刷新public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        test3(request,response);  //实用的自动跳转技术    }    public void test3(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {        //假设这是一个用户登陆的servlet        //程序到此运行到此,用户登陆成功了        String message="<meta http-equiv='refresh' content='3;url=/day06/index.jsp'>登陆成功,在3秒后会跳到首页。如果没有跳转,请点击<a href=''>超链接</a>";        this.getServletContext().setAttribute("message", message);        this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);    }    public void test2(HttpServletResponse response) throws IOException {        //假设这是一个用户登陆的servlet        //程序到此运行到此,用户登陆成功了        response.setCharacterEncoding("UTF-8");        response.setContentType("text/html;charset=utf-8");        response.setHeader("refresh", "3; url = '/day06/index.jsp' ");        response.getWriter().write("登陆成功,在3秒后会跳到首页。如果没有跳转,请点击<a href=''>超链接</a>");    }    public void test1(HttpServletResponse response) throws IOException {        response.setHeader("refresh", "3");        String data = new Random().nextInt(100000)+"";        response.getWriter().write(data);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}message.jsp: 跳转中间界面<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>       <title>My JSP 'message.jsp' starting page</title>  </head>  <body>    <%            String message = (String)application.getAttribute("message");           out.write(message);     %>  </body></html>index.jsp:跳转到的界面(首页等)

⑤控制浏览器缓存:servlet数据一直不变的情形。

index.jsp:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'index.jsp' starting page</title>   </head>  <body>    <a href="/day06/servlet/ResponseDemo1">查看图书</a>  </body></html>//控制浏览器缓存public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setDateHeader("expires", System.currentTimeMillis()+1000*3600);  //缓存一小时               String data = "hahahahaah";        response.getWriter().write(data);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

⑥实现请求重定向:一般不用。

登陆用,因为重定向会使地址栏方式变化,告知用户登陆成功。
购物车,用转发的话会重新购买一次。

//实现请求重定向:/*    特点:    1.浏览器会向服务器发送两次,意味着就有两个request\response    2.用重定向技术,浏览器地址栏会发生变化    用户登录和显示购物车时,通常会用到重定向技术。 */public class ResponseDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {/*      response.setStatus(302);        response.setHeader("location", "/day06/index.jsp");*/        response.sendRedirect("/day06/index.jsp");    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

response细节:
这里写图片描述

3.HttpServletRequest

URI: 请求一个资源  /news/htmlURL: 请求一个互联网上的资源  http://www.bai.com/news

这里写图片描述

这里写图片描述

示例:

①getRequestURI(),getRequestURL():
应用场景:权限拦截,统计页面访问次数等等。URI用的比较多

这里写图片描述

示例:
②request获取请求头和请求数据:

//获取请求头信息和请求数据。public class RequestDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {//      test1(request);             test2(request);    }//获取请求数据    //一般来说,都要先检查再使用(健壮性判断)    public void test2(HttpServletRequest request) throws IOException {        //1.获取请求数据        String value =  request.getParameter("username");        if(value!=null && !value.trim().equals("")){            System.out.println(value);        }        //2.获取请求数据集        String[] values =  request.getParameterValues("username");        for(int i=0;values!=null && i<values.length;i++){            System.out.println(values[i]);                  }        //3.获取请求数据名集,再迭代获取值        Enumeration e = request.getParameterNames();        while(e.hasMoreElements()){            String name = (String)e.nextElement();            value = request.getParameter(name);        }        //4.获取请求数据map集合(用的比较多)        Map<String,String[]> map =  request.getParameterMap();  //可能值有多个,所以值是数组        User user = new User();        try {            BeanUtils.populate(user, map);   //用map集合填充bean//          BeanUtils.copyProperties(user, formbeam);  //bean的拷贝,formbean用来校验        } catch (Exception e1) {                        e1.printStackTrace();        }        //5.获取请求数据(一般文件上传用)。如果前面Servlet调用了getParameter系列方法,则这里将失效。        InputStream in = request.getInputStream();        int len = 0;        byte[] buffer = new byte[1024];        while((len=in.read(buffer))!=-1){            System.out.println(new String(buffer,0,len));        }    }//获取请求头相关的方法    public void test1(HttpServletRequest request) {        //1.获得请求头信息        String headValue =  request.getHeader("Accept-Encoding");        //2.获得请求头信息集        Enumeration e =  request.getHeaders("Accept-Encoding");        while(e.hasMoreElements()){            String value = (String)e.nextElement();        }        //3.获得请求头名集,再迭代获取值        e =  request.getHeaderNames();        while(e.hasMoreElements()){            String name = (String)e.nextElement();            String value = request.getHeader(name);        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}test.html:<!DOCTYPE html><html>  <head>    <title>test.html</title>  </head>  <body>    <a href="/day06/servlet/RequestDemo1?username=xxx">用户名</a>    <form action="/day06/servlet/RequestDemo1" method="post">    用户名:<input type="text" name="username">    <input type="submit" value="提交">    </form>  </body></html>User.java:beanpublic class User {    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

上面示例中request.getInputStream()获取数据失败的原因:

根据Servlet规范,如果同时满足下列条件,则请求体(Entity)中的表单数据,将被填充到request的parameter集合中(request.getParameter系列方法可以读取相关数据): 1 这是一个HTTP/HTTPS请求 2 请求方法是POST(querystring无论是否POST都将被设置到parameter中) 3 请求的类型(Content-Type头)是application/x-www-form-urlencoded 4 Servlet调用了getParameter系列方法如果上述条件没有同时满足,则相关的表单数据不会被设置进request的parameter集合中,相关的数据可以通过request.getInputStream()来访问。反之,如果上述条件均满足,相关的表单数据将不能再通过request.getInputStream()来读取。

常见应用

这里写图片描述

request的请求转发

这里写图片描述

这里写图片描述

细节1 举例:response已经将数据部分写回了客户端,无法再进行转发

这里写图片描述

防止方案:每次跳转就return

这里写图片描述

细节2 举例:response缓存区中已经写入了数据,但是还没有发给客户端时,仍可以实现转发,但是缓存区中的响应体会被清除。

这里写图片描述

①防盗链

//防盗链public class RequestDemo2 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String referer = request.getHeader("referer");  //查看来访地址        if(referer==null || !referer.startsWith("http://localhost")){            response.sendRedirect("/day06/index.jsp");   //重定向            return;        }//      response.setCharacterEncoding("UTF-8");        response.setContentType("text/html;charset=gbk");        String data = "一篇日记";        response.getWriter().write(data);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'index.jsp' starting page</title>   </head>  <body>        看广告<br/>    <a href="/day06/servlet/RequestDemo2">看日志</a>  </body></html>

②通过表单收集客户机数据:

form.html:<!DOCTYPE html><html><head>    <meta http-equiv='content-type' content='text/html;charset=UTF-8'><title>form.html</title></head><body><!-- url后面如果跟了中文数据,要编码后再提交 -->    <form action="/day06/servlet/RequestDemo" method="post">        用户名:<input type="text" name="username"><br />         密码:<input type="password" name="password"><br />         性别:        <input type="radio" name="gender" value="male"><input type="radio" name="gender" value="female"><br/>        所在地:        <select name="city">        <option value="shanghai">上海</option>        <option value="beijing">北京</option>        <option value="changsha">长沙</option>                </select><br/>        爱好:            <input type="checkbox" name="likes" value="sing">唱歌            <input type="checkbox" name="likes" value="dance">跳舞            <input type="checkbox" name="likes" value="basketball">篮球            <br/>        备注:            <textarea rows="6" clos="60" name="description"></textarea><br/>        大头照:<input type="file" name="image"><br/>        <input type="hidden" name="id" value="123456">        <input type="submit" value="提交">    </form></body></html>public class RequestDemo extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");  //解决中文乱码问题        System.out.println(request.getParameter("username"));        System.out.println(request.getParameter("password"));        System.out.println(request.getParameter("gender"));        System.out.println(request.getParameter("city"));        String[] likes = request.getParameterValues("likes");        for(int i=0; i<likes.length && likes!=null;i++){            System.out.println(likes[i]);        }        System.out.println(request.getParameter("description")); //如果收到的是中文,需要解决乱码问题        System.out.println(request.getParameter("id"));         }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

③request乱码:

//request乱码问题//也可以修改服务器的配置(不建议修改)//①URIEncoding设置URI即get数据的编码格式,tomcat8.0默认UTF-8//②useBodyEncodingForURI设置URI的编码即get是否和请求体的即post一致,默认为flasepublic class RequestDemo3 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        test1(request);    }    // 解决post提交的乱码。tomcat8.0系统设置的请求体的默认编码是iso-8859-1    public void test1(HttpServletRequest request)            throws UnsupportedEncodingException {        request.setCharacterEncoding("UTF-8");   // 只对post提交有用,将请求体的默认编码改成UTF-8        String username = request.getParameter("username");        System.out.println(username);    }    // 解决get提交,超链接提交的乱码(手工处理)   //实测不处理,也输出的是中文。因为tomcat8.0系统设置的URI默认编码是UTF-8    public void test2(HttpServletRequest request)            throws UnsupportedEncodingException {        String username = request.getParameter("username");        username = new String(username.getBytes("iso-8859-1"), "UTF-8"); // 手工处理。request底层用iso8859-1处理        System.out.println(username);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}form3.html:<!DOCTYPE html><html>  <head>    <title>中文乱码问题</title>    <meta name="content-type" content="text/html; charset=UTF-8">  </head>  <body>    <form action="/day06/servlet/RequestDemo3" method="post">        用户名:<input type="text" name="username"><br/>        <input type="submit" value="提交">    </form>    <form action="/day06/servlet/RequestDemo3" method="get">        用户名:<input type="text" name="username"><br/>        <input type="submit" value="get提交">    </form>    <a href="/day06/servlet/RequestDemo3?username=中国">get</a>  </body></html>

乱码问题总结:

1.对于post提交的中文数据:服务器在用request的getParameter()方法获取客户端传来的数据(写页面时设置的UTF-8)时,服务器内部设置请求体的解码表是iso8859-1(在tomcat文档中可以查看),所以处理方式只有:①通过改变请求体的解码方式:request.setCharacterEncoding("UTF-8");2.对于get提交的中文数据:服务器在用request的getParameter()方法获取客户端传来的数据(写页面时设置的UTF-8)时,tomcat8.0服务器内部设置URI的解码表是UTF-8,处理方式有:①直接获取,不用改变如果其他版本服务器内部设置URI的解码表是iso8859-1,处理方式有:①手工处理:new String(value.getBytes("iso8859-1"),"UTF-8");②修改服务器的配置(不建议修改):两种方式URIEncoding属性(设置URI即get数据的编码格式):        <Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443" URIEncoding="UTF-8"/>   然后直接获取。useBodyEncodingForURI属性(设置URI的编码即get是否和请求体即post一致,默认为flase):    <Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443" useBodyEncodingForURI="true"/>    然后当作请求体post来设置,通过直接改变请求体的解码方式:    request.setCharacterEncoding("UTF-8");    再获取。

④request实现请求转发,MVC设计模式
特点:
1.客户端只发一次请求,而服务器端有多个资源调用
2.客户端浏览地址栏没有变化。

之前学的请求转发是通过ServletContext域获得请求转发对象的:RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");rd.forward(request, response);在学ServletContext域时存在的一个问题:用ServletContext域带数据来转发,可能会出现多个servlet对象覆盖原有的数据的安全问题:通过request域来解决,不会导致数据共享。示例:String data = "abacsbd";        //数据带给1.jsp(不能通过context域,要通过request域)        this.getServletContext().setAttribute("data", data);<%        String data = (String) application.getAttribute("data");        out.write(data);    %>request也是一个域对象,目前为止第二个。
//请求转发,以及使用request域对象把数据带给资源//mvc: ( model (javabean)  view(jsp)  cotroller(servlet) )public class RequestDemo4 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String data ="aaaa";        request.setAttribute("data", data);        //request也可以实现转发        request.getRequestDispatcher("/message.jsp").forward(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}message.jsp:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>       <title>My JSP 'message.jsp' starting page</title>  </head>  <body>  ${data}  <%         String message =(String)request.getAttribute("data");        out.write(message);  %>  </body></html>

RequestDispatcher请求转发对象除了有转发方法forward()方法外,还有实现包含其他页面方法include()

//用include实现页面包含public class RequestDemo4 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.getRequestDispatcher("/public/head.jsp").include(request, response);        response.getWriter().write("hahahahaha<br/>");        request.getRequestDispatcher("/public/foot.jsp").include(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}head.jsp:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>    head<br>foot.jsp:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>    foot<br>显示:headhahahaahhaafoot

4.web工程中各类地址的写法:
”/“开头地址原则:如果是给服务器的用的,就是当前应用目录;如果的给浏览器用的,就是网站,网站下面都多个web应用。

”\“:获取硬盘资源用的

request.getRequestDispatcher("/form.html").forward(request,response);//转发给服务器response.sendRedirect("/day01/form.html");//重定向给浏览器this.getServletContext().getRealPath("/form1.html");  //给服务器this.getServletContext().getResourceAsStream("/form.html"); //给服务器<a href="/day06/form.html">点</a>   //给浏览器<form action="/day06/form.html"></form> //给浏览器
原创粉丝点击