zoukankan      html  css  js  c++  java
  • httpServletResquire 和httpServletResponse

    package com.bjsxt.servlet.request;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class ServeltRequest
    */
    @WebServlet("/ServeltRequest")
    public class ServeltRequest extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    //请求信息
    //请求行(请求方式,资源路径,协议和版本号)
    getrequestline(req,resp);
    //请求头
    //请求实体内容
    //获取网络信息
    }

    private void getrequestline(HttpServletRequest req, HttpServletResponse resp) {
    System.out.println("ServeltRequest.getrequestline(获取请求方式):"+req.getMethod());
    System.out.println("ServeltRequest.getrequestline(资源路径uri):"+req.getRequestURI());
    System.out.println("ServeltRequest.getrequestline(资源路径url):"+req.getRequestURL());
    System.out.println("ServeltRequest.getrequestline(协议):"+req.getScheme());
    System.out.println("ServeltRequest.getrequestline(版本号)"+req.getProtocol());
    System.out.println("ServeltRequest.getrequestline(项目路径)"+req.getContextPath());
    System.out.println("ServeltRequest.getrequestline(请求参数)"+req.getQueryString());


    }


    }

    package com.bjsxt.servlet.response;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    //HttpServletResponse:包含所有的响应信息,对象由servlet容器创建

    public class ServletResponse extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    //响应信息格式
    //响应行(响应协议和版本 ,状态吗,状态吗描述)-- 响应行servlet自动处理
    //响应头
    //设置响应头(同名的响应头会被覆盖)
    resp.setHeader("iphone", "iphone7");
    resp.setHeader("iphone", "iphone8");
    //添加响应头(同名的响应头不会被覆盖)
    resp.addHeader("vivo", "vivoX5");
    resp.addHeader("vivo", "vivoX6");

    //设置浏览器的解码格式
    //resp.setHeader("content-type", "text/html;charset=utf-8");
    //简单写法
    resp.setContentType("text/html;charset=utf-8");

    //响应的实体内容
    PrintWriter pw = resp.getWriter();
    pw.print("<html>");
    pw.print("<head>");
    pw.print("</head>");
    pw.print("<body>");
    pw.print("<font style='color:red'>");
    pw.print("星期天去西单看美女");
    pw.print("</font>");
    pw.print("</body>");
    pw.print("</html>");
    }


    }

  • 相关阅读:
    Ubuntu系统---安装思维导图XMind
    Git---Ubuntu下的安装与使用
    YOLO---YOLOv3 with OpenCV安装与使用
    Keras---Virtualenv 下安装Keras (基于Tensorflow后端)
    Python---virtualenv + Tensorflow + 安装jupyter notebook
    Ubuntu系统---以virtualenv方式安装Tensorflow-CPU
    Caffe---Pycaffe 绘制loss和accuracy曲线
    Caffe---自带工具 绘制loss和accuracy曲线
    day91 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
    day 90 DjangoRestFramework学习二之序列化组件
  • 原文地址:https://www.cnblogs.com/wq-9/p/10185786.html
Copyright © 2011-2022 走看看