servlet中文显示是问号怎么解决

package com.serlet1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

  • Servlet implementation class No1
    */
    @WebServlet("/No1")
    public class No1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet() */ public No1() { super(); // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      response.getWriter().append("Served at: ").append(request.getContextPath());

      response.setContentType("text/html;charset=UTF-8");

      PrintWriter out = response.getWriter();
      String title = "使用 GET 方法读取表单数据";
      // 处理中文
      String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
      String docType = "<!DOCTYPE html> \n";
      out.println(docType +
      "\n" +
      "

      " + title + "\n" +
      "\n" +
      "

      " + title + "

      \n" +
      "
        \n" +
        "
      • 站点名:"
        + name + "\n" +
        "
      • 网址:"
        + request.getParameter("url") + "\n" +
        "
      \n" +
      "");
      }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }

}
图片说明

6个回答

乱码了,用tomcat8.0以上的,默认没问题。

qq_33550694
Eden__Chen 用的就是8.0
2 年多之前 回复

        response.setCharacterEncoding("utf-8");

设置一下编码

Small_Mouse0
小鼠标丶 回复Eden__Chen: 乱码形式变了??,,,utf-8改成gbk试试
2 年多之前 回复
qq_33550694
Eden__Chen 已经试过了,没用
2 年多之前 回复

可能是你的Tomcat安装的时候没有选择utf-8,重装一次试试。不过重装之前你要百度一下教程,不删除干净是重装不上的。

用request设置编码

你这情况多变,得看具体穿过来的编码。和相应的编码。肯定是某个地方设置得不同步。

编码要看你的请求方式是post还是get
post的话你可以在页面设置页面编码
get接收到数据自己处理

终极大招:改动服务器配置 在server.xml中修改编码格式为utf-8,一劳永逸

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐