HTML Form posting contains weird characters when using single quote, double quote or dash

Posted on 2007-10-06
Last Modified: 2013-12-02
I am using the following FORM in my webpage.
<Form name=addreview method=POST action=savereview.jsp>
Title (less than 50 characters) <Input type=text size=50 name=title><Input type=submit value="Upload Review"><BR>
Review (less than 3500 characters)<BR><TextArea rows=35 cols=100 name=description> </TEXTAREA><BR>

if I place a single quote (') , double quote (") or dash/hyphen (-) in the textarea control and click submit I get funny characters being sent to my Tomcat 5.5 server. If I have the following script in a .jsp page I get the following results.

System.out.println (request.getParameter ("description").trim());

gives me:

Hello, I?m Sharron, the last of the truly ?sick romantics? ? or so some of my be
st friends have dubbed me ? and frankly they?re absolutely right!  But I?m also
passionate about food so think about that combination ? food and romance ? orgasmic!  With this in mind, I?m on a constant quest to find a spot to dine in the very best of romantic settings with the very best of cuisine!

As you can see the apostrophe's are replace with something. Is there a way to get exactly what was entered?
Question by:anthony0007
    LVL 8

    Expert Comment

    Did you happen to paste this text from a word processor?  If so, the text may not be exactly what you think it is.

    Many modern word processors replace the generic apostrophe and quotation marks with their Unicode equivalents which are facing either left or right.  Similarly, they'll replace hyphens with Unicode dashes.  Your system's console may not be able to display Unicode characters, which is why it prints a ? instead.

    Luckily, Java's Strings use Unicode under the hood (the char primitive is Unicode), so it should be simple enough to replace the offending characters in this string with their good ol' ASCII equivalents.  The following link has the information you'll need to write a method to perform this task.

    If you need some help writing this method, please feel free to ask.
    LVL 17

    Expert Comment

    Why dont you replace them with relavant html entities? like replace " to &quot; etc etc
    LVL 8

    Accepted Solution

    To clarify my answer, I believe the form is submitting the characters correctly, but the console is displaying them incorrectly (as question marks).

    Try replacing the Unicode quotation characters with ASCII ones by using this method:

    private static String convertToASCII(String str) {
        char single = '\'';
        char double = '"';
        char dash = '-';
        return str.replace((char) 0x2018, single)
                .replace((char) 0x2019, single)
                .replace((char) 0x201c, double)
                .replace((char) 0x201d, double)
                .replace((char) 0x2010, dash)
                .replace((char) 0x2011, dash)
                .replace((char) 0x2012, dash)
                .replace((char) 0x2013, dash)
                .replace((char) 0x2014, dash)
                .replace((char) 0x2015, dash)
                .replace((char) 0x2212, dash);

    LVL 8

    Expert Comment

    The second answer I gave (#20050807) is more complete.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now