【问题标题】:JSP encoding problemsJSP编码问题
【发布时间】:2017-01-17 12:45:49
【问题描述】:

我被困住了。我的问题是:当我将俄语文本数据发送到 mysql 时,它保存就像 ????。我尝试了很多不同的解决方案,但没有一个适合我。这是我的jsp文件。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
    <%@page import="java.sql.*" %>
    <%@page import="com.mysql.jdbc.PreparedStatement" %>
    <%@page import="com.mysql.jdbc.Connection" %>
    <%@ page import="java.util.Enumeration" %>
    <%@ page import="java.io.PrintWriter" %>

<fmt:requestEncoding value="UTF-8" />
<%request.setCharacterEncoding("UTF-8");response.setContentType ("text/html; charset=UTF-8");   %>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi">
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP</title>
</head>
<body>
<%@ page contentType="text/html; charset=UTF-8" %>
<%
    try {
        response.setContentType ("text/html; charset=UTF-8");
        request.setCharacterEncoding ("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Connection con, conn;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        conn = (Connection) DriverManager.getConnection("jdbc:mysql:localhost:3306/schedule", "sch", "sch");
        PreparedStatement zxc = (PreparedStatement) conn.prepareStatement("SET NAMES utf8");

        //zx.setString(1, login);
        zxc.execute();
        zxc.close();
        String dat = request.getParameter("btn");
        String day_num = request.getParameter("day_num" + dat.substring(dat.length()-1, dat.length()));
        String first = request.getParameter("first" + dat.substring(dat.length()-1, dat.length()));
        String second = request.getParameter("second" + dat.substring(dat.length()-1, dat.length()));
        String third = request.getParameter("third" + dat.substring(dat.length()-1, dat.length()));
        String fourth = request.getParameter("fourth" + dat.substring(dat.length()-1, dat.length()));
        String fifth = request.getParameter("fifth" + dat.substring(dat.length()-1, dat.length()));
        String sixth = request.getParameter("sixth" + dat.substring(dat.length()-1, dat.length()));
        String seventh = request.getParameter("seventh" + dat.substring(dat.length()-1, dat.length()));
        String group = dat.substring(0, dat.length()-1);
        out.println(group);



        String bad_redirectURL = "login.jsp";
        String good_redirectURL = "tables.jsp?group=" + dat.substring(0, dat.length()-2);


        con = (Connection) DriverManager.getConnection("jdbc:mysql:localhost:3306/schedule", "user_creator", "user_creator");

        Statement rs = con.createStatement();
        rs.executeQuery("SET NAMES 'UTF8'");
        rs.close();


        String query = "UPDATE " + group +" SET first = ?, second = ?, third = ?, fourth = ?, fifth = ?, sixth = ?, seventh = ? WHERE day_num = ?;";




        PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);




        ps.setString(1, first);
        ps.setString(2, second);
        ps.setString(3, third);
        ps.setString(4, fourth);
        ps.setString(5, fifth);
        ps.setString(6, sixth);
        ps.setString(7, seventh);
        ps.setString(8, day_num);
        out.println(ps.toString());



        ps.execute();

        response.sendRedirect(good_redirectURL);



    } catch (NullPointerException | SQLException e) {
        e.printStackTrace();
    }
%>
</body>
</html>

我已将此行添加到server.xml

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

我也将此添加到web.xml

<jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>

【问题讨论】:

  • 你的表字段编码是如何定义的?
  • 我的表也是 UTF-8。
  • 所以,一切似乎都很好(我认为您确实检查了您的数据库表,请记住,除了将表定义为 UTF8 之外,它还应该是 utf8mb4,也就是 utf8mb4_general_ci,您的 varchar 列应该被定义一样)。我的另一个建议是检查项目文件(.java、.jsp、web.xml 等)的编码,如果其中一个也不是 UTF8,那么它可能是你的问题。我建议你使用 notepad++ 来检查你的文件。

标签: java mysql jsp encoding


【解决方案1】:

我认为你错过了useUnicode=true

阅读Trouble with utf8 characters; what I see is not what I stored中的“问号”,它说这些是可能的原因:

  • 要存储的字节未编码为 utf8/utf8mb4。解决这个问题。
  • 数据库中的列是 CHARACTER SET utf8(或 utf8mb4)。解决这个问题。
  • 另外,请检查读取期间的连接是否为 UTF-8。

如果您仍然遇到问题,请使用该链接中的故障排除提示,看看您是否获得了成对的十六进制字节 Dxyy。在 utf8 中,Cyrillic 的第一个字节是 D0 到 D4。如果您没有看到,请回来进一步讨论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2011-11-02
    相关资源
    最近更新 更多