问题描述
我有一个问题,发送特殊字符,如西里尔字母或umlauts从一个jsp到一个servlet。
我非常感谢您在这里的帮助。
这是我所做的:
-
在jsp中定义utf-8字符集:
<%@ page language = javacontentType =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-typecontent =text / html; charset = utf-8/>
...
< div class =authentication>
< form name =registerFormaction =registermethod =getaccept-charset =UTF-8>
< input type =inputname =newUsersize =17/>
< input type =submitvalue =senden/>
< / form>
< / div>
...
-
在server.xml中设置Tomcat URIEncoding
< Connector URIEncoding =UTF-8...
pre>
-
在servlet中设置CharacterEncoding为UTF并读取请求
code> 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);
-
以下是使用时显示的内容:Однако
编码:UTF-8
received:
我想servlet不能正确解码字符串,但我不知道为什么这是发生。我已遵循所有关于这个主题的建议,但没有运气。
提前感谢。
一切都很好。只有 System.out.println()
控制台还需要配置为将字节流解释为UTF-8。
如果您坐在像Eclipse这样的IDE中,则可以通过将窗口>首选项>常规>工作区>文本文件编码设置为UTF-8来实现。对于其他环境,您应该更具体,以便我们可以告诉如何配置它。
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.
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.
这篇关于无法将特殊字符(UTF-8)从JSP发送到Servlet:显示问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!