Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自jsp表单的Java UTF-8编码_Java_Mysql_Jsp_Tomcat_Servlets - Fatal编程技术网

来自jsp表单的Java UTF-8编码

来自jsp表单的Java UTF-8编码,java,mysql,jsp,tomcat,servlets,Java,Mysql,Jsp,Tomcat,Servlets,我尝试做一个java web应用程序。在本地Tomcat7服务器中一切都很好。我有一个jsp文件 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 它是有效的。但在jelastic tomcat服务器中,

我尝试做一个java web应用程序。在本地Tomcat7服务器中一切都很好。我有一个jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
它是有效的。但在jelastic tomcat服务器中,它无法工作,这些土耳其字符“ş”、“ğ”、“ı”正在插入mySql数据库“?”

如果我更新单元格,它会显示在文件true中


我能做什么?我在网上什么都试过了,但都没变

仔细检查以下设置,确保每个人都知道这是UTF-8聚会

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Page Title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="format-detection" content="telephone=no" />
</head>
<body>
your html content goes here....
</body>
</html>
让JDBC连接知道utf-8字符集

<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
  maxActive="10" maxIdle="2" maxWait="10000"
  username="myuid" password="mypwd"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8"
  validationQuery="SELECT 1"
/>
请注意.jsp页面中的空白字符。我使用此技术设置多个标记头,查看结束标记和开始标记是如何彼此相邻的

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
   page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"
   import="java.util.*, 
             java.io.*"
%><%
   request.setCharacterEncoding("UTF-8");
   String myvalue = "hello all and ÅÄÖ";
   String param = request.getParameter("fieldName");
   myvalue += " " + param;
%><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Page Title</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="format-detection" content="telephone=no" />
</head>
<body>
your html content goes here.... <%= myvalue %>
</body>
这是一篇很长的帖子,但它几乎涵盖了大多数角落的问题。

上面的答案中的短语“尽早打电话”一针见血

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    String command = request.getParameter("command");
    ...
工作。但是,

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String command = request.getParameter("command");
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    ...

不起作用。

jsp文件的内部承诺使用utf-8,但您确定它确实使用utf-8吗?项目中的文件编码是否设置为utf-8?响应的编码不是utf-8。请求不是发送真字符,但在servlet中,我在本地主机中使用setCharacterEncoding()方法更改它。在jsp页面中,静态字符串显示为true。而且我的项目的文本文件编码在应用程序属性中为UTF-8。感谢您的回复。这是一个非常好的答案,但我试着按照您所说的去做,它不起作用。您确定servlet/jsp页面得到了正确的字符。如果将byte[]bytes=mystr.getBytes(“UTF-8”)数组作为ascii数字转储到应答中。您认为土耳其语字母的utf8字节序列正确吗?字节顺序如下:[B@3f7d8bac我不明白。getCharacterEncoding()返回null。请查看此字节到十六进制字符串,然后查看序列是否正确。
if (req.getCharacterEncoding() == null)
      req.setCharacterEncoding("UTF-8");
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
   page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"
   import="java.util.*, 
             java.io.*"
%><%
   request.setCharacterEncoding("UTF-8");
   String myvalue = "hello all and ÅÄÖ";
   String param = request.getParameter("fieldName");
   myvalue += " " + param;
%><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Page Title</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="format-detection" content="telephone=no" />
</head>
<body>
your html content goes here.... <%= myvalue %>
</body>
response.setContentType("text/html; charset=UTF-8");
response.getOutputStream().write( myData.getBytes("UTF-8") );
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    String command = request.getParameter("command");
    ...
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String command = request.getParameter("command");
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    ...