• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Problem with UTF-8 Character encoding

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.





 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use Code Tags for code,
Code tag
So many people can unserstand code easily.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic