This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with UTF-8 Character encoding " Watch "Problem with UTF-8 Character encoding " New topic
Author

Problem with UTF-8 Character encoding

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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 server.in the ajax call i have made use of the following to set the character encoding to UTF-8

encodeURIComponent(document.getElementById("fname").value)
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 java.io.IOException;
import java.io.PrintWriter;

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 ***********");
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setCharacterEncoding("UTF-8");
HttpServletRequest req=(HttpServletRequest)servletRequest;
System.out.println(req.getParameter("firstName"));
// 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

AddContact.jsp
-------------------------
--------------------------

<%@ 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">
<html>
<head>
<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()
{
try
{
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
//alert("Created XMLHttpRequest Object");
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser doesnot support the AJAX");
}
}
}
}

function frmSub(){
ajaxFunction();
pth='<%=request.getContextPath()%>';
alert("path is ");
var str=pth+"/addcontact.do?firstName=";
alert(document.getElementById("fname").value)
str+=encodeURIComponent(document.getElementById("fname").value);
alert("Final Path Is :" + str);
xmlHttp.open("post",str,true);
xmlHttp.onreadystatechange = hndlRespAddCont;
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
xmlHttp.send(null);
}
function hndlRespAddCont()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status == 200)
{
alert("handlerResponseCreate gets Called **********");
}
}
}

function frmTest(){
document.AddContactForm.submit();
}
</script>
</head>
<body>
<html:form action="/addcontact.do" acceptCharset="UTF-8" enctype="multipart/form-data">
First Name :<html:text property="firstName" styleId="fname" />

<input type="button" value="Login" onclick="frmSub()"/>
</html:form>
</body>
</html>


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.


Thanks,
Shailesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with UTF-8 Character encoding
 
Similar Threads
Unicode convert problem with Jboss in Linux
JavaScript not working in Netscape
transfering Euro (?) character
jsp with javascript
passing arabic parameters