Request和Response

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Request和Response相关的知识,希望对你有一定的参考价值。

1 简介

  • web服务器收到客户端的http请求,会针对每一个请求,分别创建一个用于代表请求的request对象和代表响应的response对象。
  • request和response对象既然代表请求和响应,那么获取客户端提交的数据,只需要找request对象,而向容器输出对象,只需要找response对象。

 

2 HttpServletResponse

  • HttpServletResponse对象代表服务器的响应,这个对象中封装了向客户端发送数据、发送响应头、发送响应状态码的方法。

 

  • 响应消息头:
public void addDateHeader(java.lang.String name,long date)
public void addHeader(java.lang.String name, java.lang.String value)
public void addIntHeader(java.lang.String name, int value)
public boolean containsHeader(java.lang.String name)
public void setHeader(java.lang.String name, java.lang.String value)
public void setIntHeader(java.lang.String name,int value)
public void setDateHeader(java.lang.String name,   long date)

 

  • 响应消息正文:
public ServletOutputStream getOutputStream()  throws java.io.IOException
public java.io.PrintWriter getWriter() throws java.io.IOException

 

  • 示例:字节流输出数据查本地码表
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "我爱你";
        OutputStream out = response.getOutputStream();
        out.write(data.getBytes());//String 的getBytes()默认在windows系统中默认上的是GBK

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <servlet>
        <servlet-name>ResponseDemo1</servlet-name>
        <servlet-class>com.servlet.ResponseDemo1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ResponseDemo1</servlet-name>
        <url-pattern>/responseDemo1</url-pattern>
    </servlet-mapping>
</web-app>

技术分享

 

 

 

技术分享

 

 

  • 示例:字节流输出数据查UTF-8
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "我爱你";
        OutputStream out = response.getOutputStream();
        out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <servlet>
        <servlet-name>ResponseDemo1</servlet-name>
        <servlet-class>com.servlet.ResponseDemo1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ResponseDemo1</servlet-name>
        <url-pattern>/responseDemo1</url-pattern>
    </servlet-mapping>
</web-app>

技术分享

 

技术分享

  • 解决方案:
    • ①手动修改浏览器的码表为UTF-8(不建议)              http://jingyan.baidu.com/article/d169e186a4e826436611d81e.html
    • ②向客户端输出一个meta标签,模拟响应消息头 (不建议,这种方式目前好像只适合于IE)    
<meta http-equiv=‘Content-Type‘ content=‘text/html;charset=UTF-8‘>
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "我爱你";
        OutputStream out = response.getOutputStream();
        out.write("<meta http-equiv=‘Content-Type‘ content=‘text/html;charset=UTF-8‘>".getBytes());
        out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
    • ③向客户端输出响应消息头:response.setHeader("Content-Typt","text/html;charset=UTF-8");
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setHeader("Content-Type","text/html;charset=UTF-8");
        String data = "我爱你";
        OutputStream out = response.getOutputStream();

        out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK

    }

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

 

技术分享

 

技术分享

 

 

    • ④向客户端输出响应消息头:response.setContentType("text/html;charset=UTF-8");  
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
        String data = "我爱你";
        OutputStream out = response.getOutputStream();

        out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK

    }

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

 

  • 示例:字符流输出中文内容
package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String data = "我爱你";
        PrintWriter out = response.getWriter();
        out.print(data);
    }

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

技术分享

 

  • 出现乱码,因为response对象是由Tomcat实现的,而Tomcat内部的码表是ISO8859-1。
  • 改变字符流默认的编码:response.setCharacterEncoding("UTF-8");
package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        String data = "我爱你";
        PrintWriter out = response.getWriter();
        out.print(data);
    }

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

技术分享

 

 

  • 因为:字符流是UTF-8,而浏览器本地的编码是GBK
  • 修改浏览器本地的编码:response.setContentType("text/html;charset=UTF-8");
package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String data = "我爱你";
        PrintWriter out = response.getWriter();
        out.print(data);
    }

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

技术分享

 

  • 示例:;定时刷新
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("refresh","2;URL=http://www.baidu.com");
    }

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

 

  • 示例:控制缓存
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //清除缓存
        response.setHeader("expires","-1");
        response.setHeader("pragma","no-cache");
        response.setHeader("cache-control","no-cache");
    }

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

 

  • 示例:请求重定向
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setStatus(302);
        response.setHeader("location",request.getContextPath()+"/responseDemo2");


    }

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

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

/**
 * 2017/11/5
 * 说明:
 */
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.sendRedirect(request.getContextPath()+"/responseDemo2");


    }

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

 

3 HttpServletRequest

  • 常用方法:
public java.lang.String getMethod()
public java.lang.String getRequestURI()
public java.lang.StringBuffer getRequestURL()
public java.lang.String getProtocol()
public java.lang.String getParameter(java.lang.String name)
public java.lang.String[] getParameterValues(java.lang.String name)
public java.util.Enumeration getParameterNames()
public java.util.Map getParameterMap()
public ServletInputStream getInputStream()
public java.lang.String getRemoteAddr()

 

 

  • 示例:
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class RequestServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        String method = request.getMethod();//获取请求方式
        String uri = request.getRequestURI();
        String url = request.getRequestURL().toString();
        String protocol = request.getProtocol();
        String ip = request.getRemoteAddr();//ip




        response.getWriter().println("方法:"+method+"<br/>");
        response.getWriter().println("uri:"+uri+"<br/>");
        response.getWriter().println("url:"+url+"<br/>");
        response.getWriter().println("protocol:"+protocol+"<br/>");
        response.getWriter().println("ip:"+ip+"<br/>");


    }

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

技术分享

  • 示例:获取请求参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
<table>
    <form action="${pageContext.request.contextPath}/register" method="post">
        <tr>
            <td>用户名</td>
            <td>
                <input type="text" name="username"/>
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td>
                <input type="password" name="password"/>
            </td>
        </tr>
        <tr>
            <td>爱好</td>
            <td>
                <input type="checkbox" name="hobby" value="游戏"/>游戏
                <input type="checkbox" name="hobby" value="开发"/>开发
                <input type="checkbox" name="hobby" value="电视"/>电视
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="注册"/>
            </td>
        </tr>

    </form>
</table>
</body>
</html>
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobby = request.getParameterValues("hobby");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        System.out.println("爱好:"+ Arrays.toString(hobby));
    }

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

 

  • 示例:post请求
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobby = request.getParameterValues("hobby");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        System.out.println("爱好:"+ Arrays.toString(hobby));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
  • 通过request.setCharacterEncoding("utf-8") 设置请求消息内容的编码为utf-8

 

  • 示例:get请求
package com.servlet;

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

/**
 * 2017/11/5
 * 说明:
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobby = request.getParameterValues("hobby");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        System.out.println("爱好:"+ Arrays.toString(hobby));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
  • 注意:Tomcat8.0以上的版本使用的编码是UTf-8,所以不需要改变手动转换,如果是Tomcat8.0以下的。就需要手动装换了。

 

以上是关于Request和Response的主要内容,如果未能解决你的问题,请参考以下文章

Request和Response

javaWeb中request请求转发和response重定向

request重定向或者是response转发请求后面的代码依然执行

WinForm窗体程序中使用CefSharp获取加载后的资源截取request参数拦截response数据注入jquery文件和js代码-拦截response数据

WinForm窗体程序中使用CefSharp获取加载后的资源截取request参数拦截response数据注入jquery文件和js代码-截取request参数

Request和Response