0

I am using the below two lines in my servlet to display the arabic URL parameter

response.setContentType("text/html; charset=UTF-8");

String s = new String(request.getParameter("p").getBytes("8859_1"), "UTF-8");

Note that in the above code if I am passing the parameter p as arabic character for example:

http://localhost/sample/MyServlet?p=عربي

then its displays the return as as ?????? characters

Any suggestion will be appreciated

  • Please edit your question to show an example of what is returned by `request.getParameter("p")`. – VGR Apr 02 '15 at 23:33

2 Answers2

0

Since parameter "p" is arabic character you shouldn't probably read it as 8859_1 encoding character since it doesn't correspond to arabic characters - http://en.wikipedia.org/wiki/ISO/IEC_8859-1 Try to use UTF-8 encoding for getting bytes.

Stan
  • 1,410
  • 10
  • 14
  • I tried with several combinations: `String s = new String(request.getParameter("p").getBytes("8859_1"), "cp1256");` and `String s = new String(request.getParameter("p").getBytes("8859_1"), "UTF-8");` but the problem still there @Stan – user3801082 Apr 03 '15 at 11:12
  • In both examples you are using 8859_1 as parameter to getBytes(). Did you try UTF-8 there? Also where do you see that symbol displays wrong? Is it on jsp or you are checking in debug mode? – Stan Apr 03 '15 at 11:20
  • Yes I tried with UTF-8 and the result was the same, I am using the following code `PrintWriter pw = response.getWriter(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String s = new String(request.getParameter("p").getBytes("8859_1"), "UTF-8"); pw.println(s); pw.close(); ` @Stan – user3801082 Apr 03 '15 at 11:26
  • Browsers (usually) send Unicode characters in URLs as UTF-8 encoded (as per IRI). Servlet containers, unless otherwise configured, mis-decode these as ISO-8859-1 because the Servlet specification is outmoded rubbish and says they must. `new String(x.getBytes("ISO-8859-1"), "UTF-8")` is a standard idiom to un-mangle input that has been mis-decoded in this way, for cases where the container can't easily be reconfigured. – bobince Apr 03 '15 at 13:34
  • I tried and finally it works for me if operating just with Strings. String s = request.getParameter("p"); response.getOutputStream().print(s); – Stan Apr 03 '15 at 16:05
  • I tried with just simple string as you said but not worked for me... do we need to configure any environmental settings for java or tomcat? @Stan – user3801082 Apr 03 '15 at 17:23
  • I tried mis-decode these as ISO-8859-1 `String s = new String(request.getParameter("p").getBytes("ISO-8859-1"), "UTF-8");` but sill the same for me.. @bobince – user3801082 Apr 03 '15 at 17:24
  • I used default Tomcat settings, but you can check http://wiki.apache.org/tomcat/FAQ/CharacterEncoding for encoding configuration. Maybe you need something like "Set the URIEncoding attribute on the element in server.xml to something specific (e.g. URIEncoding="UTF-8")." – Stan Apr 03 '15 at 20:20
  • I configured the URL encoding in Tomcat by adding to the server.xml file `URIEncoding="UTF-8"` in the active element, but the issue is not resolved @Stan – user3801082 Apr 03 '15 at 20:40
  • Think I'm repeating but please try to copy/paste following method and test. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String s = request.getParameter("p"); response.getOutputStream().print(s); } If not working is there any chance you can share your tomcat? – Stan Apr 03 '15 at 21:01
  • I was able o solve this by using different approach `String[] s = URLDecoder.decode(request.getQueryString(), "UTF-8").split("&"); ` as shown [here](http://stackoverflow.com/a/2553995/3801082) @Stan – user3801082 Apr 03 '15 at 21:24
  • Looks like workaround a bit but glad it works for you! – Stan Apr 03 '15 at 21:33
0

in Tomcat server.xml in conf folder, edit and put encoding like this

URIEncoding="UTF-8"

<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->

<Connector URIEncoding="UTF-8" connectionTimeout="20000" maxPostSize="67589953" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>

and if you are using https

do same

<!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
<Connector URIEncoding="UTF-8" SSLEnabled="true" clientAuth="false"  ...
shareef
  • 9,255
  • 13
  • 58
  • 89