Chinese issues in JSP and Servlet

xiaoxiao2021-03-06  104

A sentence in the JSP page solves the problem <% @ page contentty = "text / html; charset = GB2312" Language = "java"%> But to make customers submitted in Chinese, then one sentence <% request.setcharacterencoding ("GB2312 ");%> But cannot solve the URL of the direct handwritten" to the page, otherwise the parameters will be garbled, or try the construction method of String. Servlet is also a big sentence for a while. It is also a word response.setContentType ("text / html; charset = GB2312"); the effect is equal to JSP. . . . Be careful, it must be added at PrinterWriter Out = response.getwriter (); before you can go crazy, of course you can use

String constructor new string (srcstring, "destination.Encode"); such as: new string ("here is the source string coding method", "ISO-8859-1") This seems to be a life-saving straw Haha is also the pain of the world, but for the world, it is really a gospel. Some people say that Chinese in the .properties file may have not been an extensive project, from the document reading the Chinese seems to be not very Convenient for software Localization, it may be useful to use the resource files in C . There is also a good way to say request.setcharacterencoding ("GB2312"), which is Filter (I use Tomcat). Write a server-side filter to make all the Request encoding for the way you want. This method can also be used for authentication, such as whether the user who has access to this directory is logged in, if there is permission, do not have to verify The page is coming <% @ include file = "checker.jsp"%> is really good. Or let go of the sample code I collected:

// Title: Filter for Transform Encoding Method To GB2312 // Note: Deploying FILTER contains two parts: Specify class name, Filter name, initialization parameter; // Specify FILTER mapping and scope (directory) package JGWL.Base; import Javax .servlet.FilterChain; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse ; import javax.servlet.ServletException; import javax.servlet.FilterConfig; public class myFilter implements Filter {protected FilterConfig filterConfig; private String targetEncoding = "gb2312"; // servlet general and can be obtained as the initial parameters public void init (FilterConfig config ) throws ServletException {this.filterConfig = config; this.targetEncoding = config.getInitParameter ( "encoding"); System.out.println ( "!!!! request is encoded in" targetEncoding);} // this filtering Implement Public Void Dofilter (ServletRequest Req, ServletResponse Res, Filterchain Chain) Throw s IOException, ServletException {// follow line is for debug helping //System.out.println("!!!!request is encoded in ** " targetEncoding); HttpServletRequest request = (HttpServletRequest) req; request.setCharacterEncoding (this .targetEncoding); // turn the operation right to the next filter chain.doFilter (req, res);} public void serFilterConfig (final FilterConfig filterConfig) {this.filterConfig = filterConfig;} public void destroy () {this.filterConfig = NULL;}}

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

New Post(0)