1

I read a lot of similar question in the WEB but non of the solutions helped in my case. I implement web page with search, when the query can be Hebrew, English or any other language. When the user click "search" I use JavaScript to get the value in the field and show the result using Ajax. When user click "search" the JavaScript function sends a new request to my JSP file, when the query sent as a parameter.

Do you have any idea how to fix it?

The relevant lines in JavaScript:

...  
request = new XMLHttpRequest();  
var url = "SearchResults.jsp?userID=" + userID + "&query=" + encodeURIComponent(query);  
alert("The query = " + query);  
alert("The url = " + url);   
request.open("GET", url, true);  
... 

The first alert shows the Hebrew correctly, the second alert show the url when the query parameter encoded with %%.. when I paste it into the Google search it writes the word in Hebrew correctly

The relevant lines in the server:

...  
String userID = request.getParameter("userID");  
FbLogger.fbLogger.info(request.getParameter("query"));  
String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");  
FbLogger.fbLogger.info("Entered as Manager with Params: userID = " + userID + " , query = " + query);       
...  

I see the query as ×?שר×?×? in my log for the 2 messages above, the userID is passed correctly.

On each page I added:

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
<meta http-equiv="Content-Type" content="text/html; charset=charset=UTF-8">  
<script language="javascript" type="text/javascript" charset="utf-8" src="javascript/friendsSearch.js"></script> 

I also changed the configuration in the Tomcat server (server.xml):

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

1 Answer 1

1

Just request.getParameter("query") is enough with the connector attribute. If it's not working, then the URIEncoding="UTF-8" in your <Connector> tag is not taking any effect.

Note that you are adding extra step of complexity by logging the string directly, the logger output encoding and the log reader also needs encoding/decoding step. Instead search for ל and log the result "ל".equals(request.getParameter("query")) which will be true or false and only the URIEncoding needs to be considered.

Try applying port="1337" to the same <Connector> tag and see if your server works from localhost:1337. If it does work from :1337, try to search in Hebrew again.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I solved the problem by String q = new String(request.getParameter("query").getBytes("iso-8859-1"),"UTF-8");, I guess the connector did not work. I also did not encode/decode before logging, so your suggestion (to use equals) helped!
@IrenaZuyev yes that is another option, but do you really want to write that line for every parameter containing text?
No, my next step is to understand why the connector does not work, by changing the port as you suggested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.