ServletUtil

Origin

Initially, Servlet-related tools were not considered within Hutool’s encapsulation scope. However, many people later requested a Servlet Cookie tool, so I decided to create ServletUtil. This tool’s usage scope is not limited to just Cookies, but also includes parameters and more.

In fact, the earliest Servlet encapsulation originated from the author’s MVC framework: Hulu. This MVC framework provided a layer of encapsulation for Servlet, making request handling more convenient. Therefore, Hutool encapsulates the methods in the Request and Response classes of Hulu here.

Usage

Add Dependency

<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <!-- This package is generally provided by the Servlet container -->
 <scope>provided</scope>
</dependency>

Methods

  • getParamMap obtains all request parameters.
  • fillBean converts request parameters into a Bean.
  • getClientIP obtains the client IP, supporting retrieval from Nginx header information or custom header information retrieval locations.
  • getHeader and getHeaderIgnoreCase obtain information from the request header.
  • isIE checks if the client browser is IE.
  • isMultipart checks if the form is of Multipart type, which is used for file uploads.
  • getCookie obtains the specified Cookie.
  • readCookieMap encapsulates cookies into a Map.
  • addCookie sets the Cookie to be returned to the client.
  • write returns data to the client.
  • setHeader sets the response header.