javaWeb---Response和Request

Request

1. request对象和response对象的原理

  1. request和response对象是由服务器创建的。我们来使用它们

  2. request对象是来获取请求消息,response对象是来设置响应消息
    在这里插入图片描述

2. request对象继承体系结构:

在这里插入图片描述
在这里插入图片描述

3. request功能:

3.1. 获取请求消息数据
1. 获取请求行数据
  • GET /Servlet/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
    StringBuffer getRequestURL() :http://localhost/day14/demo1
    URL:统一资源定位符http://localhost/day14/demo1 中华人民共和国
    URI:统一资源标识符(表示的范围更大) : /day14/demo1 共和国
    6. 获取协议及版本:HTTP/1.1
    String getProtocol()
    7. 获取客户机的IP地址:
    String getRemoteAddr()
//1. 获取请求方式 :GET**
        String method=request.getMethod();
        System.out.println(method);
//2. (*)获取虚拟目录:/day14**
        String contextPath=request.getContextPath();
        System.out.println(contextPath);
// 3. 获取Servlet路径: /demo1**
        String servletPath=request.getServletPath();
        System.out.println(servletPath);
// 4. 获取get方式请求参数:name=zhangsan**
        String queryString=request.getQueryString();
        System.out.println(queryString);
// 5. (*)获取请求URI:/day14/demo1**
        StringBuffer requestURL=request.getRequestURL();
        String requestURL1=request.getRequestURI();
        System.out.println(requestURL1);
        System.out.println(requestURL);
// 6. 获取协议及版本:HTTP/1.1
        String protocol=request.getProtocol();
        System.out.println(protocol);
// 7. 获取客户机的IP地址:**
        String remoteAddr=request.getRemoteAddr();
        System.out.println(remoteAddr);

结果展示
在这里插入图片描述
在这里插入图片描述

2. 获取请求头数据
  • 方法:
    • String getHeader(String name): 通过请求头的名称获取请求头的值
    • Enumeration getHeaderNames(): 获取所有的请求头名称
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示请求头数据
        //1.获取所有请求头名称
        Enumeration<String> headerNames=request.getHeaderNames();
        //2.遍历
        while(headerNames.hasMoreElements()){
            String name=headerNames.nextElement();
            //根据名称获取请求头的值
            String value=request.getHeader(name);
            System.out.println(name+"---"+value);
        }
    }

结果:
在这里插入图片描述
判断浏览器的版本

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示请求头数据:user-agent
        String agent=request.getHeader("user-agent");
        //判断浏览器的版本
        if(agent.contains("Chrome")){
            System.out.println("谷歌来了 ...");
        }else if(agent.contains("Firefox")){
            System.out.println("火狐来了...");
        }else {
            System.out.println("IE 来了...");
        }
    }

在这里插入图片描述

防盗链

//放盗链
        if(referce!=null){
            if(referce.contains("/Request&Response")){
                //正常访问
                System.out.println("Ok..");
            }else {
                //异常访问
                System.out.println("Error");
            }
//盗链
        if(referce!=null){
            if(referce.contains("/Request&Response")){
                //正常访问
                response.setContentType("网页链接;字符集");
                response.getWriter().write("播放电影");
            }else {
                //盗链
                response.setContentType("网页链接;charset=utf-8");
                System.out.println("去官网看电影");
            }
        }
3. 获取请求体数据
  • 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
  • 步骤:
    1. 获取流对象

      • BufferedReader getReader():获取字符输入流,只能操作字符数据
      • ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
      • 在文件上传知识点后讲解
    2. 再从流对象中拿数据

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取其你去消息体---   请求参数
        //获取字符流
        BufferedReader br=request.getReader();
        //2.读取数据
        String line=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
    }
2. 其他功能:
1. 获取请求参数通用方式:

不论get还是post请求方式都可以使用下列方法来获取请求参数
1. String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2. String[] getParameterValues(String name):根据参数名称获取参数值的数组(只能获取一个) hobby=xx&hobby=game
3. Enumeration getParameterNames():获取所有请求的参数名称
4. Map<String,String[]> getParameterMap():获取所有参数的map集合

  • 中文乱码问题:
    • get方式:tomcat 8 已经将get方式乱码问题解决了
    • post方式:会乱码
    • 解决:在获取参数前,设置request的编码request.setCharacterEncoding(“utf-8”);
      request.setCharacterEncoding(“utf-8”);
      在这里插入图片描述
public class RequestDemo6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数   post
        //根据参数名称获取参数值
        String username=request.getParameter("username");
        System.out.println("post");
        System.out.println(username);
        //根据参数名称获取参数值得数组
        String[] hobbies=request.getParameterValues("hobby");
        for(String hobby:hobbies){
            System.out.println(hobby);
        }

        //获取所有请求参数的名称
        Enumeration<String> paraterNames=request.getParameterNames();
        while(paraterNames.hasMoreElements()){
            String name=paraterNames.nextElement();
            System.out.println(name);
            String value=request.getParameter(name);
            System.out.println(value);
            System.out.println("----------");

        //获取所有的map集合
        Map<String,String[]> parameterMap=request.getParameterMap();
        //遍历
        Set<String> keyset=parameterMap.keySet();
        for(String name:keyset){
            //获取值
            String[] values=parameterMap.get(name);
            System.out.println(name);
            for(String value:values){
                System.out.println(value);
            }
            System.out.println("------");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数   get
        //genuine参数吗获取参数值
        //String username = request.getParameter("username");
        //System.out.println("get");
        //System.out.println(username);
        this.doPost();

    }

在这里插入图片描述

2. 请求转发:一种在服务器内部的资源跳转方式

1. 步骤:
1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)

2. 特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。外部资源访问不到。
3. 转发是一次请求,一次响应
在这里插入图片描述

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Demo8被访问了...");
//        //转发
        request.getRequestDispatcher("/Demo9").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

在这里插入图片描述

3. 共享数据:
  • 域对象:一个有作用范围的对象,可以在范围内共享数据
  • request域代表一次请求的范围,一般用于请求转发的多个资源中共享数据
    • 方法:
      1. void setAttribute(String name,Object obj):存储数据
      2. Object getAttitude(String name):通过键获取值
        3. void removeAttribute(String name):通过键移除键值对
@WebServlet("/Demo8")
public class RequestDemo8 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Demo8被访问了...");
        request.setAttribute("msg","zhangsan");
//        //转发
//        RequestDispatcher requestDispatcher=request.getRequestDispatcher("/Demo9");
//        requestDispatcher.forward(request,response);
        request.getRequestDispatcher("/Demo9").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
@WebServlet("/Demo9")
public class RequestDemo9 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       Object msg=request.getAttribute("msg");
        System.out.println(msg);
        System.out.println("Demo9被访问了...");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
4. 获取ServletContext:
  • ServletContext getServletContext()
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext=request.getServletContext();
        System.out.println(servletContext);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

在这里插入图片描述

BeanUtils工具类,简化数据封装

  • 用于封装JavaBean的
1. JavaBean:标准的Java类
  1. 要求:
    1. 类必须被public修饰
    2. 必须提供空参的构造器
    3. 成员变量必须使用private修饰
    4. 提供公共setter和getter方法
  2. 功能:封装数据
2. 概念:

成员变量:定义的变量
属性:setter和getter方法截取后的产物
例如:getUsername() --> Username–> username

3. 方法:
  1. setProperty()
  2. getProperty()
  3. populate(Object obj , Map map):将map集合的键值对信息,封装到对应的JavaBean对象中

HTTP协议

1. 请求消息:客户端发送给服务器端的数据
* 数据格式:
	1. 请求行
	2. 请求头
	3. 请求空行
	4. 请求体
2. 响应消息:服务器端发送给客户端的数据
* 数据格式:
1. 响应行

1. 组成协议/版本 响应状态码 状态码描述
2. 响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态。
1. 状态码都是3位数字
2. 分类:
1. 1xx:服务器就收客户端消息,但没有接受完,等待一段时间后,发送1xx多状态码
2. 2xx:成功。代表:200
3. 3xx:重定向。代表:302(重定向),304(访问缓存)
4. 4xx客户端错误
* 代表:
* 404(请求路径没有对应的资源)
* 405:请求方式没有对应的doXxx方法
5. 5xx服务器端错误。代表:500(服务器内部出现异常)
在这里插入图片描述

2. 响应头:
  1. 格式:头名称: 值
  2. 常见的响应头:
    1. Content-Type:服务器告诉客户端本次响应体数据格式以及编码格式。浏览器会根据Content-Type里面的解码方式解码。防止乱码。
    2. Content-disposition:服务器告诉客户端以什么格式打开响应体数据
  • 值:
    * in-line:默认值,在当前页面内打开
    * attachment;filename=xxx:以附件形式打开响应体。文件下载
3. 响应空行

它是HTTP的head和body分割的标志

4. 响应体:传输的数据
  • 响应字符串格式
    html
    HTTP/1.1 200 OK
    Content-Type: text/html;charset=UTF-8
    Content-Length: 101
    Date: Wed, 06 Jun 2018 07:08:42 GMT
    <html>
    	  <head>
    	    <title>$Title$</title>
    	  </head>
    	  <body>
    	  hello , response
    	  </body>
    	</html>

Response对象

功能:设置响应消息
1. 设置响应行
  1. 格式:HTTP/1.1 200 ok
  2. 设置状态码:setStatus(int sc)
2. 设置响应头:

setHeader(String name, String value)

3. 设置响应体:

使用步骤

  1. 获取输出流

    • 字符输出流:PrintWriter getWriter()
    • 字节输出流:ServletOutputStream getOutputStream()
  2. 使用输出流,将数据输出到客户端浏览器

利用案例说明response方法
案例1:完成重定向
  • 重定向:资源跳转的方式
    在这里插入图片描述
  • 代码实现:
    //1. 设置状态码为302
    response.setStatus(302);
    //2.设置响应头location
    response.setHeader(“location”,"/day15/responseDemo2");

的重定向方法 response.sendRedirect("/day15/responseDemo2");

@WebServlet("/demo1")
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("demo1被访问了。。。。");
       //方法一:
        //设置状态码
        response.setStatus(302);
        //设置重定向路径
        response.setHeader("location","/Response_02/demo2");

        //方法二:简单的重定向
        response.sendRedirect("/Response_02/demo2");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doPost(request,response);
    }
}



@WebServlet("/demo2")
public class ResponseDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("demo2被访问了。。。");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doPost(request,response);
    }
}
forward 和 redirect 区别
重定向的特点:redirect
  1. 地址栏发生变化
  2. 重定向可以访问其他站点(服务器)的资源
  3. 重定向是两次请求,,两次响应。不能使用request对象来共享数据
转发的特点:forward
  1. 转发地址栏路径不变
  2. 转发只能访问当前服务器下的资源
  3. 转发是一次请求,一次响应。可以使用request对象来共享数据
路径写法
1. 路径分类

1. 相对路径:通过相对路径不可以确定唯一资源
如:./index.html
* 不以/开头,以.开头路径

规则:找到当前资源和目标资源之间的相对位置关系
1) ./:当前目录
2.)../:后退一级目录

2. 绝对路径:通过绝对路径可以确定唯一资源
* 如:http://localhost/day15/responseDemo2 /day15/responseDemo2
* 以/开头的路径

规则:判断定义的路径是给谁用的?判断请求将来从哪儿发出

  • 给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
  • 建议虚拟目录动态获取:request.getContextPath()
    * <a> , <form> 重定向...
    * 给服务器使用:不需要加虚拟目录
    * 转发路径
@WebServlet("/Demo3")
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo3来了");
        request.setAttribute("msg","reponse");

        //动态获取虚拟目录
        String contextPath=request.getContextPath();


        //简单的方法获取虚拟目录
        response.sendRedirect(contextPath+"/Demo4");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}


@WebServlet("/Demo4")
public class ResponseDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo4来了");
        Object msg=request.getAttribute("msg");
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
案例二. 服务器输出字符数据到浏览器
  • 步骤
    1. 获取字符输出流
    2. 输出数据

  • 注意:

乱码的原因:编解码使用的字符集不一致
乱码问题
1. PrintWriter pw = response.getWriter();获取的流的默认编码是ISO-8859-1
2. 设置该流的默认编码
3. 告诉浏览器响应体使用的编码

//简单的形式,设置编码,是在获取流之前设置
response.setContentType(“text/html;charset=utf-8”);

@WebServlet("/Demo5")
public class ResponseDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取流对象之前,设置编解码, 默认编码:ISO-8859-1  修改为  GBK
        response.setCharacterEncoding("utf-8");
        //告诉浏览器,服务器发送的消息数据的编码,减一浏览器使用该方式编解码
        response.setHeader("content-type","text/html;charset=utf-8");

        //简单的方法
        response.setContentType("text/html;charset=utf-8");
        //1.获取字符输出流
        PrintWriter pw=response.getWriter();
        //2.输出数据
        pw.write("hello");
        pw.write("你好");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

案例三. 服务器输出字节数据到浏览器
  • 步骤:
    1. 获取字节输出流
    2. 输出数据
实验4. 验证码
  1. 本质:图片
  2. 目的:防止恶意表单注册

@WebServlet("/check")
public class CheckCoder extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        int width=100;
        int height=50;
        //1.创建一个对象,在内存中图片(验证码对象)
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);

        //2.美化图片
        //2.1填充背景色
        Graphics g=image.getGraphics();//画笔对象
        g.setColor(Color.PINK);
        g.fillRect(0,0,width,height);
        //2.2花边框
        g.setColor(Color.BLACK);
        g.drawRect(0,0,width-1,height-1);
        //2.3写验证码
        //生成随机角标
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random ran=new Random();
        for(int i=1;i<5;i++){
            //生成下标
            int index=ran.nextInt(str.length());
            //获取字符
            char ch=str.charAt(index);
            //写验证码
            g.drawString(ch+"",width/5*i,height/2);
        }
        //2.4画干扰线
        for (int i=0;i<5;i++){
            //生成随机左边
            int x1=ran.nextInt(width);
            int x2=ran.nextInt(width);
            int y1=ran.nextInt(height);
            int y2=ran.nextInt(height);
            //划线
            g.drawLine(x1,x2,y1,y2);
        }
        //3.将图片输出到页面
        ImageIO.write(image,"jpg",response.getOutputStream());
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doPost(request,response);
    }
}

ServletContext对象:

1. 概念:代表整个web应用,可以和程序的容器(服务器)来通信
2. 获取
1. 通过request对象获取
request.getServletContext();
2. 通过HttpServlet获取
this.getServletContext();
两种方式获取的对象都是一样的

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过request获取对象
        ServletContext context=request.getServletContext();
        //2.通过HTTPServlet获取
        ServletContext context1=this.getServletContext();

        System.out.println(context);
        System.out.println(context1);

        System.out.println(context==context1);
    }

在这里插入图片描述
3. 功能
1. 获取MIME类型
* MIME类型:在互联网通信过程中定义的一种文件数据类型,可以和服务器通信。
* 格式: 大类型/小类型 text/html image/jpeg

  • 获取:String getMimeType(String file)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过HTTPServlet获取
        ServletContext context=this.getServletContext();

        //定义文件名称
        String filename="a.jpg";

        //获取MIME类型
        String mineType=context.getMimeType(filename);
        System.out.println(mineType);
    }

2. 域对象:共享数据
1. setAttribute(String name,Object value)
2. getAttribute(String name)
3. removeAttribute(String name)

  • ServletContext对象范围:所有用户所有请求的数据。生命周期长,长时间存在会对服务器造成压力

3. 获取文件的真实(服务器)路径
1. 方法:String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web目录下资源访问
System.out.println(b);

String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
System.out.println©;

String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
System.out.println(a);

public class ServletContentType3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过HTTPServlet获取
        ServletContext context=this.getServletContext();

        //获取文件服务器路径
        String realPath=context.getRealPath("b.txt");
        System.out.println(realPath);
        File file=new File(realPath);

        String c=context.getRealPath("?WEB-INF/a.txt");
        System.out.println(c);

        String b=context.getRealPath("WEB-INF/classes/b.txt");
        System.out.println(b);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值