Solving Encoding Issues in GSP Pages (Without Grails)

At one point, there was one noticeable problem that was preventing me from completely 100% replacing PHP with Groovy for the web without using the relatively heavyweight Grails MVC framework.

This applies to * .gsp pages (Groovy Server Pages), which are html pages with inserts of the form <% ...%> with arbitrary code in Groovy or Java, or in the original language: " GSP means GroovyServer Pages, which is similar to JSP ( JavaServer Pages). "



Exactly the same functionality in PHP is implemented by inserts <? ...?> (Moreover, for PHP the presence of such a block is mandatory even if it implements classes or business logic; if after such a block there will usually be an invisible space or line break, then it will fall into conclusion, which can lead to problems).

The groove site says: " GSP are not maintained as a standalone module. But it has been forked and reintegrated in Grails ." But, nevertheless, like groove servlets, gsp-pages work without Grails, just connect the grooves to the application.

The problem was that in these gsp-pages the Russian text turned into "crooked hair". In the groove servlets, both in the “real” and script types, this was not observed.

By the method of scientific poking, it turned out that if the source gsp was transferred to the default encoding on the system (for example, cp1251 for Windows), then the problem is solved (moreover, the page is already output to UTF-8).

And this is pure hint: we go to the sources of the servlet groovy.servlet.TemplateServlet.java, which is responsible for parsing such pages, and find the line there:

privatestaticfinal String GROOVY_SOURCE_ENCODING = "groovy.source.encoding";


It also shows how this parameter affects the creation of an instance of Reader.

We write the parameter to the servlet with the value “UTF-8” in the web.xml of the application, and voila, the problem is solved.

The full text of the web.xml application, in which all arbitrary * .groovy and * .gsp files will be compiled into servlets and correctly output the Cyrillic alphabet:

<?xml version="1.0" encoding="UTF-8"?><web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
			http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><servlet><servlet-name>GroovyServlet</servlet-name><servlet-class>groovy.servlet.GroovyServlet</servlet-class></servlet><servlet-mapping><servlet-name>GroovyServlet</servlet-name><url-pattern>*.groovy</url-pattern><url-pattern>*.grv</url-pattern></servlet-mapping><servlet><servlet-name>GSP</servlet-name><servlet-class>groovy.servlet.TemplateServlet</servlet-class><init-param><param-name>groovy.source.encoding</param-name><param-value>UTF-8</param-value></init-param></servlet><servlet-mapping><servlet-name>GSP</servlet-name><url-pattern>*.gsp</url-pattern></servlet-mapping></web-app>


It also shows how a long .groovy extension that will “glow” in urls can be replaced with an arbitrary short one, for example .grv (or .php - just 4 fun).

Now you can quickly and easily write websites, web applications, frameworks on Groovy, for which you only need installed Java and Tomcat on the server.

Also popular now: