2020.04.17 Request获取请求行数据
作者:互联网
package com.aojie.web.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;
/**
* @author aojie
* @fuction
* @create 2020-04-17 19:52
*/
@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.获取请求方式:getMethod()
2.(*)获取虚拟目录 /day14:getContextPath()
3.获取Servlet路径 /demo4:getServletPath()
4.获取get方法的请求参数:name=zhangsan
*getQueryString()
5.(*)获取请求URI /day14/demo4:
*getRequestURI()
*getRequestURL() 完整的url
*http:localhost/day14/demo4
6.获取协议及版本 http/1.1
*getProtocol()
7.获取客户机的ip地址
*getRemoteAddr()
*/
//获取请求方式
String method=request.getMethod();
System.out.println(method);
//获取虚拟目录
String contextpath=request.getContextPath();
System.out.println(contextpath);
//获取Servlet路径
String servletPath = request.getServletPath();
System.out.println(servletPath);
//获取请求参数
String queryString = request.getQueryString();
System.out.println(queryString);
//获取请求url
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//获取协议和版本
String protocol = request.getProtocol();
System.out.println(protocol);
//获取客户机ip
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
标签:String,17,Request,request,2020.04,System,获取,println,out 来源: https://www.cnblogs.com/aojie/p/12723441.html