Servlet response.sendRedirect(String url) 似乎没有发送编码,为什么?
我有一些 Servlet 显式设置字符编码并重定向到某个 servlet
class Servlet1 extends HttpServle{
void doGet(..... ){
// ...
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8"):
//......
response.redirect(servlet2);
}
}
class Servlet2 extends HttpServle{
void doGet(..... ){
// ...
request.getCharacterEncoding(); // prints null ?? why???
//......
}
}
那么,为什么字符编码不随请求一起发送?
I have some Servlet that explicity sets the character encoding and redirect to some servlet
class Servlet1 extends HttpServle{
void doGet(..... ){
// ...
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8"):
//......
response.redirect(servlet2);
}
}
class Servlet2 extends HttpServle{
void doGet(..... ){
// ...
request.getCharacterEncoding(); // prints null ?? why???
//......
}
}
So, why the character encoding not being send with the request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpServletResponse#setCharacterEncoding()
设置当前响应的编码,而不是后续请求的编码。客户端也没有责任在后续请求中将其传回。如果没有客户端的交互,您想要实现的目标根本不可能实现,在这种情况下不需要这样做。为了得到你想要的,客户端必须设置 HTTP < code>Content-Type 标头本身带有charset
属性。使用 HTTP 标头调试器工具(例如 Firebug)检查它,您会发现它在请求中不存在。The
HttpServletResponse#setCharacterEncoding()
sets the encoding on the current response, not on the subsequent request. It's also not the client's responsibility to pass it back on the subsequent request. What you're trying to achieve is simply not possible without interaction of the client, which it is not required to do in this case. To get what you want, the client has to set the HTTPContent-Type
header with acharset
attribute itself. Check it with a HTTP header debugger tool like Firebug and you'll see that it is absent in the request.