wood burning stoves 2.0*
The moose likes Java in General and the fly likes  URL Encoding Decoding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark " URL Encoding Decoding problem" Watch " URL Encoding Decoding problem" New topic
Author

URL Encoding Decoding problem

Thomas Greene
Ranch Hand

Joined: Aug 09, 2004
Posts: 126
In my JSP, I am using java.net.URLEncoder to encode some parameters and then on the other page I am using
java.net.URLDecoder to decode the same parameters. When encoded some special characters are converted to %xyz, but when the original parameters themselves contains % then I am getting an exception.

Caused by: java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "2k"
at java.net.URLDecoder.decode(URLDecoder.java:168)
at jsp_servlet._view._search.__changesearch._jspService(__changesearch.java:627)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:301)
at com.wk.steuernetz.controller.ServletController.forward(ServletController.java:303)
at com.wk.steuernetz.controller.ServletController.allocateAction(ServletController.java:231)
... 28 more
Caused by:
java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "2k"
at java.net.URLDecoder.decode(URLDecoder.java:168)
at jsp_servlet._view._search.__changesearch._jspService(__changesearch.java:627)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:301)
at com.wk.steuernetz.controller.ServletController.forward(ServletController.java:303)
at com.wk.steuernetz.controller.ServletController.allocateAction(ServletController.java:231)
at com.wk.steuernetz.controller.ServletController.doPost(ServletController.java:175)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:301)
at jsp_servlet._view._search.__changesearchsuccess._jspService(__changesearchsuccess.java:490)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:301)
at com.wk.steuernetz.controller.ServletController.forward(ServletController.java:303)
at com.wk.steuernetz.controller.ServletController.allocateAction(ServletController.java:231)
at com.wk.steuernetz.controller.ServletController.doPost(ServletController.java:175)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)


Please let me know how to solve this problem.
Thomas Greene
Ranch Hand

Joined: Aug 09, 2004
Posts: 126
I want to save a value say, %2k among some other values. I enter this value in a field in say, page1. Then I press submit and now teh form submits ot page 2. Suppose one of the other fields contain some syntax error. So as per out requirement I hve to come back to page 1 and prefill the values entered by the user in the respective text fields. Now In page 2 i am doing



and requestdispatching to page 1 with search name as a URL parameter. In page 1 to get the search name from URL I do



This throws an exception shown above.
Please help me resolve this
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I can't reproduce the error:


[My Blog]
All roads lead to JavaRanch
Thomas Greene
Ranch Hand

Joined: Aug 09, 2004
Posts: 126
Thanks for replying. This is the exact scenario

Control comes to page1 from page x, where is URL passed contains an encoded value of searchname. Soemthing like this in page x

<a href="#" onklick="javascript:submitChange('<%= java.net.URLEncoder.encode(sName, "UTF8") %></a>

In page 1 I am doing

String searchName = request.getParameter("searchName");
Logger.getInstance().writeLog("Search name BEFORE decoding ---"+request.getParameter("searchName")); //%252k
if(searchName != null) {
searchName = URLDecoder.decode(searchName,"UTF-8");
Logger.getInstance().writeLog("Search name AFTER decoding ---"+searchName); //%2k
searchName = searchName.replaceAll("&","&");
searchName = searchName.replaceAll("<", "<");
searchName = searchName.replaceAll(">", ">");
searchName = searchName.replaceAll(""", "\"");
Logger.getInstance().writeLog("Search name AFTER replcasing form input elements ---"+searchName); //%2k
}

Then this page (page 1) is further submitted to page 2, Where searchname is passed as

<input type="hidden" name="searchName" value="<%= java.net.URLEncoder.encode(searchName, "UTF8") %>" />

In page 2, first this code is excetuted


searchName = request.getParameter("searchName");
if(searchName != null) {
searchName = java.net.URLDecoder.decode(searchName,"UTF-8");
Logger.getInstance().writeLog("Search name AFTER decoding ---"+searchName); //%2k
searchName = searchName.replaceAll("&","&");
searchName = searchName.replaceAll("<", "<");
searchName = searchName.replaceAll(">", ">");
searchName = searchName.replaceAll(""", "\"");
Logger.getInstance().writeLog("Search name AFTER replacing form input elements ---"+searchName); //%2k
}

Then after some processing it is found taht there was an error is some other form field. So this page is requestdispatched back to page1. Before doing that I encode the searchanme again

searchName = searchName.replaceAll("&","&");
searchName = searchName.replaceAll("<", "<");
searchName = searchName.replaceAll(">", ">");
searchName = searchName.replaceAll("\"", """);
Logger.getInstance().writeLog("Search name AFTER replacing (ERRORS) changeSearchSuccess.jsp ---"+searchName); //%2k

searchName = java.net.URLEncoder.encode(searchName,"UTF-8");
Logger.getInstance().writeLog("Search name AFTER encoding (ERRORS) changeSearchSuccess.jsp ---"+searchName); //%252k

RequestDispatcher rd =
request.getRequestDispatcher("/controller?action=page1&searchName="+searchName+"&some other params);


Then back in page 1, I am doing (as posted above)

String searchName = request.getParameter("searchName");
Logger.getInstance().writeLog("Search name BEFORE decoding ---"+request.getParameter("searchName")); //%2k
if(searchName != null) {
searchName = URLDecoder.decode(searchName,"UTF-8"); //gives error
Logger.getInstance().writeLog("Search name AFTER decoding ---"+searchName);
searchName = searchName.replaceAll("&","&");
searchName = searchName.replaceAll("<", "<");
searchName = searchName.replaceAll(">", ">");
searchName = searchName.replaceAll(""", "\"");
Logger.getInstance().writeLog("Search name AFTER replcasing form input elements ---"+searchName);
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URL Encoding Decoding problem