Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set Encoding Filter in Tomcat

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to set up the coding Filter in Tomcat, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. SetCharacterEncodingFilter

Public void doFilter (ServletRequest request, ServletResponse response

FilterChain chain) {

/ / Conditionally select and set the character encoding to be used

If (ignore | | (request.getCharacterEncoding () = = null)) {

String characterEncoding = selectEncoding (request)

If (characterEncoding! = null) {

Request.setCharacterEncoding (characterEncoding)

}

}

Chain.doFilter (request, response)

}

Filter's doFilter method is shown above, and in this class, in addition to the parameter encoding, an ignore parameter is provided. This parameter is a switch, which is mainly used to decide whether to ignore the encoding specified in the client request. If it is true, we see that the process will do the selectEncoding operation directly. When there is a specific need, you can inherit the Filter and override its selectEncoding method implementation, such as setting the corresponding encoding according to the Accept-Language entry in the request header, or according to the specific identity in the session.

Processing of additional parameters

We know that for Filter, Servlet, etc., we can specify initialization parameters in web.xml, which we call initParameter. For our own application, we may have defined the parameters to be passed in, and we can parse the passed values directly in the application.

Within Tomcat, in order to support multiple forms of custom initParameter in multiple Filter, a utility class is specially defined to parse the passed parameters.

The Filter we mentioned above inherits from a base class such as FilterBase, which is mainly used to set initialization parameters.

Public void init (FilterConfig filterConfig) throws ServletException {

Enumeration paramNames = filterConfig.getInitParameterNames ()

While (paramNames.hasMoreElements ()) {

String paramName = paramNames.nextElement ()

If (! IntrospectionUtils.setProperty (this, paramName)

FilterConfig.getInitParameter (paramName)) {

String msg = sm.getString ("filterbase.noSuchProperty"

ParamName, this.getClass (). GetName ()

If (isConfigProblemFatal ()) {

Throw new ServletException (msg)

} else {

GetLogger () .warn (msg);}}

This utility class is the IntrospectionUtils marked red above.

Reflection is mainly used in Utils to set parameter values, that is, reflection calls the setter of the corresponding parameters to assign values. When I mention this tool class, the main purpose is not to say reflection, but because the way it is implemented can be referred to, and the boundary conditions are well considered.

For example, after getting the Method corresponding to class, it will determine the number and type of its parameters.

/ / First, the ideal case-a setFoo (String) method

For (int I = 0; I < methods.length; iTunes +) {

Class paramT [] = methods.getParameterTypes ()

If (setter.equals (methods [I] .getName ()) & & paramT.length = = 1

& "java.lang.String" .equals (paramT [0] .equals ()) {

Methods [I] .invoke (o, new Object [] {value})

Return true

}

}

When considering other types, in addition to the base type, wrapper classes are also taken into account

If (setter.equals (methods.getName ())

& & methods [I] .getParameterTypes (). Length = = 1) {

/ / match-find the type and invoke it

Class paramType = methods[ I] .getParameterTypes () [0]

Object params [] = new Object [1]

/ / Try a setFoo (int)

If ("java.lang.Integer" .equals (paramType.getName ()

| "int" .equals (paramType.getName ()) {

Try {

Params [0] = new Integer (value)

} catch (NumberFormatException ex) {

Ok = false

}

/ / Try a setFoo (long)

} else if ("java.lang.Long" .equals (paramType.getName ()

| "long" .equals (paramType.getName ()) {

Try {

Params [0] = new Long (value)

} catch (NumberFormatException ex) {

Ok = false

}

/ / Try a setFoo (boolean)

} else if ("java.lang.Boolean" .equals (paramType.getName ()

| "boolean" .equals (paramType.getName ()) {

Params [0] = Boolean.valueOf (value)

}

For the getter of parameters, in addition to getXXX, you also think of isXXX.

Public static Object getProperty (Object o, String name) {

String getter = "get" + capitalize (name)

String isGetter = "is" + capitalize (name)

The above is how to set up the coding Filter in Tomcat. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report