request和response

概念

在b/s架构中,请求和响应都是必须的,在我们跟网页进行交互的时候,我们需要使用request发送请求,当服务器接收到请求后,根据设定代码,返回响应包的内容。

当我们需要在页面上显示一些东西的时候,就需要使用response来打印。

request和response

  1. request和response对象是由服务器创建的。我们来使用它。
  2. request对象是来获取请求信息,response对象是来设置相应信息。

request

常用方法:

  • String getMethod() 获取请求方式
  • String getContextPath() 获取虚拟路径
  • String getServletPath() 获取Servlet路径
  • String getQueryString() 获取get请求方式参数
  • String getRequestURL() 获取url
  • String getProtocol() 获取协议版本
  • String getRemoteAddr() 获取客户端ip
  • String getHeader(String name) 通过请求头的名称获取请求头的值
  • Enumeration getHeaderNames() 获取所有的请求头名称

代码:

package Request;

import javax.jws.WebService;
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(urlPatterns = "/demo01")
public class Demo01 extends HttpServlet {
   protected void doGet(HttpServletRequest request, HttpServletResponse response){
       String header = request.getHeader("user-agent");//获取ua
       System.out.println(header);
       String method = request.getMethod();//获取请求方式
       System.out.println(method);
       String contextPath = request.getContextPath();//获取虚拟路径
       System.out.println(contextPath);
       String servletPath = request.getServletPath();//获取servlet路径
       System.out.println(servletPath);
       String queryString = request.getQueryString();//获取get请求参数
       System.out.println(queryString);
       String requestURL = request.getRequestURI();//获取url路径
       System.out.println(requestURL);
       //StringBuffer requestURL = request.getRequestURI();//获取完整url路径
       //System.out.println(requestURL);
   }


    protected void doPost(HttpServletResponse response,HttpServletRequest request){
       System.out.println("post请求");
   }
}

可以看到,通过这些方法,我们可以提取请求包里面的一些内容。

请求转发

在访问网站的过程中,我们经常会遇到一种情况,最常见的就是百度一下页面,在我们搜索指定的值之后,百度并不会在原本的页面直接给我们展示结果,而是会跳转到另外一个页面然后再将结果打印。

这就是servlet请求转发。

通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)

使用RequestDispatcher对象来进行转发:forward(ServletRequest request,ServletResponse response)

servlet1

package Request;

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("/demo02")
public class Demo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/demo00").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
    }
}

servlet2:

package Request;

import com.sun.net.httpserver.HttpServer;

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("/demo00")
public class Demo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("demo03被访问");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request,response);
    }
}

可以看到,我访问的地址是servlet1的地址,但是sevlet2里面的doGet方法被调用了。

这种方法,一般用于大型网站,因为这样会让功能更多样化,使用起来更方便。

Response

常用方法:

  • setStatus(int sc):设置响应状态码
  • setHeader(String name,String value):设置相应头
  • response.sendReadirect(String Redirect):302跳转

转发和重定向的区别

重定向:

  1. 地址栏发生变化
  2. 重定向可以访问其他站点(服务器)的资源
  3. 重定向是两次请求。不能使用request对象来共享数据

转发:

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

重定向代码

response类:

package Response;

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("/response")
public class Demo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
        response.setStatus(302);
        response.setHeader("location","demo01");
    }

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

这个代码在我们访问response的时候,就会自动帮我们跳转到demo01这个页面去。

ServletDemo类

package Response;

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("/demo01")
public class Demo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("已跳转");
        response.sendRedirect("https://www.baidu.com");
    }

    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request,response);
    }
}

write的使用

package Response;

import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
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("/response")
public class Demo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("gbk");
        PrintWriter writer = response.getWriter();
        writer.write("response");
        writer.write("12345");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request,response);
    }
}
posted @ 2020-12-14 22:03  小明-o3rr0r  阅读(267)  评论(0编辑  收藏  举报