HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.CharConversionException: Not an ISO 8859-1 character: з javax.servlet.ServletOutputStream.print(ServletOutputStream.java:89) dating.servlet.TestServlet.service(TestServlet.java:77) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
Apache Tomcat/6.0.20
Код сервлета выглядел следующим образом:
public class TestServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.setCharacterEncoding("UTF-8");
resp.getOutputStream().print("здравствуй, сервер");
}
}
Совершенно непонятно было, почему выскакивает исключение, когда я явно указываю resp.setCharacterEncoding("UTF-8"), ведь именно это являлось решением на просмотренных мною форумах.
Оказалось, проблема в том, что я по ошибке использовал resp.getOutputStream() вместо resp.getWriter(). Как только я исправил код на следующий:
public class TestServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.setCharacterEncoding("UTF-8");
resp.getWriter().print("здравствуй, сервер");
}
}
Результат:
здравствуй, сервер
Ура!

Комментариев нет:
Отправить комментарий