JAVAWeb---HttpServletRequest中的方法

Posted 时间绝境

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVAWeb---HttpServletRequest中的方法相关的知识,希望对你有一定的参考价值。

为了让后台可以取到queryString,做一个表单,用post方式提交:

<body>
    <form action="request?a=1&b=2&c=3" method="post">
        <input type="submit" value=" 提交 " />
    </form>
</body>

后台doPost方法的写法:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{
    System.out.println("request.getAuthType():" + request.getAuthType()); // 获取保护servlet的认证方案名(BASIC或SSL),未受保护的servlet返回的就是null
    System.out.println("request.getCharacterEncoding():" + request.getCharacterEncoding()); // 获取字符编码
    System.out.println("request.getContentLength():" + request.getContentLength()); // 返回请求体内容的长度
    System.out.println("request.getContentType():" + request.getContentType()); // 获取内容类型
    System.out.println("request.getContextPath():" + request.getContextPath()); // 获取上下文路径,就是"/"+工程名
    System.out.println("request.getLocalAddr():" + request.getLocalAddr()); // 获取应用服务器的IP地址
    System.out.println("request.getLocalName():" + request.getLocalName()); // 获取应用服务器的主机名
    System.out.println("request.getLocalPort():" + request.getLocalPort()); // 获取应用服务器的端口号
    System.out.println("request.getMethod():" + request.getMethod()); // 获取请求方式获取请求方式(GET与POST为主,也会有PUT、DELETE、INPUT)
    System.out.println("request.getPathInfo():" + request.getPathInfo());
    System.out.println("request.getPathTranslated():" + request.getPathTranslated());
    System.out.println("request.getProtocol():" + request.getProtocol()); // 获取客户端向服务端传送数据使用的协议名称
    System.out.println("request.getQueryString():" + request.getQueryString()); // 获取追加到Url后面的查询字符串
    System.out.println("request.getRemoteAddr():" + request.getRemoteAddr()); // 获取发出请求的客户端的IP地址
    System.out.println("request.getRemoteHost():" + request.getRemoteHost()); // 获取发出请求的客户端的主机名
    System.out.println("request.getRemotePort():" + request.getRemotePort()); // 获取发出请求的客户端的端口号
    System.out.println("request.getRemoteUser():" + request.getRemoteUser()); // 如果用户已经过认证,则返回发出请求的用户登录信息 
    System.out.println("request.getRequestedSessionId():" + request.getRequestedSessionId()); // 获取sessionId
    System.out.println("request.getRequestURI():" + request.getRequestURI()); // 获取"/"+工程名+请求路径
    System.out.println("request.getRequestURL():" + request.getRequestURL()); // 获取完整的请求地址,不带queryString
    System.out.println("request.getScheme():" + request.getScheme()); // 获取当前链接使用的协议,一般应用就是http,SSL返回https
    System.out.println("request.getServerName():" + request.getServerName()); // 获取URL请求的名字(以Ip请求就是Ip,以域名请求就是域名)
    System.out.println("request.getServerPort():" + request.getServerPort()); // 获取URL请求的端口号
    System.out.println("request.getServletPath():" + request.getServletPath()); // 获取请求路径
    System.out.println("request.isSecure():" + request.isSecure()); // 获取此请求是否使用安全协议(比如https)
}

后台的运行结果:

request.getAuthType():null
request.getCharacterEncoding():null
request.getContentLength():0
request.getContentType():application/x-www-form-urlencoded
request.getContextPath():/TestWeb
request.getLocalAddr():127.0.0.1
request.getLocalName():dell
request.getLocalPort():8080
request.getMethod():POST
request.getPathInfo():null
request.getPathTranslated():null
request.getProtocol():HTTP/1.1
request.getQueryString():a=1&b=2&c=3
request.getRemoteAddr():127.0.0.1
request.getRemoteHost():127.0.0.1
request.getRemotePort():3405
request.getRemoteUser():null
request.getRequestedSessionId():B198D2D352F2F23A1B3754246D68B803
request.getRequestURI():/TestWeb/request
request.getRequestURL():http://localhost:8080/TestWeb/request
request.getScheme():http
request.getServerName():localhost
request.getServerPort():8080
request.getServletPath():/request
request.isSecure():false

 

以上是关于JAVAWeb---HttpServletRequest中的方法的主要内容,如果未能解决你的问题,请参考以下文章