[JavaServlet] Summary of correspondence when Japanese parameters are garbled

This is ayaka from Kids Programming Class ITWORKS. I will sew between childcare and describe it. This time I will forget the confirmation part about the garbled Japanese parameters of the HTML form data during my own development, so I will summarize it ☺️

Why is it easy for Japanese characters to be garbled in the first place?

This is because with the default settings, when Japanese parameters are sent as HTML form data, they are URL-encoded and sent to the email address indicated by action, the Jakarta Servlet of the Web server, etc. ** In other words, if you do not take measures, only Japanese parameters need to be decoded: sweat_smile: **

Japanese language measures differ depending on the form data submission method

--For POST transmission

Specify the character encoding declared in the HTML file in setCharacterEncoding of HttpServlet.HttpServletRequest. In case of POST transmission Since the parameters to be transmitted are included in the HTTP request body, it can be handled by setting HttpServletRequest.

--For GET transmission

Decode with the character encoding declared in the HTML file.

:SampleHttpServlet.java
form_data_in_japanese = request.getParameter("form-name");
URLEncoder.encode(form_data_in_japanese , "Character encoding");

In the case of GET transmission, nothing is described in the HTTP request body: flushed: It is added to the URL and sent. Therefore, if the acquired parameters include Japanese, decoding is required.

Recommended Posts

[JavaServlet] Summary of correspondence when Japanese parameters are garbled
Summary of strong parameters
Japanese characters described in MessageResources.properties are garbled
Summary of moss when updating from JMockit 1.4 to 1.30