我在 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);