<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>聊天室</title>
</head><body>
一个简单的聊天室
<br><hr><font color=red>
<%
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
out.println(content);
getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")+"<br>");
%>
</font>
<hr>
<form action="ServletContext_chast.jsp">
<input type=text name=content>
<input type=submit value="说话呀">
</form>
</body>
</html>
输出部分内容如下:
一个简单的聊天室 --------------------------------------------------------------------------------
nullnull
??????°?°?
pi
???è??
???è??
????
????????
null
×???
??
heoo
fsla
fdsfa
fdsfa
°é×???
??
??

解决方案 »

  1.   

    String content=(String)getServletContext().getAttribute(new String("chatTopic_1".getBytes("ISO-8859-1"),"GB2312"));
      

  2.   

    如果你用的是TOMCAT服务器,则把%TOMCAT安装目录%/ webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下,如果没有filters目录,就创建一个。
    在你的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>
     打开tomcat的server.xml文件,找到区块,加入如下一行:
    URIEncoding=”GBK”
    完整的应如下:
    <Connector 
    port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    enableLookups="false" redirectPort="8443" acceptCount="100"
    debug="0" connectionTimeout="20000" 
    disableUploadTimeout="true" 
    URIEncoding="GBK"
    />
    重启tomcat,一切OK。
      

  3.   

    response.setContentType("text/html;charset=GBK");去掉request.setCharacterEncoding("GBK");改为gb2312
      

  4.   

    回楼上,我本来就是用gb2312不行才改成GBK的,不关这个事还有,楼上的楼上
    %TOMCAT安装目录%/ webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件
    我用的是tomcat5.5,我找遍了整个文件夹,根本就找不到你说的那个些路径呀,
      

  5.   

    你用tomcat5.5以下的版本就可以找到这个SetCharacterEncodingFilter.class文件的
      

  6.   

    老兄,我现在用的是tomcat5.5呀,你不会让我重新download个低一点的版本来试下吧?
    有没有tomcat5.5的解决方法呢
      

  7.   

    String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
    out.println(content);把上面这两行代码改成下面的试一下:String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
    byte temp[] =N_Body.getBytes("ISO-8859-1");
    content=new String(temp);
    out.println(content);
      

  8.   

    不好意思,上面写错了一点,把第二行的N_Body改成content就可以了:
    String content=request.getParameter("Username").toString();
    byte temp[] =content.getBytes("ISO-8859-1");
    content=new String(temp);
    out.print(content);
      

  9.   

    首先我觉得
    String content=request.getParameter("Username").toString();
    应该改成
    String content=request.getParameter("content").toString();
    是吧?其次,改之前跟改之后都不行,错误如下HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /testApplication.jsp:1512: <%
    13: response.setContentType("text/html;charset=GBK");
    14: request.setCharacterEncoding("GBK");
    15: String content=request.getParameter("content").toString();
    16: byte temp[] =content.getBytes("ISO-8859-1");
    17: content=new String(temp);
    18: out.print(content);
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    root cause java.lang.NullPointerException
    org.apache.jsp.testApplication_jsp._jspService(org.apache.jsp.testApplication_jsp:57)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
    --------------------------------------------------------------------------------Apache Tomcat/5.5.15
      

  10.   

    在Apache Tomcat/5.0.28上实验你的程序没有问题啊
      

  11.   

    你的JSP中采用了两种编码方式
    1。
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    2。
    response.setContentType("text/html;charset=GBK");
    request.setCharacterEncoding("GBK");你可以统一一下编码方式,同时你可以加一个编码过滤器。
      

  12.   

    过滤器:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;public class EncodingFilter extends HttpServlet implements Filter {
        private static String ENCODING = "GBK";
        private FilterConfig filterConfig;
        //Handle the passed-in FilterConfig
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
        }    //Process the request/response pair
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain filterChain) {
            try {
                request.setCharacterEncoding(ENCODING);
                filterChain.doFilter(request, response);
                response.setCharacterEncoding(ENCODING);
            } catch (ServletException sx) {
                filterConfig.getServletContext().log(sx.getMessage());
            } catch (IOException iox) {
                filterConfig.getServletContext().log(iox.getMessage());
            }
        }    //Clean up resources
        public void destroy() {
        }
    }web.xml的配置
     <filter>
        <filter-name>encodingfilter</filter-name>
        <filter-class>cn.com.news.filter.EncodingFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>encodingfilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>