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以下的。就需要手动装换了。
时间: 2024-10-10 02:55:37

Request和Response的相关文章

Request和Response对象

最早接触Request和Response对象的时候是听王勇老师将Servlet的时候,我们编写一个Servlet类.我们在浏览器端发送一个url请求之后,会调用Servlet类对这个请求做相应的处理,Servlet类就是通过重写的service()方法来实现的,而我们知道的Request和Response对象就是作为service()方法的参数传入的.   1.整体印象 2.Request对象 Request对象的主要职责是获得HTTP请求中封装的信息,获得url和表单参数:还有一个功能就是对页

Fiddler抓包4-工具介绍(request和response)

前言 本篇简单的介绍下fiddler界面的几块区域,以及各自区域到底是干什么用的,以便于各好的掌握这个工具 一.工具简介 1.第一块区域是设置菜单,这个前面2篇都有介绍 2.第二块区域是一些快捷菜单,可以点下快捷功能键 3.第三块左边是抓捕的请求会话列表,每一个请求就是一个会话 4.第四块右边上方区域是request请求的详细信息,可以查看headerd.cookies.raw.json等 5.第五块右边下方区域就是response信息,可以查看服务端返回的json数据或其它信息 6.第六块区域

jsp九大内置对象:request、response、session、application、out、page、pageContext、config、exception

JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1.request对象 request 对象是 javax.servlet.httpServletRequest类型的对象. 该对象代表了客户端的请求信息,主要用于接受通过HTTP协议传送到服务器的数据.(包括头信息.系统信息.请求方式以及请求参数等).request对象的作用域为一次请求. 2.res

day03-java web之request、response

请求响应流程图 response 1        response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务器都会创建一个response对象,并传入给Servlet.service()方法.response对象是用来对客户端进行响应的,这说明在service()方法中使用response对象可以完成对客户端的响应工作. response对象的功能分为以下

Request 和 Response 原理

* Request 和 Response 原理:    * request对象和response对象由服务器创建,我们只需要在service方法中使用这两个对象即可        * 继承体系结构:        ServletRequest <--继承-- HttpServletRequest <--实现--- RequestFacade * Response:封装响应消息,设置响应消息    * 设置响应行:HTTP/1.1 200 ok        * setStatus(int co

ASP.NET中的Request、Response、Server对象

Request对象 Response.Write(Request.ApplicationPath) //应用根路径Request.AppRelativeCurrentExecutionFilePath //当前执行请求相对于应用根目录的虚拟路径,以-开头Request.PhysicalApplicationPath //当前应用的物理路径,如 D:\TEST\WEBRequest.PhysicalPath //当前请求的物理路径,如 D:\TEST\WEB\a.aspxRequest.RawUr

ASP.NET之Request和Response对象

经过了牛腩新闻公布系统和html的学习对B/S开发的流程有了些理解.前面尽管用到了非常多知识.但对制作网页仅仅能说知其然.当学到asp.net视频中的解说才干够说開始知其所以然了. 今天来说说client与server之间进行交流用到的Request和Response对象. Request:用户在client使用Web浏览器向Web应用程序发出请求时,会将client信息发给server.server收到一个HTTP请求,包括了全部查询字符串參数或表单參数.Cookie数据以及浏览器信息. 在a

Servlet(二)之request和response

一.简介 web服务器收到客户端的Http请求后,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象. request和response既然代表请求和响应,那么我们要获取客户端提交过来的数据,只需要找request对象就可以了,要向客户机输出数据,仅仅需要找response对象就行了. Response对象 二.响应 响应行 public void setStatus(int sc)设置状态码 响应头:key:value的形式,一个key可以对应一个v

struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用ServletActionContext的静态方法 Struts2 利用ServletActionContext类来维护Servlet对象,ServletActionContext利用ThreadLocal来维护 不同线程的Servlet对象,因此可以使用ServletActionContext类获取,这种方法

ASP.NET内置对象Request 和Response

ASP.NET提供了内置对象有Page.Request.Response.Application.Session.Server.Mail和Cookies.这些对象使用户更容易收集通过浏览器请求发送的信息.响应浏览器以及存储用户信息,以实现其他特定的状态管理和页面信息的传递. 下面针对request和response做简单介绍,大家多多指正,补充. Request 读取客户端在Web请求期间发送的值,即是读取浏览器传来的请求报文数据. Request对象主要是让服务器取得客户端浏览器的一些数据,包