我写的
package com;import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;public class DateServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html; charset=GB2312");
response.setCharacterEncoding("GB2312");
out.print("<html>");
out.print("<body>");
response.setHeader("Refresh", "2");
out.print("Now Time:");
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
String dateString = formatter.format(currentTime); out.println("<br>" + dateString);
String Uname = "返回";
String naString=new String(Uname.getBytes("GBK"),"GB2312");

out.println("<a href='servlet.jsp'>" + naString + "</a>");
out
.print("<h3>九九乘法口诀表</h3>"
+ "<table width='90%' border='1' align='center' cellpadding='0' cellspacing='1' id='Table1'>"
+ "<tr bgcolor='#E6E6E6'>" + "<th>1</th>"
+ "<th>2</th>" + "<th>3</th>" + "<th>4</th>"
+ "<th>5</th>" + "<th>6</th>" + "<th>7</th>"
+ "<th>8</th>" + "<th>9</th>" + "</tr>");
for (int i = 1; i <= 9; i++) {
out.print("<tr>");
for (int j = 1; j <= i; j++) {
out.print("<td bgcolor='#CCCCCC' align='center'>");
out.print(i);
out.print("*");
out.print(j);
out.print("=");
out.print(i * j);
}
out.print("</tr>");
}

Counter counter = new Counter();
String cou = String.valueOf(counter);
out.print(cou);
out.print("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
把你那个Uname 的String输到网叶上就成????了

解决方案 »

  1.   

    out.println(" <a href='servlet.jsp'>" + naString + " </a>"); 
    这个地方乱码?
    不用Uname.getBytes("GBK")呀,系统用默认方式编码的,这样你得到的是GBK的byte array,然后你对GBK的byte array去构造gb2312的string,肯定会乱码呀
      

  2.   

    String naString=new String(Uname.getBytes("iso-8859-1"),"GB2312"); 
      

  3.   

    request.setCharacterEncoding("GB2312"); 
      

  4.   

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    request.setCharacterEncoding("GB2312"); //加上这句
      

  5.   

    request response都要设置编码.
      

  6.   

    设置一下编码转换就可以了
    fuyou001说的很好
      

  7.   

    一般来说,这种直接从SERVLET输出网页到客户端,有下面两句话就OK了
    response.setContentType("text/html; charset=GB2312"); 
    response.setCharacterEncoding("GB2312"); 
    如果不行就配过滤器,过滤器是处理乱码的主要方法,当用了过滤器仍有乱码,我們才通常采取其它方法来解决,你这种应该用过滤器才能解决像这种request.setCharacterEncoding("GB2312");
    String naString=new String(Uname.getBytes("iso-8859-1"),"GB2312");  
    String naString = URLDecoder.decode("返回","GB2312");
    这些都处理请求的乱码而不是输出,希望不要乱用
      

  8.   

    最好使用过滤器 可以避免在每个Servlet写编码装换代码。如果还不行就改服务器编码 页面,服务器和项目编码统一。要是还不行 砸了那电脑!
      

  9.   

    写成
    response.setContentType("text/html; charset=GB2312"); 
    response.setCharacterEncoding("GB2312"); 
    PrintWriter out = response.getWriter();