Struts Chinese garbled problem solved

xiaoxiao2021-03-05  22

Recently, using struts 1.1, the annoying Chinese garbled problem is found, and the process of forming the Form will appear at the time.

The method I found on the web is as follows:

(Tomcat 5.0.28 Struts 1.1 Hibernate 2.1 SQLServer2K)

1. Directly convert the public static string isotogb (string src) {string strret = null; try {strret = new string (src.getbytes ("ISO_8859_1"), "GB2312");} Catch (Exception E) {} Return Strret } Into a function to enter code, I have no success, I don't know why!

2. Filter Filter Setting Method

Package yourbean;

Import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util. *;

public class servfilter extends HttpServlet implements Filter {private FilterConfig filterConfig; // Handle the passed-in FilterConfig public void init (FilterConfig filterConfig) {this.filterConfig = filterConfig;} // Process the request / response pair public void doFilter (ServletRequest request, ServletResponse response, filterChain filterChain) {try {request.setCharacterEncoding ( "GB2312"); ((HttpServletResponse) response) .setHeader ( "Cache-control", "no-cache"); response.setHeader ( "Pragma", "No -cache "); Response.setHeader (" cache-control "," no-cache "); response.setHeader (" Expires "," 0 "); (httpservletResponse) response) .SetHeader (" Pragram "," NO -cache "); filterChain.doFilter (request, response);} catch (ServletException sx) {filterConfig.getServletContext () log (sx.getMessage (.));} catch (IOException iox) {filterConfig.getServletContext () log. (Iox.getMessage ());}} // clean up resources public void design () {}} The following is a web.xml file You will generate a

Welcome to tomcat Welcome to Tomcat servfilter YourBean .servfilter servfilter / * Compile the above servfilter to your web-inf / class / youbean / down Web.xml placed on Web-INF / Classes in a directory plus <% @ Page ContentType = "text / html; charset = GBK"%> is not very convenient, and there is no success in Tomcat, continue depressed!

3. I am using it now, recommend! !

Write a MyActionServlet and override the Process () method in the ActionServlet.

protected void process (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {/ ** @ todo Override this org.apache.struts.action.ActionServlet method * / request.setCharacterEncoding ( "GB2312") ; // add a line and do everything to solve super.process (Request, Response);

Of course, don't forget to change the configuration of Web.xml strutsdemo.myactionServlet Debug 2 config / web -Inf / struts-config.xml 2

Change the content in the servlet-class tag!

Really, one works Yi!

If the theory of specific encoding is not said, Google is already enough.

In addition, if you don't have Struts, Hibernate may also encounter Chinese garbled problems as long as it is in hibernate.cfg.xml configuration: JDBC: Microsoft: SQLServer: // localhost: 1433 "Selectmethod = cursor; characterencoding = GBK; DatabaseName = mydatabase.

Characterencoding = GBK! Yes.

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

New Post(0)