JSP Submission Form data garbled solution
When submitting data to the server through form form, if the Chinese data is submitted, it may be garbled, if the form request is a POST request, then the following scheme can be used to solve garbled characters:
Sets the encoding of request object requests before calling GetParameter ().
<% request.setcharacterencoding ("Utf-8");%>
002. If the form is submitted by the Get method, two garbled schemes are processed:
01. Via New String (Str.getbytes ("iso-8859-1"), "Utf-8");
The disadvantage of this approach is that each acquisition of data submitted by a user will be transformed
02. Through the Server.xml document under the Conf
<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Redirectport= "8443" uriecoding= "Utf-8"/>
The second approach is not recommended, but be aware that we are not exposed to the server because of real development.
JSP Filter
The filters in Servlets and JSPs are Java classes, and they exist for the following purposes:
- Intercept the backend resource when it is requested to access it
- Manage responses returned to clients from the server
A number of commonly used filter types are listed below:
- Authentication Filter
- Data compression Filters
- Encryption Filter
- Filters that trigger resource access events
- Image Conversion Filter
- Login and verify Filters
- MIME type chain Filter
- Token filter
- XSL/T filter for converting XML content
The filter will be inserted into the Web. xml file and then mapped to the servlet, JSP file name, or URL pattern. Deployment profile Web. XML can be found under the <tomcat-installation-directory>\conf directory.
When a JSP container launches a network application, it creates an instance of each filter that must be declared in the Deployment profile Web. XML and executed in the order declared.
Servlet Filter Method
A filter is a Java class that implements the Javax.servlet.Filter interface. The Javax.servlet.Filter interface defines three methods:
Serial Number |
method & Description |
1 |
public void DoFilter (ServletRequest, Servletresponse, Filterchain) This method is called by the container whenever Request/response is going through the filter chain, because the client requests the resource at the end of the chain |
2 |
public void init (Filterconfig filterconfig) The container calls this method to indicate that a filter is placed in the service |
3 |
public void Destroy () The container calls this method to indicate that a filter is being removed from the service |
code example
1 //Introducing Java Packages2Import java.io.*;3Import javax.servlet.*;4Import javax.servlet.http.*;5Import java.util.*;6 7 //implementing the Filter class8 Public classLogfilter implements Filter {9 Public voidinit (filterconfig config)Ten throws servletexception{ One //Get initialization Parameters AString Testparam = Config.getinitparameter ("Test-param"); - - //Print Initialization Parameters theSystem. out. println ("Test Param:"+Testparam); - } - Public voidDoFilter (servletrequest request, - servletresponse Response, + filterchain chain) - throws Java.io.IOException, servletexception { + A //get client IP address atString ipAddress =request.getremoteaddr (); - - //output IP address and current time -System. out. println ("IP"+ ipAddress +", time" -+NewDate (). toString ()); - in //Pass the request channel filter chain - Chain.dofilter (request,response); to } + Public voiddestroy () { - /*called before the filter instance is removed from the server. */ the } *}
View Code
JSP filter mappings in the Web. xml file
The filter is defined and then mapped to a URL or JSP file name, similar to how the servlet is defined and then mapped. In the Deployment profile Web. XML, use the <filter> tag for filter mapping:
1<filter>2<filter-name>LogFilter</filter-name>3<filter-class>logfilter</filter-class>4<init-param>5<param-name>test-param</param-name>6<param-value>initialization paramter</param-value>7</init-param>8</filter>9<filter-mapping>Ten<filter-name>LogFilter</filter-name> One<url-pattern>/*</url-pattern> A </filter-mapping>
View Code
The above filters will be applied in all servlets and JSP programs, as we specify "/*" in the configuration. You can also specify a servlet or JSP path if you only want to apply the filter to a few servlets or JSP programs.
Now, accessing the servlet or JSP page as you normally would, you will find that the log in the server generates a record of the visit. You can also use the log4j recorder to record logs in other files.
__________________________________________________________________________________________________________
Using multiple filters
Your Web application can define a number of different filters. Now that you have defined two filters, Authenfilter and Logfilter, the other steps are the same as before, unless you want to create a different mapping, like this:
1<filter>2<filter-name>LogFilter</filter-name>3<filter-class>logfilter</filter-class>4<init-param>5<param-name>test-param</param-name>6<param-value>initialization paramter</param-value>7</init-param>8</filter>9 Ten<filter> One<filter-name>AuthenFilter</filter-name> A<filter-class>authenfilter</filter-class> -<init-param> -<param-name>test-param</param-name> the<param-value>initialization paramter</param-value> -</init-param> -</filter> - +<filter-mapping> -<filter-name>LogFilter</filter-name> +<url-pattern>/*</url-pattern> A </filter-mapping> at - <filter-mapping> - <filter-name>AuthenFilter</filter-name> - <url-pattern>/*</url-pattern> - </filter-mapping>
View Code
Application Order of filters
The mapping order of <filter> elements in Web. XML determines the order in which the containers apply these filters. To reverse the order of the apps, you only need to reverse the order in which the <filter> elements are defined in Web. Xml.
For example, the above example will first apply Logfilter and then apply Authenfilter, but the following example will reverse the order of the application:
1 <filter-mapping>2 <filter-name>AuthenFilter</filter-name>3 <url-pattern>/*</url-pattern>4</filter-mapping> 5 6<filter-mapping>7 <filter-name>logfilter </filter-name>8 <url-pattern>/*</url-pattern>9 </filter-mapping>
View Code
JSP submits form data garbled, built-in objects, and filters