Tuesday, July 28, 2015

JSP encoding error on weblogic server

If you are getting the error:

The encoding specified on the page cannot be different than detected encoding for the file.
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

during JSP compilation by weblogic, chances are weblogic is not recognizing your file encoding correctly. To solve that, put the encoding tag at the very top of your JSP file:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

And when I say very top, I mean the very first declaration before any other declarations in the jsp. Weblogic expects the page encoding tag to be the first declaration on file:

Wrong:
<%@ page import="com.mycompany.blabla"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

Right:
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="com.mycompany.blabla"%>

The following excerpt was extracted from a stackoverflow question (http://stackoverflow.com/questions/3141410/weblogic-throws-compilationexception): 
"It's more than just rearranging those two lines. Actually, it's not even a rearrangement, but more like moving that directive to the very top of everything else. The JSP directive containing the pageEncoding MUST BE the very first thing in the file. That means you cannot have JSP blocks, JSP directives, JSP comments or anything before that. Anything!"

The encoding error can also happen to tag libraries. In that case, put the following tag at the very top of your file:

<%@tag description="Component Info tag for Taxrules" pageEncoding="UTF-8"%>