问题
I have a problem sending special characters like cyrillic or umlauts from a jsp to a servlet. I would greatly appreciate your help here.
Here is what I have done:
Defined the utf-8 charset in the jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> ... <div class="authentication"> <form name="registerForm" action="register" method="get" accept-charset="UTF-8"> <input type="input" name="newUser" size="17"/> <input type="submit" value="senden" /> </form> </div> ...Set Tomcat URIEncoding for Connector, in the server.xml
<Connector URIEncoding="UTF-8" ...Inside the servlet - set the CharacterEncoding to UTF and read request
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ request.setCharacterEncoding("UTF-8"); String username = request.getParameter("newUser"); System.out.println("encoding: "+request.getCharacterEncoding()); System.out.println("received: "+username);Here is what gets displayed when using for example: Однако
encoding: UTF-8 received: ??????
Am I missing something ? I suppose the servlet can not decode the String properly, but I have no idea why is this happening. I've followed all the advises on this topic but had no luck.
thanks in advance.
回答1:
Everything looks fine. Only the System.out.println() console also needs to be configured to interpret the byte stream as UTF-8.
If you're sitting in an IDE like Eclipse, then you can do that by setting Window > Preferences > General > Workspace > Text File Encoding to UTF-8. For other environments, you should be more specific about that so that we can tell how to configure it.
来源:https://stackoverflow.com/questions/7539590/can-not-send-special-characters-utf-8-from-jsp-to-servlet-question-marks-disp