Hi,
I am having in my
servlets. As a standalone program i am able to replacing '&' with "&" and " ' " with "'" but as i do the same thing in servlets, i am getting a different output .
Actually what the servelt does is it parses a blank XML document and then populates it the HTML form values and using the Transformer class converting the XML file into a
String and storing in the database. What i have to check that if there is an occurence of "'" or "&" then that to be converted to "'" or "&". I am having a filter class which should replace any such charaters . and then return the String value back to servlets.
---------------------------------------------------------
In HTML (ie in an HTTPServlet) the character '&' will be changed to '&'
---------------------------------------------------------
The following is the piece of code in (servlet file)
--------------------------------------------------
String QReplace = "'"; String QSearch= "'";
domainname=req.getParameter("domainname");
RPFilter filter = new RPFilter(); // name of the class
firstNameT = filter.newReplace(firstName, QSearch, QReplace);
//here above i am passing a String variable got from the form i.e firstName.
CASE1
If i am giving "John&Doe" the result i am getting is "John&Doe"
i.e John&(semicolon)ampDoe.// I have typed in semicolon as it not been shown on the browser.
CASE2
If i am giving "John'Doe" the result i am getting is "John&aposDoe".
I am giving another String variable "lastName" as "John&Doe"
and not filtering it, still it automatically converts "&" to "&"
The following is the code in RPFilter
-------------------------------------
public static String newReplace(String sOriginal, String sSearch, String sReplace){
int iPos = sOriginal.indexOf(sSearch);
if(iPos==-1){
return sOriginal;
}else{
StringBuffer sb = new StringBuffer();
sb.append(sOriginal.substring(0,iPos));
sb.append(sReplace);
sb.append(sOriginal.substring(iPos+1));
return sb.toString();
//return modified string
}
}
Question?:Is there any way to avoid this conversion of & to & automatically and then adding 'apos' to it?
Kindly let me know if there is a better way out to check the occurences of these charaters and how to convert it so that it does not give and XML error as well?
Thanks,
Regards,
Lijoy