Problem with enctype="multipart/form-data" and UTF-8 combination in struts

We are facing an issue when we combine the both features (enctype=“multipart/form-data” and UTF-8) in one JSP page. The application is developed with MySQL database. We are executing a command “SET NAMES ‘utf8’” at MySQL end to enable UTF-8 feature.

Usage of feature enctype=“multipart/form-data”: - fileUpload
Usage of feature UTF-8: - Multi-language support

The above features are working perfectly individually, but failed when we try to combine them. Please let me know if anybody have ideas to handle this issue in a smooth way. Thanks in advance.

I have added the following configurable lines in MySQL’s my.ini file.

default-collation=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
default-character-set=utf8

But, JSP page is not displaying the special characters perfectly.

this is very interesting problem. I’m afraid I don’t have any clue on this. Perhaps, it is a known bug in JSP. Try to upgrade to latest web server you’re using and see if that works.

I am using Tomcat 5.0 for my application.

try using latest struts or tomcat

try using <%@ page contentType=“text/html; charset=UTF-8” %> in your JSP

Previously, my application is working fine with UTF-8 feature with the following settings.

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

and

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

Newly, I have added the fileUpload feature to the same JSP pages. Now, JSP page is not displaying the content (Japanese characters) correctly.

I’ve implemented the filter and it works nice. We can use a request filter instead of updating the code in each and every JSP and SERVLET.

CharacterEncodingFilter.java


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;

import java.io.IOException;

public class CharacterEncodingFilter implements Filter
{
	
	private FilterConfig fc;
	
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
	{
		
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF8");
		
		chain.doFilter(request, response);        //do it again, since JSPs will set it to the default
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF8");
	}
	
	public void init(FilterConfig filterConfig)
	{
		
		this.fc = filterConfig;
	}
	
	public void destroy()
	{
		
		this.fc = null;
	}
}

web.xml

	
<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>com.its.struts.action.CharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <servlet-name>action</servlet-name>
</filter-mapping>

<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>*.html</url-pattern>
</filter-mapping>