狼爱上狸

我胡汉三又回来了

tomcat 5.0.28的问题

1.配置成首页的问题

          <Logger className="org.apache.catalina.logger.FileLogger"
                 directory="logs"  prefix="localhost_log." suffix=".txt"
            timestamp="true"/>
        这里增加:
         <Context path="" docBase="C:/Tomcat/webapps/myweb" debug="0" reloadable="true" crossContext="true"/>

  要说的是myweb/web-inf/web.xml的文件最好改下名,我的从weblogic转过来,看了很多论坛,因为这里没改名,一直错误,还不知道怎回事,改了名就好了,该是冲突问题。

2. 乱码问题

如果你没有乱码,不用看,有了乱码,你就成乱麻,总得要解决,现在论坛有说这,有说那,只要解决了你的难题的,就是好方案。

我的问题时,从数据库中读出没乱码,写入出现乱码,我的方案是:

(1).在filters中做个SetCharacterEncodingFilter.java的bean文件。

package filters;

import java.io.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;

public class SetCharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

public void destroy() {
 this.encoding = null;
 this.filterConfig = null;
}

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);
}

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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}

编译上段代码时,要在classpath中注册servlet.jar文件。
(2).在web.xml中引入:

<filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>filters.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>GBK</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

(3)在server.xml中修改

                port="8080"  maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               debug="0" connectionTimeout="20000"
               disableUploadTimeout="true" URIEncoding="ISO-8859-1" />

     有种说法是把URIEncoding="ISO-8859-1"可换成URIEncoding="GBK",自己测试,对的就是硬道理。

最后顺便说下mysql-connector-java-5.0.3-bin.jar的jdbc连接代码,与以前低版本的不一样。

String sDBDriver="com.mysql.jdbc.Driver";
String sConnStr="jdbc:mysql://localhost:3306/gain?user=root&password=password";


posted on 2006-10-12 23:08 狼爱上狸 阅读(261) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航: