1

I was working normally on my netbeans project. I sent post parameters from html forms to servlets and then capture them with request.getParameter... and all worked fine. But some hours ago, I don't know what happened with netbeans because all new servlets that I create, the requested post parameters are not encoded well. I mean that if I send "tílde" or "ñ" it will be received as "tílde" or "ñ". The old servlets still work fine, receiving well the post parameters.

With GET parameters works fine, but with POST parameters always the same with new servlets. Old servlets work fine post parameters.

I tested receiving parameters with php and they are correctly formated, so I discart the posibility of being the jsp encoding.

I think may be netbeans error, but I really dont know.

Here some code and images:

The servlet

public class TestParameters extends HttpServlet {

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

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    System.out.println("> Ñ,ñ, tílde. Parámetros: " + request.getParameter("name"));
}

The JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form method="post" action="/TestParameters">
            <input type="text" name="name">
            <button type="submit">Enviar</button>
        </form>
    </body>
</html>

Html input:

Text for example

NetBeans console with POST parámeters:

enter image description here

Netbeans console with GET parámeters:

enter image description here

And receiving with php, works well:

echo $_POST['name'];

Php html echo:

enter image description here

wilmerlpr
  • 448
  • 6
  • 15
  • try `request.setCharacterEncoding("UTF-8")` before reading paramters – ZhongYu Jan 13 '16 at 05:53
  • Yes, it now works with request.setCharacterEncoding("UTF-8"), but why it works and the old servlets work fine without setting request encondig? I tried minutes ago that setting that parameter but it doesn't work. Do you know what can be the problem? – wilmerlpr Jan 13 '16 at 05:57
  • 1
    well, to be safe, you should configure `UTF-8` everywhere:) ... and there are a lot of places where char encoding can be configured... too bad UTF-8 isn't often the default config. – ZhongYu Jan 13 '16 at 06:04
  • I have configured utf-8 everywhere with – wilmerlpr Jan 13 '16 at 06:26
  • Well, I have implemented a filter that works fine for all POST parameters. For GET paramteres the way is – wilmerlpr Jan 13 '16 at 07:21

1 Answers1

1

Finally I have resolved my problem. I have implemented a filter that encode all requests. But this only work for POST parameters that is what I need. That's because for POST parameters Tomcat's default encoding is iso-8859-1 and I was ignoring that. For GET parameters the way is

Encoding filter for java web application

wilmerlpr
  • 448
  • 6
  • 15