JavaRush /Java Blog /Random EN /Request encoding in Spring using CharacterEncodingFilter
Core
Level 35
Екатеринбург

Request encoding in Spring using CharacterEncodingFilter

Published in the Random EN group
Hello good man! Introduction There is a very popular problem - incorrect display of Russian-language characters in a web application. I read the articles and understood the root of evil and the stumbling blocks. I realized why these lines are needed on jsp pages and at what stages of data transfer in the client-server- DB chain : <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> My main problem lay in what was described in one good article (source: Tyts )
Here is the server that the admin built. And this is a web application that runs on a server built by the admin. And this is a servlet that is part of a web application that runs on a server that the admin built. Or JSP, which is the same servlet. And this servlet receives a request. And it contains data. Which? A set of bytes. What does a servlet need? Symbols. Question: what encoding should I use? Another excursion into HTTP. Data from the form when using the POST method is transmitted indicating a specific content type: Content-type: application/x-www-form-urlencoded Theoretically, the encoding of the transmitted data can also be specified here: Content-type: application/x-www-form- urlencoded; charset=UTF-8 In any case, Tomcat is trying to get it from here. In practice, only Opera specifies the encoding. Maybe something will change in the future, but for now the facts are as follows: the encoding of the incoming data is unknown! And something needs to be done about this.
The solution is to use a filter. It’s well written here and that’s what I did (source: tyts ) I inserted this configuring code into web.xml Now everything is fine and I’m basically happy, but I want to configure this filter through Spring annotations , since I carry out all other configurations with using java classes. And somehow I want uniformity. I have 2 configuration classes: one configures the application, the other configures the application context (dependency injection). Here is the code for the first class. I believe that the filter configuration needs to be inserted into this class, but something is a little off with the code. This is my first experience with a web application, so I’m asking for help from more experienced people. Thank you. characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true characterEncodingFilter /* package net.kaa.todolist.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.register(ApplicationContextConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION