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
URL-PATTERN> / * url-pattern> filter-maping> 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:
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
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: