我想在 servlet中输出一个utf-8编码的中文字符串,但浏览器一直请求后返回的是??
设置setCharacterEncoding, 加filter 也不好用
想问一下,该怎么样解决?
public void doGet(HttpServletRequest request, HttpServletResponse response )
  throws IOException, ServletException
 {
// ((ServletRequest) response).setCharacterEncoding("utf-8"); 
response.setContentType("text/html; charset=UTF-8" ); 
request.setCharacterEncoding("UTF-8");

ServletOutputStream pw = response.getOutputStream();
     
     String str= "中文";
  //   String strr = new String(str.getBytes("UTF-8"), "UTF-8");
   pw.print(str);    
 }

解决方案 »

  1.   

    在print上边加一句:response.setCharacterEncoding("UTF-8");
    试一下,祝好运。
      

  2.   

    response.setContentType("text/html; charset=UTF-8" ); 
    response.setCharacterEncoding("GBK");
    ServletOutputStream pw = response.getOutputStream();
    String str= "我是中国人";
    pw.print(new String(str.getBytes("GBK"),"ISO-8859-1"));
      

  3.   


    你传输的时候用的是utf-8,但是接收时候是默认的编码,编码不一致就出现了乱码。。
      

  4.   


    我的response 咋没有这个方法呢?
      

  5.   

    查了一下 servlet的版本是 3.0
      

  6.   


    HttpServletResponse res = (HttpServletResponse)response;
    res.setCharacterEncoding("UTF-8");
      

  7.   

    public class HttpServletrs extends HttpServlet
    { /**
     * 
     */
    private static final long serialVersionUID = -5746185404053991459L; /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {  response.setContentType("text/html; charset=UTF-8" ); 
         request.setCharacterEncoding("UTF-8");  PrintWriter out = response.getWriter();
     out.print("我是中国人");
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    { this.doPost(request, response);
    }
    }
      

  8.   

    package com.config;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class HttpServletrs extends HttpServlet
    { /**
     * 
     */
    private static final long serialVersionUID = -5746185404053991459L; /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {  response.setContentType("text/html; charset=UTF-8" ); 
         request.setCharacterEncoding("UTF-8");      ServletOutputStream pw = response.getOutputStream();      String str = "我是中国人";
         
         pw.print(new String(str.getBytes("UTF-8"),"ISO-8859-1"));    
         
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    { this.doPost(request, response);
    }
    }