简体   繁体   中英

JSP file encoding in TomCat

I have use TomCat 8.00 and IntelliJ IDEA 14.0 and h2 database. When I get text from textfields in register form (cyrillic) records in the database are like this: 图片

It's importand to say that I made these configurations. 在此处输入图片说明

This is my index.jsp file

    <%@ 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"/>

This is my 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();
        }
    }

and my RegisterDAO class:

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;
    }
}

In English everything works fine. I think that jsp file is not corectly saved in UTF-8 and it still use default IntelliJ encoding.

I would be grateful for any ideas to resolve problem.

Best regards, D. Balamjiev

You can try to add this in your servlet.

response.setCharacterEncoding("UTF-8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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