码迷,mamicode.com
首页 > 其他好文 > 详细

request

时间:2018-08-03 18:03:05      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:path   throws   width   Servle   网站跳转   zhang   gif   pass   技术分享   

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

标签:path   throws   width   Servle   网站跳转   zhang   gif   pass   技术分享   

原文地址:https://www.cnblogs.com/hellowq/p/9415361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!