• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

charset and pageEncoding in jsp  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using "<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>" in my jsp.

I have a drop down box in my jsp that contains few special characters also like (Ixcán,Heliópolis,Orobó).
Because of these special characters, my jsp is giving error.

Please let me know what exactly I should use in charset and pageEncoding.
I have tried "<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>" also.
But its also didn't work.

Thanks
 
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

my jsp is giving error



details?
 
Geet Chaudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

olivier dutranoit wrote:

my jsp is giving error



details?



----------------

public ActionForward fetchListOfDevelopmentAreas(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {

String regionId = (String) request.getParameter("region");
ArrayList listOfRegions = new ArrayList();
StringTokenizer strTok = new StringTokenizer(regionId, ",");
while (strTok.hasMoreTokens()) {
listOfRegions.add(strTok.nextToken());
}

HashMap tempDAHM = SupporterReportDBOperator.fetchListOfDAForRegion(listOfRegions);
// here tempDAHM contains data : "Heliópolis,Ixcán" (Special character data)
// fetchListOfDAForRegion(listOfRegions) fucntion is working fine , its just getting data from the DB.

// Here when I am printing tempDAHM 's value on console, its converting Heliópolis to Heli≤polis.So I cant even check for special symbol 'ó'.

formResponseForAJAX(response, tempDAHM);

return null;
}


--------------------------------------------------------------------------------------------
public void formResponseForAJAX(HttpServletResponse response, HashMap hmArg) {

response.setContentType("text/xml");
response.setHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);

Map temphmArg = sortByValue(hmArg);
StringBuffer sb = new StringBuffer();
Set tempEntrySet = temphmArg.entrySet();
Iterator itr = tempEntrySet.iterator();
Map.Entry me = null;

if (temphmArg != null) {
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<List>");
sb.append("<valid>true</valid>");
String key = null;
String value = null;
for (int i = 0; i < hmArg.size(); i++) {
me = (Map.Entry) itr.next();
key = me.getKey().toString();
value = me.getValue().toString();
sb.append("<Data>");
sb.append("<Id>" + key + "</Id>");
sb.append("<Name>" + value + "</Name >");
sb.append("</Data>");

}
sb.append("</List>");
try {
response.getWriter().write(sb.toString());
} catch (IOException e) {
// return mapping.findForward("errorPage");
}
} else {
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<List>");
sb.append("<valid>false</valid>");
sb.append("</List>");
try {
response.getWriter().write(sb.toString());

} catch (IOException e) {

}

}

}

Below is the the jsp code where I will be calling the above java code:

function populateDA() {

regionList = "";
var doc = document.forms[0];
var count = 0;
emptyMultiSelectDropDown(document.getElementById("developmentArea"));

for (var i=0; i<doc.region.length;i++)
{
if (doc.region[i].selected)
{
count++;
if(regionList.length==0)
{
regionList+= doc.region[i].value;
}else {
regionList+= ','+doc.region[i].value;
}
}
}

if((doc.region.selectedIndex==0) && (count >1)){
alert("Please select either All or other values");
return false;
}

if(doc.region.selectedIndex == 0){
return false;
}

var url = "/supporterReports.do?methodName=fetchListOfDevelopmentAreas®ion="
+ regionList;

if (window.XMLHttpRequest) {

testReq = new XMLHttpRequest();
} else if (window.ActiveXObject) {

testReq = new ActiveXObject("Microsoft.XMLHTTP");
}

testReq.open("GET", url, true);

// This line of code -will open & create the request to be sent to the action method
testReq.onreadystatechange = handleHttpResponseDA;

testReq.send(null);
}

//ajax code to fetch DA list
function handleHttpResponseDA() {

if (testReq.readyState == 4) // when the request is ready
{
var xmlDoc = testReq.responseXML;
var validate = xmlDoc.getElementsByTagName("List");

try {
if (validate[0].childNodes[0].childNodes[0].nodeValue == "true") {
var selectBox = document.getElementById("developmentArea");

var selectedIndexValue=selectBox.value;

if (selectBox.options.length > 0) {
for ( var i = selectBox.options.length - 1; i > 0; i--) {
selectBox.remove(i);
}
}

for ( var i = 1; i < validate[0].childNodes.length; i++) {
var Option = document.createElement("OPTION");
Option.text = validate[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
Option.value = validate[0].childNodes[i].childNodes[0].childNodes[0].nodeValue;
selectBox.options.add(Option);
}

selectBox.value = selectedIndexValue ;

} else {
alert("There seems to be an error fetching. Please try again1.");
}
} catch (err) {
alert("There seems to be an error fetching. Please try again2.");
}
}
}


On execution I am getting alert "There seems to be an error fetching. Please try again2" as an error message.

This happens only when I have special characters in the data.
 
olivier dutranoit
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

// Here when I am printing tempDAHM 's value on console, its converting Heliópolis to Heli≤polis.So I cant even check for special symbol 'ó'.



The console of your system is not the best way to check encoding of strings.
It depends a lot of the server your are developing on, or the ide etc etc...
So don't rely on that.


what happens if you change the

response.setContentType("text/xml");



to

response.setContentType("text/xml;charset=UTF-8");




what happens?

You also do a lot of javascript there...
try to use a javascrpit framework that does the job for you...like jQuery.




 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!