Tomcat Chinese encoding solution

Source: Internet
Author: User

For the root cause of coding problems, refer to http://www-900.ibm.com/developerWorks/cn/java/java_chinese/index.shtml

Tomcat 4.x solution:
Request. setcharacterencoding ("gb2312 ");
Output Chinese: <% @ page contenttype = "text/html; charset = gb2312" %>, transcoding required

Solution for Tomcat 5.x:
Obtain Chinese characters:
When submitting a form
1) post: request. setcharacterencoding ("gb2312 ");
2) Get: Modify server. xml and add uriencoding = "gb2312" to connector"
For example, <connector Port = "80" maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
DEBUG = "0" connectiontimeout = "20000"
Disableuploadtimeout = "true" uriencoding = "gb2312"/>
Or use usebodyencodingforuri to make Tomcat 5.x compatible with Tomcat 4.x.
Output Chinese: <% @ page contenttype = "text/html; charset = gb2312" %>, transcoding required

Appendix: Tomcat 5.x and tomcat 4.x have changed when parsing and submitting forms. Tomcat 4.x uses both post and get
The same encoding, while Tomcat 5.x releases the get method separately. For details, see the source code of Tomcat.

The get method is better. We recommend that you configure the filter for the post method, because in this way, you do not have to worry about configuring the entire system.

Simple Description:
Web. xml
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> setcharacterencodingfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
/************************/

Setcharacterencodingfilter. Java
--------------------------------------------
Import java. Io. ioexception;
Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. servlet. unavailableexception;

/**
* Example filter that sets the character encoding to be used in parsing
* Incoming request
*/
Public class setcharacterencodingfilter implements filter {

/**
* Take this filter out of service.
*/
Public void destroy (){
}
/**
* Select and set (if specified) the character encoding TO BE USED
* Interpret Request Parameters for this request.
*/
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {

Request. setcharacterencoding ("GBK ");

// Transfer control to the next Filter
Chain. dofilter (request, response );
}

Public void Init (filterconfig) throws servletexception {
}
}
//// You can also pass the encoding as a parameter.

12. If your webapp only needs to be able to access through https, add:
<Security-constraint>
<Web-resource-collection>
<Web-resource-Name> must HTTPS </Web-resource-Name>
<URL-pattern>/* </url-pattern>
</Web-resource-collection>
<User-data-constraint>
<Transport-guarantee> confidential </transport-guarantee>
</User-data-constraint>
</Security-constraint>
Reference: http://jakarta.apache.org/tomcat/faq/security.html#https
Http://marc.theaimsgroup.com /? L = tomcat-user & M = 104951559722619 & W = 2

13. Modify the remote server shutdown command.
Server. XML has the following line by default:
<Server port = "8005" shutdown = "shutdown">
In this way, anyone can telnet to port 8005 of the server, enter "shutdown", Press enter, and the server is immediately shut down.
From a security perspective, we need to convert the shutdown command into a string that is not easily guessed by others.
For example, modify as follows:
<Server port = "8006" shutdown = "lizongbo"> in this way, Tomcat can be closed only when telnet to 8005 and "lizongbo" is entered.
Note: This modification does not affect the execution of shutdown. bat. You can shut down the server by running shutdown. bat.
Reference: http://jakarta.apache.org/tomcat/faq/security.html#8005

See http://www.cnjsp.org/document/user/tuman/valve.html for the following:

14. Configure HTTP access logs. The HTTP access logs that can be recorded by Tomcat are already detailed.
Cancel the following comments:

<Valve classname = "org. Apache. Catalina. Valves. accesslogvalve"
Directory = "logs" prefix = "localhost_access_log." suffix = ". txt"
Pattern = "common" resolvehosts = "false"/>

Then modify it:
<Valve classname = "org. Apache. Catalina. Valves. fastcommonaccesslogvalve"
Directory = "logs" prefix = "localhost_access_log." suffix = ". txt"
Pattern = "combined" resolvehosts = "false" filedateformat = "yyyy-MM-dd.HH"/>

Pattern = "combined" records more detailed log Content, filedateformat = "yyyy-MM-dd.HH", will let the log file roll by hour,
It is better than the default daily roll-out, especially for websites with high traffic volumes, you can consider writing filedateformat = "yyyy-MM-dd.HH.mm", it will be a log file per minute.
In addition, you can record your own logs by engine, host, or context.
For details, refer:
Http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/valve.html
Http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/logger.html
Http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/host.html#Access%20Logs
It can also be used with AWStats for log statistical analysis: http://www.chedong.com/tech/awstats.html http://blog.csdn.net/lizongbo/archive/2005/02/18/291929.aspx

15. Restrict IP addresses and host access.
If you want to disable the specified IP address or host name to deny access to some machines, or specify some machines for access.
You can also perform the following configurations by engine, host, or context:
<Context Path = "/Examples"...>...
<Valve classname = "org. Apache. Catalina. Valves. remotehostvalve"
Allow = "* .mycompany.com, www.yourcompany.com"/>
<Valve classname = "org. Apache. Catalina. Valves. remoteaddrvalve"
Deny = "192.168.1. *"/>
</Context>
Refer:
Http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/context.html

16. Publish the webapp to the root directory of the website.
1. Directly copy to the root directory.
2. Because an XML file without a name cannot be created and the path specified in the XML file is invalid (Tomcat depends on the file name ),
Therefore, you must write the following section in server. xml:
<Context docbase = "$ {Catalina. Home}/vhost/www.lizongbo.com" Path = "/"
Privileged = "true" antiresourcelocking = "false" antijarlocking = "false">
<Manager classname = "org. Apache. Catalina. session. standardmanager" algorithm = "SHA-512" sessionidlength = "40">
<Valve classname = "org. Apache. Catalina. Valves. fastcommonaccesslogvalve"
Directory = "logs" prefix = "localhost_mytest_access_log." suffix = ". txt"
Pattern = "combined" resolvehosts = "true" filedateformat = "yyyy-MM-dd.HH"/>

</Context>
In addition, you must delete the root directory. Otherwise, the root directory of Tomcat is preferentially deployed "/".

17. When restarting Tomcat's webapp, it is prohibited to write sessions to files.
Modify CONF/Web. xml
Uncomment:
<! ---->
<Manager pathname = ""/>

18. Enhance the algorithm and length for generating sessiionid.

<Manager classname = "org. Apache. Catalina. session. standardmanager" algorithm = "SHA-512" sessionidlength = "40">
</Manager>

(The default Tomcat algorithm is MD5 and the default length is 16 bits .)

 

Http://blog.csdn.net/dakuen/

1. Download http://www.eu.apache.org/dist/jakarta/tomcat-5/
In this case, jakarta-tomcat-5.0.28.execorresponds to a local Admin module. In jakarta-tomcat-5.5.9.exe, the default admin module is not installed. When http: // 127.0.0.1: 8080/admin is enabled
Tomcat *** s administration Web application is no longer installed by default. download and install the "admin" package to use it.
Therefore, we need to download the "admin" package
Bringing jakarta-tomcat-5.5.x.zip with jakarta-tomcat-5.5.x-compat.zip and jakarta-tomcat-5.5.x-admin.zip
Decompress the three files in the same directory.
(If JDK is used to compile compat.zip, JDK is not used .)

 

2. Modify jakarta-tomcat-5.5.x/CONF/tomcat-users.xml.
Add the Administrator account lizongbo with the password lizongbopass.
The new XML is as follows:
<? XML version = *** 1.0 *** encoding = *** UTF-8 ***?>
<Tomcat-users>
<Role rolename = "Tomcat"/>
<Role rolename = "role1"/>
<Role rolename = "manager"/>
<Role rolename = "admin"/>
<User Username = "Tomcat" Password = "Tomcat" roles = "Tomcat"/>
<User Username = "role1" Password = "Tomcat" roles = "role1"/>
<User Username = "both" Password = "Tomcat" roles = "tomcat, role1"/>
<User Username = "lizongbo" Password = "lizongbopass" roles = "Admin, Manager"/>
</Tomcat-users>

Sometimes some modifications are made in % catalina_home %/Server/webapps/admin/WEB-INF/Web. xml

<! -- Security is active on entire directory -->
<Security-constraint>
<Display-Name> Tomcat server configuration security constraint </display-Name>
<Web-resource-collection>
<Web-resource-Name> Protected Area </Web-resource-Name>
<! -- Define the context-relative URL (s) to be protected -->
<URL-pattern> *. jsp </url-pattern>
<URL-pattern> *. DO </url-pattern>
<URL-pattern> *. html </url-pattern>
</Web-resource-collection>
<Auth-constraint>
<! -- Anyone with one of the listed roles may access this area -->
<Role-Name> admin </role-Name>
</Auth-constraint>
</Security-constraint>

<! -- Login configuration uses form-based authentication -->
<Login-config>
<Auth-method> form </auth-method>
<Realm-Name> Tomcat server configuration form-based authentication area </realm-Name>
<Form-login-config>
<Form-login-page>/login. jsp </form-login-page>
<Form-error-page>/error. jsp </form-error-page>
</Form-login-config>
</Login-config>

<! -- Security roles referenced by this Web Application -->
<Security-role>
<Description>
The role that is required to log in to the Administration Application
</Description>
<Role-Name> admin </role-Name>
</Security-role>
Whether authentication or authorization (permission control only sets the relevant admin role, when you want to add or modify the relevant AA, you must modify this file, to meet your environment.
3. Modify jakarta-tomcat-5.5.x/CONF/server. XML to solve the encoding problem.
(Add uriencoding parameters to connector, refer to http://blog.csdn.net/darkxie/archive/2004/10/25/TOMCATAPP.aspx)
(It can be set to gb18030)
<Connection Port = "8080"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "200"
Connectiontimeout = "20000" disableuploadtimeout = "true" uriencoding = "GBK"
Compression = "on" compressionminsize = "2048"
Nocompressionuseragents = "gozilla, Traviata"
Compressablemimetype = "text/html, text/XML"/>

<Connection Port = "8009"
Enablelookups = "false" redirectport = "8443" protocol = "AJP/1.3" uriencoding = "GBK"/>

4. enable support for gzip compression.
(Http://www.linuxaid.com.cn/forum/showdoc.jsp? L = 1 & I = 81169)
Add the following attributes
Compression = "on"
Compressionminsize = "2048"
Nocompressionuseragents = "gozilla, Traviata"
Compressablemimetype = "text/html, text/XML"

5. Set the VM.
Create the folder vhost/www.mydomain.com under the jakarta-tomcat-5.5.x.
Then modify the jakarta-tomcat-5.5.x/CONF/server. xml

<Engine defaulthost = "localhost" name = "Catalina">
<Host appbase = "vhost/www.mydomain.com" name = "http://www.mydomain.com/">
</Host>
<Host appbase = "webapps" name = "localhost">
</Host>
<Realm classname = "org. Apache. Catalina. realm. userdatabaserealm"/>
</Engine>

6. Add the database driver and update Mail. jar and actiovation. jar.
Copy mysql-connector-java-3.0.16-ga-bin.jar, pg74.215.jdbc3. jar to jakarta-tomcat-5.5.x/common/lib/
And javamail 1.3.2 mail. jar, jaf-1_0_2 activation. Jar
MSSQL 2000 JDBC SP3, msbase. jar, msutil, jar, MSSQLServer. Jar

7. Configure SSL
Reference http://jakarta.apache.org/tomcat/tomcat-5.5-doc/ssl-howto.html
D:/j2sdk1.4.2 _ 06/bin> % java_home %/bin/keytool-genkey-alias tomcat-keyalg RSA
Enter the keystore password: lizongbossl
What is your first name and last name?
[Tomcat5.5.x]: tomcat5.5.x
What is the name of your organization?
[Jakarta]: Jakarta
What is your organization name?
[Apache]: Apache
What is the name of your city or region?
[Hzcity]: hzcity
What is the name of your state or province?
[GDP]: GDP
What is the two-letter country code for this unit?
[CN]: CN
CN = tomcat5.5.x, ou = Jakarta, O = Apache, L = hzcity, St = GDP, c = cn correct?
[No]: Y

Enter the master password of <Tomcat>
(If the password is the same as the keystore password, press Enter ):

(The password must be consistent, so press enter directly)
Then copy the. keystore in userhome (for example, C:/Documents and Settings/lizongbo/)
In the conf/directory of Tomcat.
(Example: D:/jakarta-tomcat-5.5.x/CONF/. keystore
Configure jakarta-tomcat-5.5.x/CONF/server. xml
Add
<Connection Port = "8443"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" disableuploadtimeout = "true"
Acceptcount = "100" Scheme = "HTTPS" secure = "true"
Clientauth = "false" sslprotocol = "TLS"
Keystorefile = "CONF/. keystore"
Keystorepass = "lizongbossl"> <! -- Consistent with the previously set password -->
</Connector>
8. Disable the file directory list,
Modify jakarta-tomcat-5.5.x/CONF/Web. xml and set listing to false

<Servlet>
<Servlet-Name> default </servlet-Name>
<Servlet-class> org. Apache. Catalina. servlets. DefaultServlet </servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 0 </param-value>
</Init-param>
<Init-param>
<Param-Name> listings </param-Name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

9. You have specified your own javaencoding.
(Reference http://gceclub.sun.com.cn/staticcontent/html/sunone/app7/app7-dg-webapp/ch6/ch6-4.html

<Servlet>
<Servlet-Name> JSP </servlet-Name>
<Servlet-class> org. Apache. Jasper. servlet. jspservlet </servlet-class>
<Init-param>
<Param-Name> fork </param-Name>
<Param-value> false </param-value>
</Init-param>
<Init-param>
<Param-Name> javaencoding </param-Name>
<Param-value> gb18030 </param-value>
</Init-param>
<Init-param>
<Param-Name> xpoweredby </param-Name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 3 </load-on-startup>
</Servlet>
10. Add mime-type ing for RAR and ISO
Avoid opening it directly in the browser.
<Mime-mapping>
<Extension> MHT </extension>
<Mime-type> text/X-MHT </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> RAR </extension>
<Mime-type> application/octet-stream </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> ISO </extension>
<Mime-type> application/octet-stream </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> ape </extension>
<Mime-type> application/octet-stream </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> rmvb </extension>
<Mime-type> application/octet-stream </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> ICO </extension>
<Mime-type> image/X-Icon </mime-type>
</Mime-mapping>
10.1 set encoding for HTML static pages
<! -- Modify the following two lines to support Automatic Static hypertext encoding.
-->
<Mime-mapping>
<Extension> HTM </extension>
<Mime-type> text/html; charset = gb2312 </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> HTML </extension>
<Mime-type> text/html; charset = gb2312 </mime-type>
</Mime-mapping>
</Web-app>

11. Add welcome-file-list and adjust the order.
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
<Welcome-File> index.html </welcome-File>
<Welcome-File> index.htm </welcome-File>
<Welcome-File> default.html </welcome-File>
<Welcome-File> default.htm </welcome-File>
<Welcome-File> default. jsp </welcome-File>
</Welcome-file-List>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.