posts - 9,comments - 3,trackbacks - 0

 Here is my solution for  mess code on page,hope this can help you!
 The point is your database coding should be consistent with the coding of  character that you plan to insert into the database.
 Attention: Here,I take "UTF-8" as default character coding way .
 There are three steps:
 1. set page charset 
     e.g      <%@ page language="java" contentType="text/html; charset=UTF-8" %>
 2. create character filter:
     package com.victory.util;

     import javax.servlet.http.HttpServlet;
     import javax.servlet.Filter;
     import javax.servlet.FilterConfig;
     import javax.servlet.ServletException;
     import javax.servlet.ServletRequest;
     import javax.servlet.ServletResponse;
     import javax.servlet.FilterChain;
     import javax.servlet.http.*;
     public class CharacterEncodingFilter
       extends HttpServlet
       implements Filter {

       private FilterConfig filterConfig;
       private String targetEncoding = "ASCII";

      * Called by the web container to indicate to a filter that it is being placed
      * into service.
      * @param filterConfig FilterConfig
      * @throws ServletException
      * @todo Implement this javax.servlet.Filter method
      public void init(FilterConfig filterConfig) throws ServletException {
      this.filterConfig = filterConfig;
      this.targetEncoding = filterConfig.getInitParameter("encoding");

     * The <code>doFilter</code> method of the Filter is called by the container
     * each time a request/response pair is passed through the chain due to a
     * client request for a resource at the end of the chain.
     * @param request ServletRequest
     * @param response ServletResponse
     * @param chain FilterChain
     * @throws IOException
     * @throws ServletException
     * @todo Implement this javax.servlet.Filter method
     public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                       FilterChain chain) throws IOException, ServletException {
      try {
        HttpServletRequest request = (HttpServletRequest) srequest;
        chain.doFilter(srequest, sresponse);
      catch (ServletException sx) {
      catch (IOException iox) {

     * Called by the web container to indicate to a filter that it is being taken
     * out of service.
     * @todo Implement this javax.servlet.Filter method
     public void destroy() {
       filterConfig = null;
       targetEncoding = null;
  3.config web.xml
    attention: add these to your web.xml
  4.set database configration
     modify the file:    my.ini
     [client]     default-character-set=utf8 
     [mysqld]  default-character-set=utf8
  5.restart Mysql server
  6.modified your table coding way to utf8

     or ceate your table like this :
     CREATE TABLE `user` (
    `ID` int(11) NOT NULL auto_increment,
    `USERNAME` varchar(50) NOT NULL default '',
    `PASSWORD` varchar(50) NOT NULL default '',
     PRIMARY KEY  (`ID`)
  7.restrart your tomcat sever

   OK,it's all.

   Authrougn I have sovled   my problem, I think I  don't have enough understanding for it,  So hope    communicate with you! 

   Attention:mess code also exist in your database,through page hasn't mess code.


posted on 2006-03-23 13:07 freefly 阅读(332) 评论(0)  编辑  收藏 所属分类: StrutsJavaDatabaseHibernateSpring