繁体   English   中英

TomCat中的JSP文件编码

[英]JSP file encoding in TomCat

我已经使用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

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

response.setCharacterEncoding(“ UTF-8”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM