【问题标题】:JSP file encoding in TomCatTomCat 中的 JSP 文件编码
【发布时间】:2016-11-17 18:34:20
【问题描述】:

我使用过 TomCat 8.00 和 IntelliJ IDEA 14.0 和 h2 数据库。当我从数据库中注册表单(西里尔文)的文本字段中获取文本时,记录如下:

重要的是我做了这些配置。

这是我的 index.jsp 文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Home</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

这是我的 servlet

public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("usernameReg");
        String pass = request.getParameter("passReg");
        String name = request.getParameter("firstNameReg");
        String lastName = request.getParameter("lastNameReg");
        String email = request.getParameter("emailReg");

        request.setAttribute("Username", username);

        StudentBean regstudent;
        try {
            regstudent = new RegisterDAO().registerStudent(username, pass, name, lastName, email);
            request.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

还有我的 RegisterDAO 类:

public class RegisterDAO {

    Connection conn = null;
    PreparedStatement state = null;
    //ResultSet res = null;

    public StudentBean registerStudent(String userName, String pass, String name, String lastName, String email) throws SQLException{

        StudentBean result = null;
        String sql = "INSERT INTO STUDENT VALUES(null, ?, ?, ?, ?, ?)";

        try {
            conn = DbConnection.getInstance().getConnect();
            state = conn.prepareStatement(sql);
            state.setString(1, userName);
            state.setString(2, pass);
            state.setString(3, name);
            state.setString(4, lastName);
            state.setString(5, email);
            state.execute();
        } catch (SQLException e) {
            System.out.println("Нулл поинт");
            e.printStackTrace();
        }finally {
            if(state != null){
                state.close();
            }
        }
       DbConnection.getInstance().Disconnect();

        return result;
    }
}

在英语中一切正常。我认为jsp文件没有正确保存在UTF-8中,它仍然使用默认的IntelliJ编码。

如果有任何解决问题的想法,我将不胜感激。

最好的问候, D. Balamjiev

【问题讨论】:

  • 在intellij中打开你的jsp文件,查看右下角的内容,应该是你文件的编码
  • 它是 UTF-8,但不工作。
  • 检查你的数据库。
  • 据我所知H2已经支持Unicode,所以你不需要改变字符集。你能说得更具体一点吗?

标签: java jsp intellij-idea encoding h2


【解决方案1】:

您可以尝试将其添加到您的 servlet 中。

response.setCharacterEncoding("UTF-8")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多