Java日志二十三「request对象和response对象」

Servlet作为一个服务器,最主要的任务的就是与客户端进行交互。而request对象与response对象可以传递客户端与服务器的交互信息,实现交互功能。

交互过程:
1.tomcat会根据请求url中的资源路径创建对应的servlet实现类的对象
2.tomcat会创建request对象与response对象,request对象中封装请求消息
3.tomcat将request对象与response对象传递给service方法,并调用service方法
4.程序员通过request对象获取请求消息数据,通过response对象设置相应消息数据
5.服务器给浏览器作出相应之前,会从response对象中拿程序员设置的响应消息数据

**

HttpServletRequest

**
Request对象是由服务器创建的,ServletRequest接口——>HttpServletRequest接口——>org.apache.catalina.connector.RequestFacade类(这是一个tomcat写的实现类)。

我们在后面的使用中,都会使用HttpServletRequest对象,我们列举一下它的功能。

HttpServletRequest对象的功能一:获取请求行数据

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //Request对象的功能一:获取请求行数据

        //1.获取请求方式:
        String method = req.getMethod();
        System.out.println(method);

        //2.获取虚拟目录
        String contextPath = req.getContextPath();
        System.out.println(contextPath);

        //3获取servle请求路径
        String servletPath = req.getServletPath();
        System.out.println(servletPath);

        //4.获取get方式的请求参数
        String queryString = req.getQueryString();
        System.out.println(queryString);

        //5.获取URI与URL
        String requestURI = req.getRequestURI();
        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURI);
        System.out.println(requestURL);

        //6.获取协议以及版本
        String protocol = req.getProtocol();
        System.out.println(protocol);

        //7.获取客户机IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);
  }

结果

GET
/Servlet_Practice
/demo3
null
/Servlet_Practice/demo3
http://localhost:8080/Servlet_Practice/de
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值