Thorough Solution under Tomcat (transfer)

xiaoxiao2021-03-06  63

Tomcat is completely solved by Ponky original (Participation: 47, expert points: 40) Published: 2004-11-19 12:40 pm Version: 1.0 Read: 331 times

These days develop a project, the server is Tomcat, the operating system is XP, which is the MVC architecture. The mode is used in the FACADE mode. It has always been garbled. I also solved it for many days. Colleagues also help solve, and more thanks to many netizens. Articles and opinions, it is finally available.

But good memory is not as bad, so I deliberately, to prevent itself from forgotten, and also provide a good reference path to those who encounter the same problem: (1) The JSP page is Chinese, but it is a garbled. : Solving the way is the coded problem in the JSP page code <% @ page language = "java" contentty = "text / html; charset = GBK"%>, because JSP is coded when the JSP is turned into Java file, the default words The server is ISO-8859-1. If a JSP is directly entered, JSP is handled as ISO8859-1 is definitely problematic, this, we can confirm by viewing JAVA intermediate files generated by Jasper ( 2) When using the REQUEST object to obtain the Chinese character code submitted by the customer, there will be garbled: Solution to configure a filter, which is a server filter, code as follows: import java.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 the * 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 to * interpret request parameters for t . His request * / public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding ( "GBK"); // passes control to the next filter chain.doFilter (request, response); } public void init (FilterConfig filterConfig) throws ServletException {}} configured web.xml Set Character Encoding SetCharacterEncodingFilter set character encoding <

URL-PATTERN> / * If you still have this situation, you will see if you have the fourth situation, your form submitted by your Form is It is not submitted by get, in general, if you submit it, if you, you will see the solution to the fourth. There is also information about the information containing Chinese characters. The code is processed: package dbjavabean; public class code () {public code codingconvert () {//} public string TOGB (String Unistr) {String GBSTR = ""; if (Unistr = = NULL) {Unistr = "";} try {byte [] tempbyte = unistr.getbytes ("ISO8859_1"); GBSTR = New String (TempByte, "GB2312");} Catch (Exception EX) {} Return GBSTR;} Public String Touni (String gbstr) {string unistr = ""; if (gbstr == null) {GBSTR = "";} try {byte [] tempbyte = gbstr.getbytes ("GB2312"); Unistr = New String (Tempbyte , "ISO8859_1");} Catch (Exception EX) {} Return Unistr;}} You can also transform directly, first you will get the obtained string with ISO-8859-1, then store this code to one In-byte arrays, then convert this array into string objects, for example: string str = request.getParameter ("girl"); Byte B [] = Str.getbytes ("ISO-8859-1"); Str = new string (b); any information submitted correctly by the above conversion. (3) Returning in the server-only request. GetParameter ("Name") is garbled; Press Tomcat to set Filter and use Request.SetCharacterencoding ("GBK"); no matter what the problem is On the method of processing parameters: If you have a DOGET (HTTPSERVLETREST, HTTPSERVLETRESE RESPONS) method, even write: Request.SetCharacterencoding ("gbk"); response.setContentType ("text / html; charSet = GBK "); also does not work, return Chinese or garbled! ! ! If this function is changed to dopost (httpservletRequest request, httpservletResponse response) everything is OK. Similarly, the meaning of the display can be displayed in two JSP page processing forms is because the POST method is transmitted, it is still not possible to change to the GET method.

This shows that in the servlet uses a doget () method or in the JSP to use the GET method to handle it. This is, involving passing parameter information to pass through the browser, which is likely to cause conflicts of common character sets or do not match. The solution is: 1) Open Tomcat's server.xml file, find blocks, add the following line: uriencoding = "GBK" is the following: 2) restart tomcat, all OK. Reasons for need to join you can find this file under the Tomcat_Home / WebApps / Tomcat-DOCS / Config / Http.html. It should be noted that this place If you use UTF-8, you should have garbled in the Tomcat during the transfer, if you don't work, you will change your character set. (4) There are Chinese on the JSP page, and there are Chinese on the button, but there is a garbled when viewing the page through the server: Solution: First, you should not directly contain localized message text in the JSP file, but should pass Tags get text from Resource Bundle. You should put your Chinese text in the Application.properties file, this file is placed under Web-INF / CLASSES / *, for example, I have a name in the page, two Label, I am first to build an Application.properties, The content inside should be name = "Name" age = "age", then put this file into web-inf / classs / proties / down, next to the Application.properties file, coding him, create a Chinese Resource file, assume that the name is Application_CN.properties. The native2ascii command is provided in JDK, which can implement the conversion of character encoding. In the DOS environment, find the directory of this file you place Application.properties, execute the command in the DOS environment, generate the Chinese resource file application_cn.properties :native2ascii? Encoding GBK Application.Properties Application_Cn.properties execute or above Aplication_cn.properties files that will be generated later: Name = / U59D3 / U540D AGE = / U5E74 / U9F84, configured in struts-config.xml: .

To this step, basically completed a half, then you will write <% @ page language = "java" contenttype = "java" contentty = "text / html; charset = GBK"%>, the Label to the name is to Write This will appear in the page when the page appears, the age is also the same, the processing of Chinese characters on the button is also the same. (5) Write to the database is garbled: Solve method: To configure a filter, it is a server filter, the code is like the second time. If you are directly linking the database through the JDBC, the configuration code is as follows: JDBC: mysql: // localhost: 3306 / Workshopdb? Useunicode = true & characterencoding = GBK, this is guaranteed that the code in the database is garbled.

If you are through the data source link, you can't follow this way, first you have to write in the configuration file, configure the data source in Tomcat 5.0.19 is in C: / Tomcat 5.0 / Conf / Catalina / Localhost, the project I built is Workshop, the list of placed is WebApp, the configuration file of Workshop.xml is as follows: factory org.apache.commons.dbcp.basicDataSourceFactory maxactive 100 Maxidle 30 maxwait 10000 Username root password driverclassname com.mysql.jdbc.driver URL <

转载请注明原文地址:https://www.9cbs.com/read-88601.html

New Post(0)