0

我在 apache-tomcat 上部署了一个使用 UTF8 编码的 Web 应用程序。使用诸如“ąęćńźżół”之类的波兰字符在 servlet 参数中发送数据不是问题。他们被解释得很好。但是在将字符串发送回 CodenameOne 时,我得到的只是 Ä?一个??? ?? 一个????。

小服务程序代码:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String ID;
String answer = "no answer";      
ID = request.getParameter("Id");
answer="ą ę ż ź ć ó";
//this is an answer to the client – Codename one          
out.print(answer);                        
} finally {           
out.close();
}
}

代号一个代码:

response = "empty";
try {           
//the NetworkManager object
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.start();
networkManager.addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
    NetworkEvent n = (NetworkEvent) evt;
    n.getError().printStackTrace();                        
}});
//ConnectionRequest object   
ConnectionRequest request = new ConnectionRequest() {
int chr;
StringBuffer sb = new StringBuffer();                   
protected void readResponse(InputStream input) throws IOException {
//reading the answer                      
while ((chr = input.read()) != -1){
sb.append((char) chr);
}
response = sb.toString();                                           
response = response.trim();
}  
protected void handleException(Exception err) {
//An error occured - show a message:
Dialog.show("Yikes!!", "Are you connected to the internet? Check your connection", "Ok",  null);
}
};

request.setUrl("http://localhost:8080/FirstOneTomcat/Response"); //servlet calling
request.setPost(false);
request.addArgument("ID","ńćżźóąę"); //sending a the parameter Id to the servlet                                  
} catch (Exception e) {
System.out.println(e.getMessage());
}                
while(response.equals("empty")) {
//waiting for the answer from the serlvet or jsp server
System.out.println("No response from server");
}
//set the label with the information from the server
findLabelKom().setText(response);
4

1 回答 1

0

您将波兰语嵌入到服务器源代码中,这意味着编译器需要选择编码并且它可以做任何事情。使用任一native2ascii工具将您的波兰语转换为 \u 表示法(注意您没有输入 UTF 而是输入波兰语编码)。

或者您可以在编译源代码时配置您的 IDE/编译器编码。

于 2013-08-05T04:11:06.700 回答