本帖最后由 CodeGreat 于 2010-03-28 12:06:15 编辑

解决方案 »

  1.   

    request.setCharEncoding("GBK");
    response.setCharEncoding("GBK");  
    --
     将上面的代码写在servlet 请求的方法上的第一行
    --
    或者用过滤器
    --
    下面是过滤器类 类名自定义
    package filter;import java.io.*;import javax.servlet.*;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 { if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null) {
    request.setCharacterEncoding(encoding);
    }
    }
    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); }
    }
    --
    WebRoot/WEB-INF/
    web.xml
    --
    <?xml version="1.0" encoding="UTF-8" ?> 
    - <web-app 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">
    - <welcome-file-list>
      <welcome-file>index.jsp</welcome-file> 
      </welcome-file-list>
    - <filter>
      <filter-name>Set Character Encoding</filter-name> 
      <filter-class>filter.SetCharacterEncodingFilter</filter-class> 
    - <init-param>
      <param-name>encoding</param-name> 
      <param-value>utf-8</param-value> 
      </init-param>
      </filter>
    - <filter-mapping>
      <filter-name>Set Character Encoding</filter-name> 
      <url-pattern>/*</url-pattern> 
      </filter-mapping>
      </web-app>
      

  2.   

    <jsp:useBean id="login" type="natures.bean.Login" scope="session"/> 
    <jsp:getProperty name="login" property="logname"/>
    没有<jsp:setProperty name="login"/>,get是拿不到值的,
    <jsp:useBean id="login" type="natures.bean.Login" scope="session"/> 这句话等于重新创建了个对象,没给值,就像拿值,肯定是null
      

  3.   


    额,这一大堆。。
    没搞清楚您指的是放哪里..囧...经我窒息一段时间,今晚又费了几个小时,终于发现问题了,悲剧....是字符集的问题。有一个页面的页面属性 Charset=GB2312写成了chareset=GB2312然后,在servlet中的字符串也要用以下方法修改下:(JAVA中默认的字符集是UTF-8,而我的页面为GBK2312)  String s="游客 " ;
          byte bb[]=s.getBytes("GBK2312");
          s=new String(bb);
          loginbean.setLogname(s);终于解决了好悲剧