首页 > 其他分享 >HttpServletRequest 方法详解

HttpServletRequest 方法详解

时间:2023-09-20 14:02:28浏览次数:27  
标签:HttpServletRequest 返回 String request 详解 println 请求 方法 out


request.setCharacterEncoding("utf-8");//设置request编码方式
		request.getLocalAddr();//获取本地IP,即服务器IP
		request.getLocalName();//获取本地名称,即服务器名称
		request.getLocalPort();//获取本地端口号,即Tomcat端口号
		request.getLocale();//用户的语言环境
		request.getContextPath();//context路径
		request.getMethod();//GET还是POST
		request.getProtocol();//协议,http协议
		request.getQueryString();//查询字符串
		request.getRemoteAddr();//远程IP,即客户端IP
		request.getRemotePort();//远程端口,即客户端端口
		request.getRemoteUser();//远程用户
		request.getRequestedSessionId();//客户端的Session的ID
		request.getRequestURI();//用户请求的URL
		request.getScheme();//协议头,例如http
		request.getServerName();//服务器名称
		request.getServerPort();//服务器端口
		request.getServletPath();//Servlet路径

 

<%@page import="java.security.Principal"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="com.thinkgem.jeesite.common.mapper.JsonMapper"%>
<%@page import="com.google.common.collect.Maps"%>
<%@page import="org.apache.commons.lang3.StringUtils"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%!
public void getReqInfo(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html; charset = utf-8");
    PrintWriter out = response.getWriter();
    println(out,"<ol>");
    //1. 获取请求方式、处理乱码问题
    String method = request.getMethod();

    //servletRequest中的方法
    request.setCharacterEncoding("utf-8");
    //1. 获取请求体的编码方式
    String characterEncoding = request.getCharacterEncoding();
    println(out,"getCharacterEncoding = " + characterEncoding);
    
    //2. get body length
    int contentLength = request.getContentLength();
    println(out,"getContentLength = " + contentLength);
    
    //3. MIME type
    String mimeType = request.getContentType();
    println(out,"getContentType = " + mimeType);
    
    //4. 接收请求的接口的 Internet Protocol (IP) 地址
    String ip = request.getLocalAddr();
    println(out,"getLocalAddr = " + ip);
    
    //5. 基于 Accept-Language 头,返回客户端将用来接受内容的首选 Locale 客户端语言环境
    Locale locale = request.getLocale();
    println(out,"getLocale = " + locale);
    
    //6. 所有的语言环境
    Enumeration<Locale> locales = request.getLocales();
    while(locales.hasMoreElements()){
        Locale temp = locales.nextElement();
        println(out,"\n Locales = " + temp);
    }
    
    //7. 接收请求的 Internet Protocol (IP) 接口的主机名
    String localName = request.getLocalName();
    println(out,"localName = " + localName);
    
    //8. 接收请求的接口的 Internet Protocol (IP) 端口号
    int localPort = request.getLocalPort();
    println(out,"localPort = " + localPort);
    
    //9. 返回请求使用的协议的名称和版本
    String protocol = request.getProtocol();
    println(out,"protocol = " + protocol);
    
    //10. 读取请求正文信息
    BufferedReader reader = request.getReader();
    println(out,"getReader = " + reader.toString());
    
    //11. 发送请求的客户端
    String remoteAddr = request.getRemoteAddr();
    println(out,"RemoteAddr = " + remoteAddr);
    
    //12. 发送请求的客户主机
    String remoteHost = request.getRemoteHost();
    println(out,"RemoteHost = " + remoteHost);
    
    //13. 发送请求的客户主机端口
    int remotePort = request.getRemotePort();
    println(out,"RemotePort = " + remotePort);
    
    //14. 返回用于发出此请求的方案名称,例如:http 、 https 、 ftp 
    String scheme = request.getScheme();
    println(out,"Scheme = " + scheme);
    
    //15. 返回请求被发送到的服务器的主机名。它是Host头值":"(如果有)之前的那部分的值。 或者解析服务器名称或服务器的IP地址
    String serverName = request.getServerName();
    println(out,"ServerName = " + serverName);
    
    //16. 返回请求被发送到的端口。他是"Host"头值":" (如果有)之后的那部分的值,或者接受客户端连接的服务器端口。
    int serverPort = request.getServerPort();
    println(out,"ServerPort = " + serverPort);
    
    //17. 返回一个boolean值,指示此请求是否是使用安全通道(比如HTTPS) 发出的。
    boolean secure = request.isSecure();
    println(out,"isSecure = " + secure);
    
    //以上方法为 ServletRequest 接口提供的
    
    
    //以下方法为 HttpServletRequest 接口提供的
    
    /*
     * 18. 返回用于保护servlet的验证方法名称。 所有的servlet容器都支持
     * basic、 form和client certificate验证, 并且可能还支持digest验证
     */
    String authType = request.getAuthType();
    println(out,"authType = " + authType);
    
    //19. getDateHeader ??
    request.getDateHeader("");
    
    //20. 返回请求头包含的所有头名称的枚举。
    Enumeration<String> headerNames = request.getHeaderNames();
    println(out,"<hr/>");
    while(headerNames.hasMoreElements()){
        String name = headerNames.nextElement();
        println(out," headerNmea = " + name + ";   getHeader = " + request.getHeader(name));
    }
    
    println(out,"<hr/>");
    //21. 以int的形式返回指定请求头的值。 ???
    request.getIntHeader("123");
    
    //22. 返回与客户端发出此请求时发送的URL相关联的额外路径信息。
    String pathInfo = request.getPathInfo();
    println(out,"PathInfo = " + pathInfo);
    
    //23. 返回包含在请求RUL中路径后面的查询字符串。如果没有查询字符串返回null
    String remoteUser = request.getRemoteUser();
    println(out,"RemoteUser = " + remoteUser);
    
    //24. 返回客户端制定的回话ID
    String requestedSessionId = request.getRequestedSessionId();
    println(out,"requestSessionId = " + requestedSessionId);
    
    //25. 返回请求调用servlet的URL部分
    String servletPath = request.getServletPath();
    println(out,"servletPath = " + servletPath);
    
    //26. 返回与此请求关联的当前HttpSession,如果没有当前会话并且参数为true,则返回一个新会话。
    HttpSession session = request.getSession(true);
    println(out,"getSession(true) = " + session);
    
    //27. 返回包含当前已经过验证的用户的名称的java.security.Principal对象。如果用户没有经过验证,则该方法返回null
    Principal userPrincipal = request.getUserPrincipal();
    println(out,"userPrincipal = " + userPrincipal);
    
    //28. 检查会话的id是否作为Cook进入的
    boolean sessionIdFromCookie = request.isRequestedSessionIdFromCookie();
    println(out,"sessionIdFromCookie = " + sessionIdFromCookie);
    
    //29. 检查请求的会话ID是否作为请求的URL的一部分进入的
    boolean sessionIdFromURL = request.isRequestedSessionIdFromURL();
    println(out,"sessionIdFormURL = " + sessionIdFromURL);
    
    //30. 
    
    println(out,"</ol>");
    out.flush();
    out.close();

}

public void println(PrintWriter out,Object obj){
    try {
        out.println("<li>");
        out.println(obj);
        out.println("</li>\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
%>
<%
getReqInfo(request,response);
%>

 

标签:HttpServletRequest,返回,String,request,详解,println,请求,方法,out
From: https://blog.51cto.com/u_16242566/7536838

相关文章

  • HTTP响应头信息和请求头信息详解
    web性能测试中有一个web资源分析,web资源是从服务器入手对web服务器的性能进行分析。所以了解一下以下信息是很有必要的哦。一:响应头信息HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616。HTTP协议采用了请求/响......
  • tar 解压缩命令详解
    解压操作:#tar-zxvf/usr/local/test.tar.gztar解压缩命令详解-c:建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时......
  • Java学习之路--method--方法重载
    packagecom.chao.method;//Java中都是值传递(Java)和引用类型publicclassDemo02{publicstaticvoidmain(String[]args){intmax=max(40,50);//实参40,50System.out.println("最大的数是"+max);}//比大小形参num1,num2publicstatic......
  • 安装 MyEclipse 出现 "An error occurred while copying software to your machine"
    安装MyEclipse时出现Anerroroccurredwhilecopyingsoftwaretoyourmachine字样,有部分文章提出可能是因为之前安装没有完全卸载。比如MyEclipse(不论版本是啥)卸载后重新安装,提示:拷贝到计算机时出错;问题解决方案:。今天我也遇到了这种问题,但是用这种方法没有成功。然后我......
  • 最全详解Android设备UDID还是唯一ID?
    这篇文章主要介绍了Android设备UDID还是唯一ID?我觉得挺不错的,现在分享给大家,也给大家做个参考。我想为我的Android应用程序生成android设备唯一ID,以根据用户设备udid创建收藏夹.所有设备都有唯一的ID.importandroid.provider.Settings.Secure;privateStringandroid_id=Se......
  • 苹果app开发流程详解
    苹果app开发流程详解 苹果AppStore上传应用流程详解,在向AppStore提交应用之前,开发者首先要成为苹果iOS开发者项目的成员,每年向苹果缴纳99美元或199美元的费用(具体申请方法后期更新)。免688开发IOS应用,根据apple的规定,不支付688,可以开发IOS应用,但是不支持提交审核,推送,支付和apple登......
  • Appuploader 常见错误及解决方法(一)
    目录(1)遇到问题,首先检查苹果开发官网。是否有权限/是否被停用/是否过期/是否有协议需要同意。 (2)在右上角切换账号检查所关联的账号是否功能正常。Apple邮箱会接收到许多通知消息,如:IPA上传,账号发生变化,被停用,都会接收到消息提醒。 (3)只要账号正常,再考虑是否软件哪个操作或软件功能错......
  • 2022最新爱思助手怎么用 爱思助手使用方法【教程】
    爱思助手集成了一键刷机,一键越狱,高级玩家,免越狱安装正版软件等功能,是所有苹果用户必备的工具软件。下面就由小编来给大家带来爱思助手的刷机操作步骤。软件功能1.支持iPhone4S-iOS5.x平刷。2.支持iPhone4、3GS刷任意版本的固件(前提是有对应版本的SHSH备份)。3.支持iPhone4、3GS......
  • 方法初学习
    方法学习方法的定义及调用设计原则,一个方法只能有一个功能//自定义方法修饰符+返回值类型/*void是不返回,如int返回数字,需要用return来输出返回值*/+自定义方法名称,或调用Java本来就有的名称+(参数//可不注释){}加法自定义备注:方法包含与类与对象中可将我们自己写的代码作......
  • 图注意网络(GAT)的可视化实现详解
    前言 能够可视化的查看对于理解图神经网络(gnn)越来越重要,所以这篇文章将介绍传统GNN层的实现,然后展示ICLR论文“图注意力网络”中对传统GNN层的改进。本文转载自DeepHubIMBA作者:DavidWiner仅用于学术分享,若侵权请联系删除欢迎关注公众号CV技术指南,专注于计算机视觉的技术......