ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

5.1 获取请求行数据

2019-08-18 13:41:43  阅读:197  来源: 互联网

标签:5.1 String request 获取 yang println requestDemo1 请求


获取请求行数据

* GET /yang/demo1?name=zhangsan HTTP/1.1
* 方法:
	1. 获取请求方式 :GET
		* String getMethod()  
	2. (*)获取虚拟目录:/yang
		* String getContextPath()
	3. 获取Servlet路径: /demo1
		* String getServletPath()
	4. 获取get方式请求参数:name=zhangsan
		* String getQueryString()
	5. (*)获取请求URI:/yang/demo1
		* String getRequestURI():		/yang/demo1
		* StringBuffer getRequestURL()  :http://localhost/yang/demo
		* URL:统一资源定位符 : http://localhost/yang/demo1	中华人民共和国
		* URI:统一资源标识符 : /day14/demo1					共和国
	6. 获取协议及版本:HTTP/1.1
		* String getProtocol(
	7. 获取客户机的IP地址:
		* String getRemoteAddr()
package cn.ys.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;

/**
 * 演示Request对象获取请求行数据
 */
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 获取请求方式 :GET
        String method = request.getMethod();
        System.out.println(method);
        //2.(*)获取虚拟目录:如果tomcat配置有虚拟路径/yang,会显示为:/yang
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        //3. 获取Servlet路径: /requestDemo1
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        //4. 获取get方式请求参数:name=zhangsan
        String queryString = request.getQueryString();
        System.out.println(queryString);
        //5.(*)获取请求URI:/requestDemo1 如果tomcat配置有虚拟路径/yang,会显示为:/yang/requestDemo1
        String requestURI = request.getRequestURI(); // 显示为:/yang/requestDemo1
        StringBuffer requestURL = request.getRequestURL(); // ip:port ,显示为:http://localhost:8083/requestDemo1
        System.out.println(requestURI);
        System.out.println(requestURL);
        //6. 获取协议及版本:HTTP/1.1
        String protocol = request.getProtocol();
        System.out.println(protocol);
        //7. 获取客户机的IP地址:0:0:0:0:0:0:0:1
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
    }
}

启动程序,浏览器访问:http://localhost:8083/requestDemo1 ,控制台打印:

GET

/requestDemo1
null
/requestDemo1
http://localhost:8083/requestDemo1
HTTP/1.1
0:0:0:0:0:0:0:1

标签:5.1,String,request,获取,yang,println,requestDemo1,请求
来源: https://blog.csdn.net/weixin_42112635/article/details/99703713

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有