Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems encoding form text to Servlet

843842Mar 30 2009 — edited Mar 31 2009
I have a form with a simple textarea for input. I am using Ajax to send the contents of this textarea to a servlet. I want the servlet to decode this text and store it. My problem is that this textfield may contain anything (include &, etc.).

What I am currently doing is calling javascript encodeURIComponent on the text, and sending it to servlet. I was then trying to call URLDecoder.decode(text, "UTF-8") on the text, but I am getting a error:
java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "^&"
Some simple code I created to display the problem is below. It includes index.html which has a simple form that attempts to encode the text in the textfield and pass it to test.jsp.
test.jsp tried to decode the text and display it again in the textfield. A successful result would display the exact same text in test.jsp that was entered in index.html.

index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <script type="text/javascript">
    	function sendText() {
    		var text = document.getElementById("text").value;
    		text = encodeURIComponent(text);
    		document.location="test.jsp?test=" + text;
    	}
    </script>

  </head>
  
  <body>
    Text: <textarea id="text" cols="50" rows="5"></textarea><br>
    <input type="button" value="Test" onclick="sendText();"/>
  </body>
</html>
test.jsp
<%@ page language="java" import="java.util.*,java.net.*" pageEncoding="ISO-8859-1"%>
<%
String test = request.getParameter("test");
if (test != null)
	test = URLDecoder.decode(test, "UTF-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<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">
  </head>
  
  <body>
  Text: <textarea cols="50" rows="5"><%=test %></textarea>
  </body>
</html>
The test string that I am using is:
Testing of Dates, for P=x; C=y) & Testing of long text truncation issue from first reporting to portal,`~!@#$%^&*()_+-=;’:”,./<>?-_1234567890[]\{}| character test.
The results of javascript encodeURIComponent on this text is:
Testing%20of%20Dates%2C%20for%20P%3Dx%3B%20C%3Dy)%20%26%20Testing%20of%20long%20text%20truncation%20issue%20from%20first%20reporting%20to%20portal%2C%60~!%40%23%24%25%5E%26*()_%2B-%3D%3B%E2%80%99%3A%E2%80%9D%2C.%2F%3C%3E%3F-_1234567890%5B%5D%5C%7B%7D%7C%20character%20test.
So in effect I am looking for a way to unescape/decode this text back into the original form.

Any help would be appreciated.

-Tom
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 28 2009
Added on Mar 30 2009
2 comments
271 views