Meaningless Drivel is fun!
The moose likes Struts and the fly likes Problem with UTF-8 Character encoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with UTF-8 Character encoding " Watch "Problem with UTF-8 Character encoding " New topic

Problem with UTF-8 Character encoding

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 51
Hi all

i am using struts-1.2.9 and Jboss4.0.4 GA and mysql 5.1 as a database.My Question is regarding the character encoding in UTF-8.

I am making the ajax call to send the data to the the ajax call i have made use of the following to set the character encoding to UTF-8

xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

but when i fetch the data on the server side

if i have enetered data in the first name field as šašal?lyj?? i would get the data Å¡aÅ¡al?lyj?? on the server side.

i don't know why it is changing the characters ???

i have also made changes to set the character Encoding to UTF -8

i have attached the source code of my files below to made you understand my problem.

1.setting the content type in the JSP page
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

2. I am also placed the filter that will set the character encoding of the request and response to UTF-8.Code of the Fileter SetCharSet Filter is below attahced

package com;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetCharsetFilter implements Filter{

private FilterConfig config;
public SetCharsetFilter()
config = null;

public void init(FilterConfig config)
throws ServletException
this.config = config;

public void destroy()
config = null;

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException
System.out.println("Setting the Character Set ***********");
HttpServletRequest req=(HttpServletRequest)servletRequest;
// Set the content type in the header of the response servletResponse.setContentType("text/html;charset=UTF-8");
chain.doFilter(servletRequest, servletResponse);
System.out.println("Ending the Character Set ***********");



i am also attaching the JSP file on which i have placed one firstName field


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache" >
<meta http-equiv="expires" content="0" >
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript">

function ajaxFunction()
xmlHttp=new XMLHttpRequest();
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
//alert("Created XMLHttpRequest Object");
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
alert("Your browser doesnot support the AJAX");

function frmSub(){
alert("path is ");
var str=pth+"/";
alert("Final Path Is :" + str);"post",str,true);
xmlHttp.onreadystatechange = hndlRespAddCont;
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
function hndlRespAddCont()
if(xmlHttp.status == 200)
alert("handlerResponseCreate gets Called **********");

function frmTest(){
<html:form action="/" acceptCharset="UTF-8" enctype="multipart/form-data">
First Name :<html:text property="firstName" styleId="fname" />

<input type="button" value="Login" onclick="frmSub()"/>

in the above code if on the button Login's click event when i call the function frmTest() it works fine i get the correct data on the server side.But when i am sending the data using the ajax using the function frmSub() then i will get the different data.

Please help me .

Any Help would be appreciated.

Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Please use Code Tags for code,
Code tag
So many people can unserstand code easily.

I agree. Here's the link:
subject: Problem with UTF-8 Character encoding
It's not a secret anymore!