Class Charsets
- Author:
- tomyeh
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final voidCleans up what has been set insetup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String).static final LocalegetPreferredLocale(jakarta.servlet.http.HttpSession sess, jakarta.servlet.ServletRequest request) Returns the preferred locale of the specified request.static final LocalegetPreferredLocale(jakarta.servlet.ServletRequest request) Returns the preferred locale of the specified request.static final StringReturns the charset used to encode URI and query string.static final booleanhasSetup(jakarta.servlet.ServletRequest request) Returns whether the specified request has been set up, i.e.,setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)is calledstatic final voidmarkSetup(jakarta.servlet.ServletRequest request, boolean setup) Marks the specified request whether it has been set up, i.e.,setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)is called.static final voidsetPreferredLocale(jakarta.servlet.http.HttpSession hsess, Locale locale) Sets the preferred locale for the specified session.static final voidsetPreferredLocale(jakarta.servlet.ServletContext ctx, Locale locale) Sets the preferred locale for the specified servlet context.static final Objectsetup(jakarta.servlet.http.HttpSession sess, jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, String charset) Sets up the charset for the request and response based ongetPreferredLocale(HttpSession,ServletRequest).static final Objectsetup(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, String charset) Sets up the charset for the request and response based ongetPreferredLocale(HttpSession,ServletRequest).
-
Constructor Details
-
Charsets
public Charsets()
-
-
Method Details
-
getURICharset
Returns the charset used to encode URI and query string. -
setup
public static final Object setup(jakarta.servlet.http.HttpSession sess, jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, String charset) Sets up the charset for the request and response based ongetPreferredLocale(HttpSession,ServletRequest). After setting up, you shall invokecleanup(jakarta.servlet.ServletRequest, java.lang.Object)before exiting.final Object old = setup(request, response, null); try { .... } finally { cleanup(request, old); }It is OK to call this method multiple time, since it is smart enough to ignore redundant calls.
CharsetFilteractually use this method to setup the proper charset and locale. By mappingCharsetFilterto all servlets, the encoding charset could be prepared correctly. However, if you are writing libraries to be as independent of web.xml as possible, you might choose to invoke this method directly.- Parameters:
sess- the session to look for the preferred locale. Ignored if null.charset- the response's charset. If null or empty, response.setCharacterEncoding won't be called, i.e., the container's default is used.- Returns:
- an object that must be passed to
cleanup(jakarta.servlet.ServletRequest, java.lang.Object)
-
setup
public static final Object setup(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, String charset) Sets up the charset for the request and response based ongetPreferredLocale(HttpSession,ServletRequest). It is the same as setup(request.getSession(false), request, response, charset); -
cleanup
Cleans up what has been set insetup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String). Some invocation are not undo-able, so this method only does the basic cleanups.- Parameters:
old- the value must be the one returned by the last call tosetup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String).
-
hasSetup
public static final boolean hasSetup(jakarta.servlet.ServletRequest request) Returns whether the specified request has been set up, i.e.,setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)is calledIt is rarely needed to call this method, because it is called automatically by
setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String). -
markSetup
public static final void markSetup(jakarta.servlet.ServletRequest request, boolean setup) Marks the specified request whether it has been set up, i.e.,setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)is called.It is rarely needed to call this method, because it is called automatically by
setup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String). -
getPreferredLocale
public static final Locale getPreferredLocale(jakarta.servlet.http.HttpSession sess, jakarta.servlet.ServletRequest request) Returns the preferred locale of the specified request. You rarely need to invoke this method directly, because it is done automatically bysetup(jakarta.servlet.http.HttpSession, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String).- It checks whether any attribute stored in HttpSession called
Attributes.PREFERRED_LOCALE. If so, return it. - If not found, it checks if the servlet context has the
attribute called
Attributes.PREFERRED_LOCALE. If so, return it. - If not found, it checks if the library property
called
Attributes.PREFERRED_LOCALEis defined. If so, return it. - Otherwise, use ServletRequest.getLocale().
- Parameters:
sess- the session to look for the preferred locale. Ignored if null.
- It checks whether any attribute stored in HttpSession called
-
getPreferredLocale
Returns the preferred locale of the specified request. It is the same as getPreferredLocale(request.getSession(false), request). -
setPreferredLocale
Sets the preferred locale for the specified session. It is the default locale for the whole Web session.Default: null (no preferred locale -- depending on browser's setting).
- Parameters:
locale- the preferred Locale. If null, it means no preferred locale- Since:
- 3.6.3
- See Also:
-
setPreferredLocale
Sets the preferred locale for the specified servlet context. It is the default locale for the whole Web application.Default: null (no preferred locale -- depending on browser's setting).
- Parameters:
locale- the preferred Locale. If null, it means no preferred locale- Since:
- 3.6.3
- See Also:
-