上海移动网站建设,网站代理登录网址,电子商务主要学什么适合女生吗,设计网站大全下载总结#xff1a;JavaEE的Servlet中HttpServletRequest请求对象调用各种API方法结果示例 一方法调用顺序是按照英文字母顺序从A-Z二该示例可以用作servlet中request的API参考#xff0c;从而知道该如何获取哪些路径参数等等三Servlet的API版本5.0.0、JSP的API版本#xff1a;… 总结JavaEE的Servlet中HttpServletRequest请求对象调用各种API方法结果示例 一·方法调用顺序是按照英文字母顺序从A-Z二·该示例可以用作servlet中request的API参考从而知道该如何获取哪些路径参数等等三·Servlet的API版本5.0.0、JSP的API版本3.1.1、Tomcat的API版本10.1.14四·源代码五·浏览器发起一个GET请求结果示例1.GET请求示例2.GET请求响应结果文本以及截图 六·Postman发起一个POST请求结果示例1.POST请求示例2.POST请求响应结果文本以及截图 一·方法调用顺序是按照英文字母顺序从A-Z
二·该示例可以用作servlet中request的API参考从而知道该如何获取哪些路径参数等等
三·Servlet的API版本5.0.0、JSP的API版本3.1.1、Tomcat的API版本10.1.14 四·源代码
% page importjakarta.servlet.http.HttpSession %
% page importjakarta.servlet.http.Cookie %
% page importjakarta.servlet.http.Part %
% page importjava.util.* %
% page importjava.security.Principal %
% page importjakarta.servlet.ServletInputStream %
% page importjava.io.BufferedReader %
% page importjakarta.servlet.RequestDispatcher %
% page importjakarta.servlet.ServletContext %
% page importjakarta.servlet.http.HttpServletRequest %
% page importjakarta.servlet.DispatcherType %
% page importsun.misc.CompoundEnumeration %
% page importjava.net.URLDecoder %%--Created by IntelliJ IDEA.User: 刘明福Date: 2022/9/19Time: 17:30To change this template use File | Settings | File Templates.
--%
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitleTitle/title
/head
body
%//在请求会话里面存放两个键值对request.setAttribute(key1, 张三);request.setAttribute(key2, 李四);/*** 用于获取客户端对当前请求进行身份验证的方法的名称。它返回一个字符串表示身份验证方法的名称或者如果请求未经过身份验证则返回null。* getAuthType()方法返回的身份验证方法的名称通常是由服务器提供的并且取决于服务器的配置和实现。* 常见的身份验证方法包括BASIC、DIGEST、FORM等。如果请求未经过身份验证则返回null。*/String authType request.getAuthType();//获取异步上下文对象AsyncContext。异步上下文对象用于支持异步请求和响应处理
// jakarta.servlet.AsyncContext asyncContext request.getAsyncContext();//获取请求中所有属性的名称。它返回一个枚举Enumeration对象其中包含请求中所有属性的名称EnumerationString attributeNames request.getAttributeNames();//获取请求中指定属性的值。它接受一个字符串参数表示要获取的属性的名称并返回该属性的值。String attribute1 (String) request.getAttribute(key1);String attribute2 (String) request.getAttribute(key2);//获取请求类的class对象Class? extends HttpServletRequest aClass request.getClass();//获取当前Web应用程序的上下文路径String contextPath request.getContextPath();//用于获取服务器解析请求时确定的内容类型。String contentType request.getContentType();//用于获取当前请求的字符编码。它返回一个字符串表示当前请求使用的字符编码或者如果字符编码未指定则返回null。String characterEncoding request.getCharacterEncoding();//它返回一个Cookie数组其中包含当前请求的所有Cookie对象。Cookie[] cookies request.getCookies();//获取请求正文的长度。它返回一个整数表示请求正文的长度以字节为单位。int contentLength request.getContentLength();//用于获取请求正文的长度。它返回一个长整型表示请求正文的长度以字节为单位。long contentLengthLong request.getContentLengthLong();//获取请求的调度器类型。它返回一个DispatcherType枚举对象表示请求的调度器类型可以是REQUEST、FORWARD、INCLUDE、ASYNC或ERRORDispatcherType dispatcherType request.getDispatcherType();//用于从HTTP请求头中获取指定名称的日期值。它接受一个字符串参数表示要获取的HTTP请求头的名称然后返回该请求头的日期值。long dateHeader request.getDateHeader(Last-Modified);//返回一个枚举Enumeration对象该对象包含所有HTTP请求头的名称。可以通过遍历该枚举对象来获取每个请求头的名称。EnumerationString headerNames request.getHeaderNames();//获取指定的请求头值String header request.getHeader(Accept-Encoding);//用于获取客户端发送请求时指定的内容类型String header_ContentType request.getHeader(Content-Type);//返回一个枚举Enumeration对象该对象包含指定HTTP请求头的所有值。可以通过遍历该枚举对象来获取每个请求头的值。有的请求头可以同时存在多个值EnumerationString headers request.getHeaders(User-Agent);//获取请求的HttpServletMapping对象。HttpServletMapping对象表示请求的URL与Servlet的映射关系。HttpServletMapping httpServletMapping request.getHttpServletMapping();//获取一个请求头的整数值int intHeader_Content_Length request.getIntHeader(Content-Length);//用于获取请求的输入流。它返回一个ServletInputStream对象可以用于读取请求中的数据。例如读取post请求body中的数据ServletInputStream inputStream request.getInputStream();//获取服务器的IP地址。这个IP地址是服务器上网络接口的本地地址。String localAddr request.getLocalAddr();//获取服务器的主机名。这个主机名是服务器上网络接口的本地名称。String localName request.getLocalName();//返回服务器接收客户端连接的本地端口号。int localPort request.getLocalPort();//用于获取客户端的首选语言环境。它返回一个Locale对象表示客户端的首选语言环境。Locale locale request.getLocale();//用于获取客户端支持的所有语言环境。它返回一个枚举Enumeration对象其中包含客户端支持的所有语言环境。EnumerationLocale locales request.getLocales();//用于获取HTTP请求的方法。它返回一个字符串表示HTTP请求的方法例如GET、POST、PUT等。String method request.getMethod();/*** 用于获取HTTP请求中的指定部分,通常用于处理文件上传等场景。* file就是上传文件对应的key上传的文件二进制流会被封装进Part对象里面* 在使用getPart()方法之前需要确保请求中包含指定的部分否则可能会返回null。* 此外还需要注意处理文件上传时的文件名编码、文件大小限制等问题以确保安全和正确处理上传的文件。*/
// Part part request.getPart(file);//获取HTTP请求中的所有部分例如上传的文件。它返回一个Collection对象其中包含表示每个部分的Part对象。
// CollectionPart parts request.getParts();//getPathTranslated()方法返回一个字符串表示对应请求路径在服务器文件系统上的真实路径。它会将相对路径转换为服务器上的绝对路径。String pathTranslated request.getPathTranslated();//用于获取HTTP请求的协议。它返回一个字符串表示HTTP请求的协议例如HTTP/1.1String protocol request.getProtocol();//用于获取请求参数的映射。它返回一个Map对象其中包含请求参数的名称和对应的值。MapString, String[] parameterMap request.getParameterMap();//用于获取请求参数的名称。它返回一个枚举Enumeration对象其中包含请求参数的名称。EnumerationString parameterNames request.getParameterNames();//用于获取请求参数的值。它接受一个字符串参数表示要获取的参数的名称然后返回一个字符串数组包含该参数的所有值。String[] parameterValues request.getParameterValues(car);//用于获取请求的附加路径信息。它返回一个字符串表示请求的附加路径信息。String pathInfo request.getPathInfo();//用于获取请求参数的值。它接受一个字符串参数表示要获取的参数的名称然后返回该参数的值。String parameter_car request.getParameter(car);//用于获取请求参数的值。它接受一个字符串参数表示要获取的参数的名称然后返回该参数的值。String parameter_name request.getParameter(name);//获取路径uri后面拼接的所有参数键值对也称作请求的查询字符串也就是?后面拼接的所有参数String queryString request.getQueryString();//用于获取与当前请求关联的已验证用户的名称。它返回一个字符串表示发起请求的用户的名称或者如果用户未经过身份验证则返回null。String remoteUser request.getRemoteUser();//用于获取客户端请求中所包含的会话ID。它返回一个字符串表示会话ID或者如果请求中不包含会话ID则返回null。String requestedSessionId request.getRequestedSessionId();//将相对路径转为服务器上面的绝对路径String realPath request.getRealPath(/msdev/dongYaBank/request_Test.jsp);//返回客户端的IP地址即客户端的网络地址String remoteAddr request.getRemoteAddr();//返回客户端的主机名即客户端的计算机名。String remoteHost request.getRemoteHost();//返回客户端连接的端口号。int remotePort request.getRemotePort();//用于获取请求的URI统一资源标识符。它返回一个字符串表示请求的URI。String requestURI request.getRequestURI();//用于获取请求的URL统一资源定位器。它返回一个StringBuffer对象表示请求的URL。// URL是URI的子集uri只是端口后面的路径且不包括任何参数url包括协议后面的一切StringBuffer requestURL request.getRequestURL();//用于获取请求的字符输入流。它返回一个BufferedReader对象可以用于读取请求中的字符数据。
// BufferedReader reader request.getReader();//用于获取请求的转发器RequestDispatcher。转发器用于将请求转发到其他资源例如Servlet、JSP或其他Web组件。RequestDispatcher requestDispatcher request.getRequestDispatcher(/index.html);//获取当前请求的session会话HttpSession session_1 request.getSession();//用于获取Servlet上下文对象ServletContext。Servlet上下文对象是在Web应用程序级别共享的对象用于在不同的Servlet和JSP之间共享数据和资源。ServletContext servletContext request.getServletContext();//用于获取服务器的主机名hostname。它返回一个字符串表示服务器的主机名。String serverName request.getServerName();//返回服务器运行的端口号。int serverPort request.getServerPort();//用于获取请求的协议方案scheme。它返回一个字符串表示请求的协议方案例如http、https等。String scheme request.getScheme();//用于获取请求的Servlet路径。它返回一个字符串表示请求的Servlet路径。String servletPath request.getServletPath();//用于获取HTTP请求的尾部字段Trailer Fields。尾部字段是在HTTP请求的尾部包含的一些附加字段用于提供额外的请求信息。//尾部字段通常在使用Transfer-Encoding为chunked的情况下使用。MapString, String trailerFields request.getTrailerFields();//用于获取HTTP请求的用户主体User Principal。用户主体表示当前请求的用户身份信息通常用于进行身份验证和授权。// 它返回一个Principal对象表示用户主体。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%!/*** 打印parameterMap** param parameterMap 路径携带的参数键值对* return void* author LiuMingFu* date 2023/10/15*/public ListMapString, Object printParameterMap(MapString, String[] parameterMap) {ListMapString, Object resultData new LinkedList();for (Map.EntryString, String[] stringEntry : parameterMap.entrySet()) {MapString,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;}/*** 打印httpServletMapping** param httpServletMapping servlet映射对象* return void* author LiuMingFu* date 2023/10/15*/public MapString, Object printHttpServletMapping(HttpServletMapping httpServletMapping) {MapString,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;}/*** 遍历打印cookie数组** param cookies cookie数组* return void* author LiuMingFu* date 2023/10/15*/public ListMapString, Object printCookies(Cookie[] cookies) {ListMapString, Object resultData new LinkedList();for (Cookie cookie : cookies) {MapString, 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 ListT getEnumerationList(EnumerationT parameterNames, ClassT targetType) {ListT resultData new LinkedList();while (parameterNames.hasMoreElements()) {//获取下一个元素Object element parameterNames.nextElement();if (targetType.isInstance(element)) {resultData.add(targetType.cast(element));}}return resultData;}%
/body
/html
五·浏览器发起一个GET请求结果示例
1.GET请求示例
http://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp?nameidealage24jobJava开发工程师car问界M7car比亚迪仰光car五菱宏光2.GET请求响应结果文本以及截图
authTypenull
attributeNames[key1, key2]
attribute1张三
attribute2李四
aClassclass org.apache.catalina.connector.RequestFacade
contextPath/bookshop_22
contentTypenull
characterEncodingUTF-8
cookies[{pathnull, maxAge-1, domainnull, nameJSESSIONID, commentnull, securefalse, value5912481EAC59D7CB07FC54AA4CEAC973, classclass jakarta.servlet.http.Cookie, version0}, {pathnull, maxAge-1, domainnull, nameIdea-3b4ddd5e, commentnull, securefalse, valuea8522174-dd13-43ad-9a5e-6546ef665637, classclass jakarta.servlet.http.Cookie, version0}]
contentLength-1
contentLengthLong-1
dispatcherTypeREQUEST
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]
headergzip, deflate, br
header_ContentTypenull
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{mappingMatchEXTENSION, pattern*.jsp, matchValuemsdev/dongYaBank/request_Test, servletNamejsp, classclass org.apache.catalina.core.ApplicationMapping$MappingImpl}
intHeader_Content_Length-1
inputStreamorg.apache.catalina.connector.CoyoteInputStream2e3656be
localAddr0:0:0:0:0:0:0:1
localNamelocalhost
localPort8080
localezh_CN
locales[zh_CN, zh, en]
methodGET
pathTranslatednull
protocolHTTP/1.1
parameterMap[{keyname, value[ideal], classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keyage, value[24], classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keyjob, value[Java开发工程师], classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keycar, value[问界M7, 比亚迪仰光, 五菱宏光], classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}]
parameterNames[name, age, job, car]
parameterValues[问界M7, 比亚迪仰光, 五菱宏光]
pathInfonull
parameter_car问界M7
parameter_nameideal
queryStringnameidealage24jobJava开发工程师car问界M7car比亚迪仰光car五菱宏光
remoteUsernull
requestedSessionId5912481EAC59D7CB07FC54AA4CEAC973
realPath/Users/ideal/私人文件夹/JavaProjects/Javaweb基础练习/尚硅谷书城项目/bookshop_22/out/artifacts/bookshop_22_war_exploded/msdev/dongYaBank/request_Test.jsp
remoteAddr0:0:0:0:0:0:0:1
remoteHost0:0:0:0:0:0:0:1
remotePort65325
requestURI/bookshop_22/msdev/dongYaBank/request_Test.jsp
requestURLhttp://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp
requestDispatcherorg.apache.catalina.core.ApplicationDispatcher3c2b66ee
session_1org.apache.catalina.session.StandardSessionFacadeb6f8fd1
servletContextorg.apache.catalina.core.ApplicationContextFacade58672f7d
serverNamelocalhost
serverPort8080
schemehttp
servletPath/msdev/dongYaBank/request_Test.jsp
trailerFields{}
userPrincipalnull六·Postman发起一个POST请求结果示例
1.POST请求示例 2.POST请求响应结果文本以及截图
htmlheadtitleTitle/title
/headbodyauthTypenull/brattributeNames[key1, key2]/brattribute1张三/brattribute2李四/braClassclass org.apache.catalina.connector.RequestFacade/brcontextPath/bookshop_22/brcontentTypeapplication/xml/brcharacterEncodingUTF-8/brcookies[{pathnull, maxAge-1, domainnull, nameJSESSIONID, commentnull, securefalse,valueDA6AACA99B25F1C78FB7CA19D32A9559, classclass jakarta.servlet.http.Cookie, version0}]/brcontentLength0/brcontentLengthLong0/brdispatcherTypeREQUEST/brdateHeader-1/brheaderNames[ms-token, content-type, user-agent, accept, cache-control, postman-token, host, accept-encoding,connection, cookie, content-length]/brheadergzip, deflate, br/brheader_ContentTypeapplication/xml/brheaders[PostmanRuntime/7.33.0]/brhttpServletMapping{mappingMatchEXTENSION, pattern*.jsp, matchValuemsdev/dongYaBank/request_Test,servletNamejsp, classclass org.apache.catalina.core.ApplicationMapping$MappingImpl}/brintHeader_Content_Length0/brinputStreamorg.apache.catalina.connector.CoyoteInputStream48eb975b/brlocalAddr0:0:0:0:0:0:0:1/brlocalNamelocalhost/brlocalPort8080/brlocalezh_CN/brlocales[zh_CN]/brmethodPOST/brpathTranslatednull/brprotocolHTTP/1.1/brparameterMap[{keyname, value[ideal], classclassjava.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keyage, value[24], classclassjava.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keyjob, value[Java开发工程师],classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}, {keycar, value[问界M7,比亚迪仰光, 五菱宏光], classclass java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry}]/brparameterNames[name, age, job, car]/brparameterValues[问界M7, 比亚迪仰光, 五菱宏光]/brpathInfonull/brparameter_car问界M7/brparameter_nameideal/brqueryStringnameidealage24jobJava开发工程师car问界M7car比亚迪仰光car五菱宏光/brremoteUsernull/brrequestedSessionIdDA6AACA99B25F1C78FB7CA19D32A9559/brrealPath/Users/ideal/私人文件夹/JavaProjects/Javaweb基础练习/尚硅谷书城项目/bookshop_22/out/artifacts/bookshop_22_war_exploded/msdev/dongYaBank/request_Test.jsp/brremoteAddr0:0:0:0:0:0:0:1/brremoteHost0:0:0:0:0:0:0:1/brremotePort51562/brrequestURI/bookshop_22/msdev/dongYaBank/request_Test.jsp/brrequestURLhttp://localhost:8080/bookshop_22/msdev/dongYaBank/request_Test.jsp/brrequestDispatcherorg.apache.catalina.core.ApplicationDispatcher1a332a1b/brsession_1org.apache.catalina.session.StandardSessionFacade1328a40f/brservletContextorg.apache.catalina.core.ApplicationContextFacade58672f7d/brserverNamelocalhost/brserverPort8080/brschemehttp/brservletPath/msdev/dongYaBank/request_Test.jsp/brtrailerFields{}/bruserPrincipalnull/br/body/html