Http和response

1. http响应消息

http协议分为两个部分:
1、请求消息:客户端发送给服务器端的数据
2、相应消息:服务器发送给客户端的数据

数据格式

1、响应行

组成:协议+版本+相应状态码
HTTP/1.1 (404、200、500等)
Http和response_第1张图片

2、响应头
Content-Type:text/html:charset=utf-8
Content-Language:en
Content-Length:1085
Http和response_第2张图片

3、相应空行

4、响应体
响应发送的数据

Http和response_第3张图片

2. Response对象

功能:设置响应消息
1.1、设置响应行,设置状态码setStatus(int sc);

//设置状态码302
response.setStatus(302);

1.2、设置响应头

setHeader(String name,String value)

1.3、设置响应体…

package sc;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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.io.PrintWriter;

@WebServlet("/test4")
public class test4 extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        response.setStatus(302);
        response.setHeader("location","/test5");

        //设置响应体   以流的方式输出
        PrintWriter writer = response.getWriter();
        writer.write("我是response");
        //以字节流的方式传输
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write("我是responser".getBytes("utf-8"));
    }
 }

response流程图为
Http和response_第4张图片

3.重定向

重定向特点redirect

1、地址栏发生变化
2、重定向可以访问其他服务器的资源,例如写为其他网站的http网址
3、重定向是两次请求,所以不能用request域来共享数据

转发特点
1、转发地址栏路径不变
2、转发只能访问当前服务器下的资源
3、转发试一次请求,可以使用request对象来共享数据

//测试是否能共享数据
request.setAttribute("user","usernamezhangshan");
System.out.println("得到的共享数据servlet4中:"+request.getAttribute("user"));

response.sendRedirect("/test5");
package sc;

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;

@WebServlet("/test5")
public class test5 extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        System.out.println("重定向过来的");
        Object user = request.getAttribute("user");
        System.out.println("通过共享数据获得:"+user);
    }

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

4. ServletContext对象

package sc;

import cn.dsna.util.images.ValidateCode;

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

@WebServlet("/test6")
public class test6 extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
//        response.setContentType("text/html:charset=utf-8");
//        ServletOutputStream outputStream = response.getOutputStream();
//        outputStream.write("哈哈哈".getBytes("utf-8"));

        //相应一个验证码
        ValidateCode validateCode = new ValidateCode(200,100,4,10);
        validateCode.write(response.getOutputStream());
    }
}

你可能感兴趣的:(java)