Angularjs - Problems with accentuation

Asked

Viewed 1,092 times

2

I’m getting strings on servlet with problems in accentuation. Such as for example: Send $scope.nome = "Pé" to the servlet. In the servlet the line: String nome = request.getParameter("nome"); does not receive and yes PÃc

Filing cabinet js requisition:

var ajaxApp = angular.module("myApp", []);

ajaxApp.controller("gridUsersControl", [ '$scope', '$http', function($scope, $http) {

    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";

    $scope.saveUser = function() {

        var data = {
            'action'    : 'saveUser',
            'id'        : $scope.id,
            'nome'      : $scope.nome,
            'sobrenome' : $scope.sobrenome,
            'email'     : $scope.email,
            'telefone'  : $scope.telefone,
            'rua'       : $scope.rua,
            'bairro'        : $scope.bairro,
            'cidade'        : $scope.cidade,
            'estado'        : $scope.estado,
        }

        $http({
            contentType: "application/json; charset=utf-8",
            url : 'UserController',
            method : "POST",
            params: data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}

        }).then(function(response) {

            $('#dialogEditUser').modal('hide');
            generalDialog('Usuário cadastrado com sucesso.');
            console.log(response.data);
            angular.element(document.getElementById('principalUsers')).scope().loadUsers();


        }, function(response) {
            generalDialog('Problemas ao cadastrar usuário.');
            console.log(response);
        });
    };
} ]);

My Servlet:

public class UserController extends HttpServlet{

    private static final long serialVersionUID = 1L;

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

        doPost(request, response);
    }

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

        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        String action = request.getParameter("action");

        switch(action){

            case "saveUser":
                saveUser(request, response);
        }   
    }

    private void saveUser(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        String nome = request.getParameter("nome");
        String sobrenome = request.getParameter("sobrenome");
        String email = request.getParameter("email");
        String telefone = request.getParameter("telefone");
        String rua = request.getParameter("rua");
        String bairro = request.getParameter("bairro");
        String cidade = request.getParameter("cidade");
        String estado = request.getParameter("estado");

        Usuario user = null;

        if(id != null){
            user = (Usuario) PersistEngine.findById(Usuario.class, Integer.valueOf(id));
        } else {    
            user = new Usuario();
        }

        user.setNome(nome);
        user.setSobrenome(sobrenome);
        user.setEmail(email);
        user.setTelefone(telefone);
        user.setRua(rua);
        user.setBairro(bairro);
        user.setCidade(cidade);
        user.setEstado(estado);
        PersistEngine.persist(user);
    }
}

File header Jsp meta tag:

"<"meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Does anyone know how I could solve?

  • When I had trouble with that, the question was on my server. I had to change the UTC inside the server in order to receive/send the characters correctly. I don’t know what it would be like in your case, but who knows how it helps in some way

1 answer

1

I’m not using a Servlet directly, but I’ll give you the settings I’m using. Maybe this will help you.

  • Prelude.jspf

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

    <!doctype html>
    <html lang="pt-br">
        <head>
         <meta charset="utf-8">
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.