------------------SubmitMessage.jsp-------------------------<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="SubmitPage.jsp" method="post">
用户名:<input type="text" name="name" size="20"><br>
留  言:<input type="textarea" name="message" size="300"><br>
<input type="submit" value="提交"><input type="Reset" value="重添">
</form></center></body>
</html>
------------------------SubmitPage.jsp---------------------<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <jsp:useBean id = "sub" scope="page" class="com.beans.GuestBookBean"/>
    <jsp:setProperty name="sub" property="*"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SumbitPage</title>
</head>
<body>
用户是:<%=sub.getName()%><br>
留 言:<%=sub.getMessage() %><br></body>
</html>-----------GuestBookBean.java--------------package com.beans;public class GuestBookBean {
private int id;
private String name;
private String message;
private String savetime;
/**
 * @return Returns the id.
 */
public int getId() {
return id;
}
/**
 * @param id The id to set.
 */
public void setId(int id) {
this.id = id;
}
/**
 * @return Returns the message.
 */
public String getMessage() {
return message;
}
/**
 * @param message The message to set.
 */
public void setMessage(String message) {
this.message = message;
}
/**
 * @return Returns the name.
 */
public String getName() {
return name;
}
/**
 * @param name The name to set.
 */
public void setName(String name) {
this.name = name;
}
/**
 * @return Returns the savetime.
 */
public String getSavetime() {
return savetime;
}
/**
 * @param savetime The savetime to set.
 */
public void setSavetime(String savetime) {
this.savetime = savetime;
}

}

解决方案 »

  1.   

    在网上找了个过滤器也不行
    /*
     * ====================================================================
     *
     *              JavaWebStudio 开源项目
     *               
     *               Struts_db v0 dot 1
     *
     * ====================================================================
     */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;
    /**
    *   中文过滤器
    */
    public class SetCharacterEncodingFilter implements Filter { 
    // ----------------------------------------------------- Instance Variables
    /**
    * 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.
    *
    * at 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.
    *
    * at 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
    * null.
    * * The default implementation unconditionally returns the value configured
    * by the encoding initialization parameter for this
    * filter.
    *
    * at param request The servlet request we are processing
    */
    protected String selectEncoding(ServletRequest request) { 
    return (this.encoding);
    }
    }//EOC
    =============web.xml=============配制<?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>GuestBook</display-name> <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list> <filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping></web-app>哪位大哥能帮忙啊!谢谢啦!
      

  2.   

    我是将JSP中的字符集设置成UTF-8  想用过滤器不知道能实现吗?我无论怎么改在
    用户是:<%=sub.getName()%><br>
    留 言:<%=sub.getMessage() %><br>
    这里都是乱码 过滤器也改成UTF-8试验过。不行!要疯啦~~~~~~~~~~~~~~~~~~
      

  3.   

    name=new String(name.getString("ISO8859_1"),"UTF-8")页面输入参数都是ISO8859_1字符编码的,接收的时候需要转化一下
      

  4.   

    那name=new String(name.getString("ISO8859_1"),"UTF-8")
    应该写到哪里啊?是bean中吗?
    不是很明白啊~~~
      

  5.   

    原因:tomcat的默认编码方式是iso-8859-1(西欧字符),所以提交的表单中文数据全部为问号。
    过滤器的作用就是转换字符编码。