myeclipse+struts+tomcat5.5 中文问题- -

最近在开发一个网站.是一个信息中介.主要内容就是注册 查询 和 收费.没有什么特别的地方.使用struts 1.2 +hibernate+myeclipse3.0  数据库用的是mssqlserver.

 一开始的时候,配置中文老出现问题.比较搞笑的是,网上有的中文问题的情况,都碰到过.这里,自己做个记录.以备将来查阅.

1.myeclipse 读取html文件时中文正常,但是把html文件改成.jsp后缀的时候,读入的就变成乱码.美工在dw设计html文件的时候在meta里面已经设置过编码方式了.   

解决方法:在每个jsp页面的第一行申明编码方式(先用记事本打开html页面,改完之后再改后缀)

< %@page contentType="text/html; charset=GBK" language="java"%>

2.再eclipse里中文显示正常,但是部署到tomcat里跑起来的时候又是乱码

解决方法:在tomcat的conf目录的server.xml文件里修改加入编码声明

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector
  port="8080"               maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true"
 URIEncoding="GBK" useBodyEncodingForURI="true"
     />

3.jsp页面显示正常的中文由form提交到action时就已经变成乱码

解决方法:加一个过滤器.在提交的时候把编码转换过来.

web.xm的新加配置如下

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.jfgcn.struts.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>

其中com.jfgcn.struts.SetCharacterEncodingFilter为转换的class的路径.转换类的代码如下
package com.jfgcn.struts;

import javax.servlet.*;
import java.io.IOException;

public class SetCharacterEncodingFilter implements Filter {

 /**
 * The default character encoding to set for requests that pass through
 * this filter.
 */
 protected String encoding = null;


 /**
 * The filter configuration object we are associated with. If this value
 * is null, this filter instance is not currently configured.
 */
 protected FilterConfig filterConfig = null;


 /**
 * Should a character encoding specified by the client be ignored?
 */
 protected boolean ignore = true;


//  --------------------------------------------------------- Public Methods


 /**
 * Take this filter out of service.
 */
 public void destroy() {

 this.encoding = null;
 this.filterConfig = null;

 }


 /**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 *
 * @param request The servlet request we are processing
 * @param result The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
 public void doFilter(ServletRequest request, ServletResponse response,
 FilterChain chain)
 throws IOException, ServletException {

//  Conditionally select and set the character encoding to be used
 if (ignore || (request.getCharacterEncoding() == null)) {
 String encoding = selectEncoding(request);
 if (encoding != null)
 request.setCharacterEncoding(encoding);
 }

//  Pass control on to the next filter
 chain.doFilter(request, response);

 }


 /**
 * Place this filter into service.
 *
 * @param filterConfig The filter configuration object
 */
 public void init(FilterConfig filterConfig) throws ServletException {

 this.filterConfig = filterConfig;
 this.encoding = filterConfig.getInitParameter("encoding");
 String value = filterConfig.getInitParameter("ignore");
 if (value == null)
 this.ignore = true;
 else if (value.equalsIgnoreCase("true"))
 this.ignore = true;
 else if (value.equalsIgnoreCase("yes"))
 this.ignore = true;
 else
 this.ignore = false;

 }


//  ------------------------------------------------------ Protected Methods


 /**
 * Select an appropriate character encoding to be used, based on the
 * characteristics of the current request and/or filter initialization
 * parameters. If no character encoding should be set, return
 * <code>null</code>.
 * <p>
 * The default implementation unconditionally returns the value configured
 * by the <strong>encoding</strong> initialization parameter for this
 * filter.
 *
 * @param request The servlet request we are processing
 */
 protected String selectEncoding(ServletRequest request) {

 return (this.encoding);

 }

 }

 

以上问题是在eclipse所有可以设置编码的地方都设置好的情况下发生并解决.

作者: 萧剑风

 
阅读更多
换一批

没有更多推荐了,返回首页