request

Posted hellowq

tags:

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

HttpservletRequest

简介和运行环境

我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加强大

request的运行流程

技术分享图片

request请求航的内容

技术分享图片

通过request获得请求行

获得客户端的请求方式:String getMethod()

获得请求的资源:

String getRequestURI()

StringBuffer getRequestURL()

String getContextPath() ---web应用的名称

String getQueryString() ---- get提交url地址后的参数字符串

username=zhangsan&password=123

技术分享图片
package com.ithiema.line;

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

public class LineServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //1、获得请求方式
        String method = request.getMethod();
        System.out.println("method:"+method);
        //2、获得请求的资源相关的内容
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("uri:"+requestURI);
        System.out.println("url:"+requestURL);
        //获得web应用的名称
        String contextPath = request.getContextPath();
        System.out.println("web应用:"+contextPath);
        //地址后的参数的字符串
        String queryString = request.getQueryString();
        System.out.println(queryString);
        //3、获得客户机的信息---获得访问者IP地址
        String remoteAddr = request.getRemoteAddr();
        System.out.println("IP:"+remoteAddr);
        
        
    }

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

获得客户机的一些信息   getRemoteAddr()获得访问的客户端ip地址

request获得请求头

ong getDateHeader(String name)

String getHeader(String name)

Enumeration getHeaderNames()

Enumeration getHeaders(String name)

int getIntHeader(String name)

referer头的作用:执行该此访问的的来源 做防盗链

技术分享图片
package com.ithiema.header;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeaderServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //1、获得指定的头
        String header = request.getHeader("User-Agent");
        System.out.println(header);
        //2、获得所有的头的名称
        Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            System.out.println(headerName+":"+headerValue);
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
View Code
技术分享图片
package com.ithiema.header;

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

public class RefererServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //对该新闻的来源的进行判断
        String header = request.getHeader("referer");
        if(header!=null&&header.startsWith("http://localhost")){
            //是从我自己的网站跳转过来的 可以看新闻
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("中国确实已经拿到100块金牌....");
        }else{
            response.getWriter().write("你是盗链者,可耻!!");
        }
        
        
    }

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

 通过request获得请求体

请求体中的内容是通过post提交的请求参数,格式是:

username=zhangsan&password=123&hobby=football&hobby=basketball

key ---------------------- value

username                    [zhangsan]

password                    [123]

hobby                         [football,basketball]

                       

 

以上面参数为例,通过一下方法获得请求参数:

String getParameter(String name)

String[] getParameterValues(String name)

Enumeration getParameterNames()

Map<String,String[]> getParameterMap()

 

 

      注意:get请求方式的请求参数 上述的方法一样可以获得

 

      解决post提交方式的乱码:request.setCharacterEncoding("UTF-8");

      解决get提交的方式的乱码:

            parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

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

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段

推进学说代码片段

Motan在服务provider端用于处理request的线程池

[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段

你如何在 python 中处理 graphql 查询和片段?

request.getParameter讲解