总结:JavaEE的Servlet中HttpServletRequest请求对象调用各种API方法结果示例
- 一·方法调用顺序是按照英文字母顺序从A-Z
- 二·该示例可以用作servlet中request的API参考,从而知道该如何获取哪些路径参数等等
- 三·Servlet的API版本5.0.0、JSP的API版本:3.1.1、Tomcat的API版本:10.1.14
- 四·JSP的HttpServletRequest调用各个方法的代码示例:
- 五·浏览器发起GET请求上面JSP页面结果示例:
-
- 1.GET请求示例:
- 2.GET请求响应结果:文本以及截图
- 六·Postman发起POST请求上面JSP页面结果示例:
-
- 1.POST请求示例:
- 2.POST请求响应结果:文本以及截图
一·方法调用顺序是按照英文字母顺序从A-Z
二·该示例可以用作servlet中request的API参考,从而知道该如何获取哪些路径参数等等
三·Servlet的API版本5.0.0、JSP的API版本:3.1.1、Tomcat的API版本:10.1.14
四·JSP的HttpServletRequest调用各个方法的代码示例:
<%@ page import="jakarta.servlet.http.HttpSession" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page import="jakarta.servlet.http.Part" %>
<%@ page import="java.util.*" %>
<%@ page import="java.security.Principal" %>
<%@ page import="jakarta.servlet.ServletInputStream" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="jakarta.servlet.RequestDispatcher" %>
<%@ page import="jakarta.servlet.ServletContext" %>
<%@ page import="jakarta.servlet.http.HttpServletRequest" %>
<%@ page import="jakarta.servlet.DispatcherType" %>
<%@ page import="sun.misc.CompoundEnumeration" %>
<%@ page import="java.net.URLDecoder" %><%--
Created by IntelliJ IDEA.
User: 刘明福
Date: 2022/9/19
Time: 17:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("key1", "张三");
request.setAttribute("key2", "李四");
String authType = request.getAuthType();
Enumeration<String> attributeNames = request.getAttributeNames();
String attribute1 = (String) request.getAttribute("key1");
String attribute2 = (String) request.getAttribute("key2");
Class<? extends HttpServletRequest> aClass = request.getClass();
String contextPath = request.getContextPath();
String contentType = request.getContentType();
String characterEncoding = request.getCharacterEncoding();
Cookie[] cookies = request.getCookies();
int contentLength = request.getContentLength();
long contentLengthLong = request.getContentLengthLong();
DispatcherType dispatcherType = request.getDispatcherType();
long dateHeader = request.getDateHeader("Last-Modified");
Enumeration<String> headerNames = request.getHeaderNames();
String header = request.getHeader("Accept-Encoding");
String header_ContentType = request.getHeader("Content-Type");
Enumeration<String> headers = request.getHeaders("User-Agent");
HttpServletMapping httpServletMapping = request.getHttpServletMapping();
int intHeader_Content_Length = request.getIntHeader("Content-Length");
ServletInputStream inputStream = request.getInputStream();
String localAddr = request.getLocalAddr();
String localName = request.getLocalName();
int localPort = request.getLocalPort();
Locale locale = request.getLocale();
Enumeration<Locale> locales = request.getLocales();
String method = request.getMethod();
String pathTranslated = request.getPathTranslated();
String protocol = request.getProtocol();
Map<String, String[]> parameterMap = request.getParameterMap();
Enumeration<String> parameterNames = request.getParameterNames();
String[] parameterValues = request.getParameterValues("car");
String pathInfo = request.getPathInfo();
String parameter_car = request.getParameter("car");
String parameter_name = request.getParameter("name");
String queryString = request.getQueryString();
String remoteUser = request.getRemoteUser();
String requestedSessionId = request.getRequestedSessionId();
String realPath = request.getRealPath("/msdev/dongYaBank/request_Test.jsp");
String remoteAddr = request.getRemoteAddr();
String remoteHost = request.getRemoteHost();
int remotePort = request.getRemotePort();
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.html");
HttpSession session_1 = request.getSession();
ServletContext servletContext = request.getServletContext();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String scheme = request.getScheme();
String servletPath = request.getServletPath();
Map<String, String> trailerFields = request.getTrailerFields();
Principal userPrincipal = request.getUserPrincipal();
%>
authType=:<%=authType%></br>
<%--asyncContext=:<%=asyncContext.toString()%></br>--%>
attributeNames=:<%=getEnumerationList(attributeNames,String.class)%></br>
attribute1=:<%=attribute1%></br>
attribute2=:<%=attribute2%></br>
aClass=:<%=aClass%></br>
contextPath=:<%=contextPath%></br>
contentType=:<%=contentType%></br>
characterEncoding=:<%=characterEncoding%></br>
cookies=:<%=printCookies(cookies)%></br>
contentLength=:<%=contentLength%></br>
contentLengthLong=:<%=contentLengthLong%></br>
dispatcherType=:<%=dispatcherType%></br>
dateHeader=:<%=dateHeader%></br>
headerNames=:<%=getEnumerationList(headerNames,String.class)%></br>
header=:<%=header%></br>
header_ContentType=:<%=header_ContentType%></br>
headers=:<%=getEnumerationList(headers,String.class)%></br>
httpServletMapping=:<%=printHttpServletMapping(httpServletMapping)%></br>
intHeader_Content_Length=:<%=intHeader_Content_Length%></br>
inputStream=:<%=inputStream%></br>
localAddr=:<%=localAddr%></br>
localName=:<%=localName%></br>
localPort=:<%=localPort%></br>
locale=:<%=locale%></br>
locales=:<%=getEnumerationList(locales,Locale.class)%></br>
method=:<%=method%></br>
<%--part=:<%=part%></br>--%>
<%--parts=:<%=parts%></br>--%>
pathTranslated=:<%=pathTranslated%></br>
protocol=:<%=protocol%></br>
parameterMap=:<%=printParameterMap(parameterMap)%></br>
parameterNames=:<%=getEnumerationList(parameterNames,String.class)%></br>
parameterValues=:<%=Arrays.toString(parameterValues)%></br>
pathInfo=:<%=pathInfo%></br>
parameter_car=:<%=parameter_car%></br>
parameter_name=:<%=parameter_name%></br>
queryString=:<%=URLDecoder.decode(queryString)%></br>
remoteUser=:<%=remoteUser%></br>
requestedSessionId=:<%=requestedSessionId%></br>
realPath=:<%=realPath%></br>
remoteAddr=:<%=remoteAddr%></br>
remoteHost=:<%=remoteHost%></br>
remotePort=:<%=remotePort%></br>
requestURI=:<%=requestURI%></br>
requestURL=:<%=requestURL%></br>
<%--reader=:<%=reader%></br>--%>
requestDispatcher=:<%=requestDispatcher%></br>
session_1=:<%=session_1%></br>
servletContext=:<%=servletContext%></br>
serverName=:<%=serverName%></br>
serverPort=:<%=serverPort%></br>
scheme=:<%=scheme%></br>
servletPath=:<%=servletPath%></br>
trailerFields=:<%=trailerFields%></br>
userPrincipal=:<%=userPrincipal%></br>
<%!
public List<Map<String, Object>> printParameterMap(Map<String, String[]> parameterMap) {
List<Map<String, Object>> resultData = new LinkedList<>();
for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
Map<String,Object> one = new LinkedHashMap<>();
one.put("key",stringEntry.getKey());
one.put("value",Arrays.toString(stringEntry.getValue()));
one.put("class",stringEntry.getClass());
resultData.add(one);
}
return resultData;
}
public Map<String, Object> printHttpServletMapping(HttpServletMapping httpServletMapping) {
Map<String,Object> resultData = new HashMap<>();
resultData.put("matchValue",httpServletMapping.getMatchValue());
resultData.put("servletName",httpServletMapping.getServletName());
resultData.put("pattern",httpServletMapping.getPattern());
resultData.put("class",httpServletMapping.getClass());
resultData.put("mappingMatch",httpServletMapping.getMappingMatch());
return resultData;
}
public List<Map<String, Object>> printCookies(Cookie[] cookies) {
List<Map<String, Object>> resultData = new LinkedList<>();
for (Cookie cookie : cookies) {
Map<String, Object> map = new HashMap<>();
map.put("name", cookie.getName());
map.put("value", cookie.getValue());
map.put("comment", cookie.getComment());
map.put("domain", cookie.getDomain());
map.put("path", cookie.getPath());
map.put("maxAge", cookie.getMaxAge());
map.put("secure", cookie.getSecure());
map.put("class", cookie.getClass());
map.put("version", cookie.getVersion());
resultData.add(map);
}
return resultData;
}
public <T> List<T> getEnumerationList(Enumeration<T> parameterNames, Class<T> targetType) {
List<T> resultData = new LinkedList<>();
while (parameterNames.hasMoreElements()) {
Object element = parameterNames.nextElement();
if (targetType.isInstance(element)) {
resultData.add(targetType.cast(element));
}
}
return resultData;
}
%>
</body>
</html>
五·浏览器发起GET请求上面JSP页面结果示例:
1.GET请求示例:
http://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp?name=ideal&age=24&job=Java开发工程师&car=问界M7&car=比亚迪仰光&car=五菱宏光
2.GET请求响应结果:文本以及截图
authType=:null
attributeNames=:[key1, key2]
attribute1=:张三
attribute2=:李四
aClass=:class org.apache.catalina.connector.RequestFacade
contextPath=:/bookshop_22
contentType=:null
characterEncoding=:UTF-8
cookies=:[{path=null, maxAge=-1, domain=null, name=JSESSIONID, comment=null, secure=false, value=5912481EAC59D7CB07FC54AA4CEAC973, class=class jakarta.servlet.http.Cookie, version=0}, {path=null, maxAge=-1, domain=null, name=Idea-3b4ddd5e, comment=null, secure=false, value=a8522174-dd13-43ad-9a5e-6546ef665637, class=class jakarta.servlet.http.Cookie, version=0}]
contentLength=:-1
contentLengthLong=:-1
dispatcherType=:REQUEST
dateHeader=:-1
headerNames=:[host, connection, cache-control, sec-ch-ua, sec-ch-ua-mobile, sec-ch-ua-platform, upgrade-insecure-requests, user-agent, accept, sec-fetch-site, sec-fetch-mode, sec-fetch-user, sec-fetch-dest, accept-encoding, accept-language, cookie]
header=:gzip, deflate, br
header_ContentType=:null
headers=:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36]
httpServletMapping=:{mappingMatch=EXTENSION, pattern=*.jsp, matchValue=msdev/dongYaBank/request_Test, servletName=jsp, class=class org.apache.catalina.core.ApplicationMapping$MappingImpl}
intHeader_Content_Length=:-1
inputStream=:org.apache.catalina.connector.CoyoteInputStream@2e3656be
localAddr=:0:0:0:0:0:0:0:1
localName=:localhost
localPort=:8080
locale=:zh_CN
locales=:[zh_CN, zh, en]
method=:GET
pathTranslated=:null
protocol=:HTTP/1.1
parameterMap=:[{key=name, value=[ideal], class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=age, value=[24], class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=job, value=[Java开发工程师], class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=car, value=[问界M7, 比亚迪仰光, 五菱宏光], class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}]
parameterNames=:[name, age, job, car]
parameterValues=:[问界M7, 比亚迪仰光, 五菱宏光]
pathInfo=:null
parameter_car=:问界M7
parameter_name=:ideal
queryString=:name=ideal&age=24&job=Java开发工程师&car=问界M7&car=比亚迪仰光&car=五菱宏光
remoteUser=:null
requestedSessionId=:5912481EAC59D7CB07FC54AA4CEAC973
realPath=:/Users/ideal/私人文件夹/JavaProjects/Javaweb基础练习/尚硅谷书城项目/bookshop_22/out/artifacts/bookshop_22_war_exploded/msdev/dongYaBank/request_Test.jsp
remoteAddr=:0:0:0:0:0:0:0:1
remoteHost=:0:0:0:0:0:0:0:1
remotePort=:65325
requestURI=:/bookshop_22/msdev/dongYaBank/request_Test.jsp
requestURL=:http://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp
requestDispatcher=:org.apache.catalina.core.ApplicationDispatcher@3c2b66ee
session_1=:org.apache.catalina.session.StandardSessionFacade@b6f8fd1
servletContext=:org.apache.catalina.core.ApplicationContextFacade@58672f7d
serverName=:localhost
serverPort=:8080
scheme=:http
servletPath=:/msdev/dongYaBank/request_Test.jsp
trailerFields=:{}
userPrincipal=:null
六·Postman发起POST请求上面JSP页面结果示例:
1.POST请求示例:
2.POST请求响应结果:文本以及截图
<html>
<head>
<title>Title</title>
</head>
<body>
authType=:null</br>
attributeNames=:[key1, key2]</br>
attribute1=:张三</br>
attribute2=:李四</br>
aClass=:class org.apache.catalina.connector.RequestFacade</br>
contextPath=:/bookshop_22</br>
contentType=:application/xml</br>
characterEncoding=:UTF-8</br>
cookies=:[{path=null, maxAge=-1, domain=null, name=JSESSIONID, comment=null, secure=false,
value=DA6AACA99B25F1C78FB7CA19D32A9559, class=class jakarta.servlet.http.Cookie, version=0}]</br>
contentLength=:0</br>
contentLengthLong=:0</br>
dispatcherType=:REQUEST</br>
dateHeader=:-1</br>
headerNames=:[ms-token, content-type, user-agent, accept, cache-control, postman-token, host, accept-encoding,
connection, cookie, content-length]</br>
header=:gzip, deflate, br</br>
header_ContentType=:application/xml</br>
headers=:[PostmanRuntime/7.33.0]</br>
httpServletMapping=:{mappingMatch=EXTENSION, pattern=*.jsp, matchValue=msdev/dongYaBank/request_Test,
servletName=jsp, class=class org.apache.catalina.core.ApplicationMapping$MappingImpl}</br>
intHeader_Content_Length=:0</br>
inputStream=:org.apache.catalina.connector.CoyoteInputStream@48eb975b</br>
localAddr=:0:0:0:0:0:0:0:1</br>
localName=:localhost</br>
localPort=:8080</br>
locale=:zh_CN</br>
locales=:[zh_CN]</br>
method=:POST</br>
pathTranslated=:null</br>
protocol=:HTTP/1.1</br>
parameterMap=:[{key=name, value=[ideal], class=class
java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=age, value=[24], class=class
java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=job, value=[Java开发工程师],
class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {key=car, value=[问界M7,
比亚迪仰光, 五菱宏光], class=class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}]</br>
parameterNames=:[name, age, job, car]</br>
parameterValues=:[问界M7, 比亚迪仰光, 五菱宏光]</br>
pathInfo=:null</br>
parameter_car=:问界M7</br>
parameter_name=:ideal</br>
queryString=:name=ideal&age=24&job=Java开发工程师&car=问界M7&car=比亚迪仰光&car=五菱宏光</br>
remoteUser=:null</br>
requestedSessionId=:DA6AACA99B25F1C78FB7CA19D32A9559</br>
realPath=:/Users/ideal/私人文件夹/JavaProjects/Javaweb基础练习/尚硅谷书城项目/bookshop_22/out/artifacts/bookshop_22_war_exploded/msdev/dongYaBank/request_Test.jsp</br>
remoteAddr=:0:0:0:0:0:0:0:1</br>
remoteHost=:0:0:0:0:0:0:0:1</br>
remotePort=:51562</br>
requestURI=:/bookshop_22/msdev/dongYaBank/request_Test.jsp</br>
requestURL=:http://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp</br>
requestDispatcher=:org.apache.catalina.core.ApplicationDispatcher@1a332a1b</br>
session_1=:org.apache.catalina.session.StandardSessionFacade@1328a40f</br>
servletContext=:org.apache.catalina.core.ApplicationContextFacade@58672f7d</br>
serverName=:localhost</br>
serverPort=:8080</br>
scheme=:http</br>
servletPath=:/msdev/dongYaBank/request_Test.jsp</br>
trailerFields=:{}</br>
userPrincipal=:null</br>
</body>
</html>