📜  Servlet – 客户端 HTTP 请求

📅  最后修改于: 2022-05-13 01:54:46.558000             🧑  作者: Mango

Servlet – 客户端 HTTP 请求

当用户想要一些信息时,他/她会通过浏览器请求信息。然后浏览器会向网络服务器发出一个网页请求。它将请求信息发送到无法直接读取的网络服务器,因为该信息将成为 HTTP 请求标头的一部分。

标题和说明

Header Name

Description

Accept

Specifies the MIME types (also called Internet Media types or Content types which describe the media type of content served by web servers or applications) that the browser or other clients can handle.

Accept-CharsetSpecifies the character sets that the browser can use to display the information
Accept-EncodingSpecifies the types of encodings that the browser supports.
Accept-LanguageSpecifies the client’s preferred languages in case the servlet can produce results in more than one language
AuthorizationThis is used by clients to identify themselves when accessing password-protected web pages
ConnectionThis indicates whether the client can handle HTTP connections or not. 
Content-LengthThis header is applicable only for POST requests and gives the size of the POST data
CookieThis returns cookies to servers that are previously sent to the browser
HostThis specifies the host and port which are given in the original URL

用于读取 HTTP Header 的方法

  • Cookie[] getCookies():返回客户端随请求发送的所有cookie对象
  • 枚举 getAttributeNames():返回一个枚举,其中包含可用于此请求的属性的名称。
  • Enumeration getHeaderNames():返回此请求包含的所有标头名称的枚举。
  • Enumeration getParameterNames():返回字符串对象的枚举,其中包含当前请求的参数名称。
  • HTTPSession getSession():返回与该请求关联的会话,如果该请求没有会话,则为该请求创建一个会话。
  • Object getAttribute(String name):将命名属性的值作为对象返回,如果不存在具有给定名称的属性,则返回 null。
  • String getContentType():返回请求的 MIME 类型,如果类型未知,则返回 null。
  • String getMethod():返回发出请求的 HTTP 方法的名称。使用的 HTTP 方法是 GET、POST。

还有其他几种方法可用于读取 HTTP 标头。

例子

Java
package com.headers;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletresponse;
// servlet implementation;
@WebServlet("/Headers")
public class Headers extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Headers() { super(); }
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String str = "Display Header Request";
        out.println(
            "\n"
            + "

" + str + "

\n"             + "
Request Method:
"             + request.getMethod() + "
\n"             + "Request URI: "             + request.getRequestURI() + "
\n"             + "Request Protocol: "             + request.getProtocol() + "

\n"             + "\n"             + "\n"             + "
Header NameHeader Value");         Enumeration headerNames = request.getHeaderNames();         while (headerNames.hasMoreElements()) {             Sting headerName                 = (String)headerNames.nextElement();             out.println("
" + headerName);             out.println(""                         + request.getHeader(headerName));         }         out.println("\n");     } }