Unable to set character encoding UTF-8 in .xhtml - primefaces

I have set the xhtml to UTF-8 and at ServletRequest. But it seems that it is still unable to detect the character encoding as UTF-8. I am working with Primefaces 3.5 with WebSphere 8.
.xhtml
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
RedirectLogin
public class RedirectLogin implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
chain.doFilter(request, response);
}
}
web.xml
<filter>
<description>Redirect unauthenticated session to login page.</description>
<display-name>RedirectLogin</display-name>
<filter-name>RedirectLogin</filter-name>
<filter-class>com.belsize.servlet.filter.RedirectLogin</filter-class>
<init-param>
<param-name>login_page</param-name>
<param-value>/faces/login.xhtml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RedirectLogin</filter-name>
<url-pattern>/RedirectLogin</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>RedirectLogin</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>

I guess my environment is "dirty". After I clean my project, redeploy, its working fine now.

Related

JSP Loading Incompletely on Websphere Application Server 8.5

I am planning to migrate from Websphere Application Server (WAS) 7 to version 8.5. I have deployed an application which runs fine on WAS 7 and I have not made any changes to it since migration.
However, on WAS 8.5, the JSP pages are not being getting loaded completely. When I examine these pages through "View Source," I can see that the HTML content is only half-loaded. Specifically, the HTML itself is not completed with closing tags.
In WAS 7, the result of "View Source" looks like this:
<html>
...
...
<td..../>
<td..../>
<td..../>
...
...
</html>
But the same in WAS 8.5 looks like:
<html>
...
...
<td..../>
<td..../>
<td..
I have done the following so far:
I compared the class files of compiled JSP on WAS 7 and WAS 8.5. They are almost same, so I assume that the compilation is done properly. However, displaying the page with in HTML is not getting done properly.
I tried enabling JavaScript debugging on IE, but it did not show any errors while loading.
There are no errors in application logs and server logs that I can see.
My questions:
The set of <td> tags above is generated through JSP custom tags. Should I check code of the tags?
Is there any Custom property in Web Container Settings in Websphere which control such behaviour?
Is there any timeout property which is causing page to stop loading half-way?
Please suggest what else should I check.
this is a well known behavior that happens when an Exception is thrown and Response has already been commited.
generally the exception is logged, but in some particular case it is not.
i read you workarounded removing EncodingFilter, but if you still want to find the problem you can try to code a Filter, that must be executed BEFORE EncodingFilter, wich sets response.setBufferSize to a larger size.
this can be such a filter:
public class ResponseBufferFilter implements Filter
{
private FilterConfig filterConfig;
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
try
{
// or better take the value from filterConfig.getInitParameter
response.setBufferSize(100000000);
chain.doFilter(request, response);
}
catch(Exception e)
{
e.printStackTrace();
throw new ServletException(e.getMessage(), e);
}
}
#Override
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
}
#Override
public void destroy()
{
filterConfig = null;
}
}
and this is the mapping:
<filter>
<filter-name>Response Buffer Filter</filter-name>
<filter-class>test.example.filter.ResponseBufferFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Response Buffer Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<!-- provide the class causing unwanted behavior -->
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
this will not solve the problem, but at least it should allow log of exception, one way or another, if any.

encoding filter , struts working just when using html:form tag

I face this problem. I have a filter that sets the character encoding of the request according to the filter's config (for example, to UTF-8). This works with forms coded using the struts html:form tag. However, if I use the ordinary HTML form tag, the data are not encoded correctly.
This is the filter definition in the web.xml:
<filter>
<filter-name>Encoding Filter</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Here's the filter :
public class EncodingFilter implements javax.servlet.Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void destroy() {
}
}
If you use a Struts tag <html:form> and omit the METHOD attribute it defaults to POST.
If you use a standard HTML <form> and omit the METHOD attribute it defaults to GET.
Tomcat will process your POST and GET parameters differently:
POST: your filter will be used. Note that you should really only set the request character encoding if it has not been specified by the client (your filter is always setting it to UTF-8). Tomcat comes with a filter SetCharacterEncodingFilter.java that does this.
GET: Tomcat will use ISO-8859-1 as the default character encoding. There are two ways to specify how GET parameters are interpreted:
Set the URIEncoding attribute on the element in server.xml to something specific (e.g. URIEncoding="UTF-8").
Set the useBodyEncodingForURI attribute on the element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.
This is all in: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

Response encoding in Spring

I've got problem with character encoding of my http responses. I read many tips, tutorials etc., but I can't resolve my problem. We are using Spring MVC with Hibernate and ExtJS as view technology. All data are returns as JSON using #ResponseBody on controllers method. Example method:
#RequestMapping(method = RequestMethod.POST, value = "dispatcher")
#ResponseBody
public String dispatcherPost(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
return process(req, resp, session);
}
There is simple dispatching mechanism for dispatching url commands (not really important in this case). Process method is doing something with parameters and return JSON. This JSON contains for example data from database (PostgreSQL 9.1.4). Data in Postgres are stored in UTF-8 and are 'visible correctly' for example in pgAdmin. We can also see valid data (from database) while debuging in eclipse. It all looks like there is everything ok with getting data from the Postgres. Problem starts when we want to return them via #ResponseBody annotated method. Method 'process' returns valid string (I can see in debugging mode) with utf-8 characters but in web browser (chrome, firefox) there are '?' instead of polish characters which are stored in database. Looking into firebug I can see that response headers are partially valid: 'Content-type: text/html;charset=UTF-8'. I told 'partially', because I have this line of code in process method:
`resp.setContentType("application/json;charset=UTF-8");`
where resp is HttpServletResponse. I've tried adding springs bean post processor from this solution: http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody/3617594#3617594 but it doesn't works. I've got also character encoding filter in web.xml
<!-- Force Char Encoding -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
So basically the problem is with returning well encoded JSON from server. Any idea?
EDIT:
I paste code into proccess method:
System.err.println("TEST " + System.getProperty("file.encoding"));
System.err.println("TEST normal: " + response);
System.err.println("TEST cp1250: " + new String(response.getBytes(),"cp1250"));
System.err.println("TEST UTF-8: " + new String(response.getBytes(),"UTF-8"));
And result is something like this:
TEST Cp1250
TEST normal: {"users":[{"login":"userąęśćółżń"}]}
TEST cp1250: {"users":[{"login":"userąęśćółżń"}]}
TEST UTF-8: {"users":[{"login":"user?????"}]}
Thanks, Arek
Ok, it seems to be something weird with http://static.springsource.org/spring/docs/3.0.x/reference/remoting.html#rest-message-conversion and #ResponseBody annotation which is using springs http message converters. I don't have time for go into creating custom message converters, so solution for me is remove #ResponseBody annotation and use something like this:
#RequestMapping(method = RequestMethod.GET, value = "dispatcher")
public void dispatcherGet(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
String response = process(req, resp, session);
try {
resp.getWriter().write(response);
resp.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
Maybe this will helps someone. Thanks, Arek

Turn on gzip compression for grizzly under JerseyTest

I have jersey implementation of web service. The response per requirements must be gzip-ed.
Client side contains following bootstrap code to switch gzip on:
Client retval = Client.create();
retval.addFilter(
new com.sun.jersey.api.client.filter.GZIPContentEncodingFilter());
For Tomcat web.xml gzip is configured as follow
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
</init-param>
And everything works fine!
But I need write unit test that invokes my service. I'm using JerseyTest as base and in practice way it was shown that grizzly is not correctly handles gzip without explicit declaration. I have found code snippet how to switch it on similar problem, but I have no idea how to combine it with JerseyTest.
Thank you in advance
Here is a sample test case if you're using the jersey test Framwork:
#Test
public void testGet(){
WebResource webResource = resource();
ClientResponse result = webResource
.path("pathToResource")
.header("Accept-Encoding", "gzip")
.head();
assertEquals(
"response header must contain gzip encoding",
"[gzip]",
result.getHeaders().get("Content-Encoding").toString());
}
AS the client API changed in the current Jersey versions, this is a sample test which works with Jersey 2.6:
public class WebServicesCompressionTest extends JerseyTest {
#Path("/")
public static class HelloResource {
#GET
public String getHello() {
return "Hello World!";
}
}
#Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
return new ResourceConfig(
HelloResource.class,
EncodingFilter.class,
GZipEncoder.class,
DeflateEncoder.class
);
}
#Test
public void testGzip() {
Response response = target().request().acceptEncoding("gzip").get(Response.class);
assertThat(response.getStatus(), is(200));
assertThat(response.getHeaderString("Content-encoding"), is("gzip"));
}
}

Unicode characters are being saved incorrectly

I have a mysql database with unicode text strings in it. My JSF application (running on tomcat 6) can read these unicode strings out and display them correctly in the browser. All the html charsets are set to UTF-8.
When I save my object, even having made no changes, Hibernate persists it back to the database. If I look directly in the database now, I see that characters with accents have become junk.
My database connection string is:
<property name="hibernate.connection.url"
value="jdbc:mysql://database.address.com/dbname?autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8"/>
Changing that characterEncoding to UTF-8 instead of utf8 makes no difference.
Actually it was working days ago for sure, but now it isn't and I'm out of ideas for what to check. Do you have any suggestions? I can provide any further info deemed relevant.
Does it only happen on certain tables? Perhaps the DEFAULT CHARSET of those tables is different than the others. (http://dev.mysql.com/doc/refman/5.1/en/charset-table.html)
Some options to resolve that come to my mind:
if using facelets, specify <?xml version="1.0" encoding="UTF-8"?> at the top of the page
if using JSP, specify <%# page pageEncoding="utf-8" %>
if that does not work, make a filter that is mapped to the faces servlet, and does request.setCharacterEncoding("utf-8")
I had the same problem as you but with PostgreSQL. You can use filter as below. It worked for me. I think that it must be better solution for that, but i haven't found yet :/
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class UnicodeFilter implements Filter {
#Override
public void destroy() {}
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// Set the characterencoding for the request and response streams.
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html; charset=UTF-8");
chain.doFilter(req, res);
}
#Override
public void init(FilterConfig arg0) throws ServletException {}
}
In web.xml:
<filter>
<filter-name>utffilter</filter-name>
<filter-class>utils.UnicodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>utffilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In Spring a simplest solution i've found is CharacterEncodingFilter.
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Ensure UTF-8 encoded pages so that certain characters are displayed
and submitted correctly...
Add this filter to web.xml.
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>