Java 只有从servlet访问时,Tomcat5上带有日语的自定义404错误页面才变得不可读
我对Tomcat 5.0.28上的自定义错误页面有问题。用日语书写的错误页面无法阅读。(喜欢。。。あいう -> ??????) 我的环境Java 只有从servlet访问时,Tomcat5上带有日语的自定义404错误页面才变得不可读,java,jsp,tomcat,servlets,http-status-code-404,Java,Jsp,Tomcat,Servlets,Http Status Code 404,我对Tomcat 5.0.28上的自定义错误页面有问题。用日语书写的错误页面无法阅读。(喜欢。。。あいう -> ??????) 我的环境 Java Sun SDK 1.4 Tomcat 5.0.28 我有一个Java应用程序,它有一个servlet将任何URL请求的扩展名从.htm转换为.jsp。我的jsp是用日语编写的,因此需要编码指定的内容类型头,servlet提供了它。他们相处得很好 我想在此应用程序中添加一个自定义的日语404错误页面。但只有通过servlet访问时,该404页面的日
- Java Sun SDK 1.4
- Tomcat 5.0.28
text/html;charset=Shift\u JIS
,但当不可读时变为text/html
并不是通过这个servlet,内容类型头和往常一样,定制的404页面的日文是可以使用的。这个servlet只映射到扩展名为*.htm的请求
应用程序的web.xml上的“我的错误”页面设置:
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
错误页面JSP“404.JSP”:
http://example.com/nonexisting.jsp
(不通过servlet)
有两种常见错误类别:
- 404响应有一个内容类型头,说明它是charset=Shift_JIS,但实际上它的编码不同或已损坏。假设404.jsp在磁盘上编码正确,则当它加载到字符串或将字符串写入HTTP响应时,它可能已损坏。您在哪里设置它您的读者和/或作者的编码是否为Shift_JIS
- 404响应编码为Shift_JIS,但内容类型标题指定了不同的字符集(或无,默认为ISO-8859-1)。对于200个响应,您的doGet()方法似乎是正确的,但404错误可能采用不同的路径
很明显,查看404响应的HTTP头信息会非常有用。您还应该调试应用程序以检查404响应的处理方式。您的代码示例明显不完整:out和reqUrl没有使用,您也没有编写响应。谢谢您的评论。我附加了一些信息。谢谢Lorenzo。我就是这么做的我同意你的观点…只需删除未使用的
getWriter()
is works。通常情况下,编码是正确的,404.jsp是由Shift_JIS编码的。但是404.jsp的HTTP头,内容类型是“text/html”(可能是默认值),正如你所说的。它应该是getWriter()输出response对象并清除它。因此response不保留setContentType()谢谢你以后的请求。无论如何,非常感谢!
public class Converter extends HttpServlet {
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
response.setContentType("text/html; charset=Shift_JIS");
ServletContext context = getServletConfig().getServletContext();
PrintWriter out = response.getWriter();
String reqUrl = request.getServletPath();
String jspName = reqUrl.substring(1,reqUrl.indexOf("."));
RequestDispatcher dispatcher = context.getRequestDispatcher("/"+jspName+".jsp");
dispatcher.forward(request,response);
}
public void destroy() {
}
}
<%@ page language="java" isErrorPage="true" %>
<%@ page contentType="text/html; charset=Shift_JIS" pageEncoding="Shift_JIS" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>
<body>
test 404<br />
日本語出力テスト<br />
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>
<body>
test 404<br />
????????<br />
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>
<body>
test 404<br />
日本語出力テスト<br />
</body>
</html>
public class Converter extends HttpServlet {
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
response.setContentType("text/html; charset=Shift_JIS");
ServletContext context = getServletConfig().getServletContext();
//PrintWriter out = response.getWriter();
String reqUrl = request.getServletPath();
String jspName = reqUrl.substring(1,reqUrl.indexOf("."));
RequestDispatcher dispatcher = context.getRequestDispatcher("/"+jspName+".jsp");
dispatcher.forward(request,response);
}
public void destroy() {
}
}