Passing Data to the Server Using Ajax, Converting it to HTML Step 48
Now that you have the data in your Ajax function, you will want to pass the data to the server to add the object to the database. You would first add the items to your URL like this.
url=url+"&book="+book+"&author="+author+"&publisher="+publisher+"&category="+category
You have to make sure the user doesn't input a & sign because you can't pass the & within the url. You will need to change it to the ascii code.
Here is a good page that explains all of the ascii codes
http://www.ascii.cl/htmlcodes.htm
You will also need to make sure you display the ascii codes onto the html page. For example, if you pass a < or a > sign to the html page, the html page will
think that it is an html code and will render it as html code. If you pass the ascii code to the page which is < or >, then you will actually see the symbol < or > appear on the page.
You can do this by using the replace function. For example, you can say book.replace("<","<") when you display the items on the page in javascript. You can use the replace function when you pass the data to the server using Javascript, but when you are retrieving data from the server, you might want your Java code to have a html method to convert your data to html.
Here is an example:
public static String convertToHtml(String inputStr) {
if(inputStr==null)
{
return "";
}
String newString=inputStr;
newString=newString.replaceAll("&", "&");
newString=newString.replaceAll("\r\n"," ");
newString=newString.replaceAll("'", "'");
newString=newString.replaceAll("<","<");
newString=newString.replaceAll("/","");
newString=newString.replaceAll(">",">");
newString=newString.replaceAll("@","and");
newString=newString.replaceAll("\"","");
return newString;
}
Before sending your String Buffer back to the JSP page, you call this method to convert the string to html.
Go To Step 49

0 comments:
Post a Comment