職案人

求職・歴史・仏教などについて掲載するつもりだが、自分の思いつきが多いブログだよ。適当に付き合って下さい。

使用注意getOutputStream()

2015年04月18日 | java
サーブレットの基本ソースコード

「MyServlet.java」のソースコード

protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
  下記の様にするとエラーが出る。
ServletOutputStream out = response.getOutputStream();
  ※htmlを出力
out.println("<html><head><title>MyServlet</title>");
out.println("</head>");
out.println("<body>※送信されたテキスト</body></html>");
out.flush();
}

下記のように改善しないと動作しない。
protected void doGet(HttpServletRequest request,
   HttpServletResponse response)
   throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
     response.setContentType("text/html; charset=UTF-8");


PrintWriter out = response.getWriter();
out.println("<html>\n"
+ "<head><title>\n"
+ "MyServlet\n"
+ "</title>\n"
+ "\n"
+ "</head>\n"
+ "<body>※送信されたテキスト</body>\n"
+ "</html>\n");


}
または
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

response.setContentType("text/html; charset=Shift_JIS");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>MyServlet</title>");
out.println("</head>");
out.println("<body>");

out.println("

※送信されたテキスト

");

out.println("</body>");
out.println("</html>");

out.close();
}
}
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

何だこのエラーは、

2015年04月18日 | java
HTTPステータス 500 - ISO 8859-1 の文字ではありません: ※


type 例外レポート

メッセージ ISO 8859-1 の文字ではありません: ※

説明 The server encountered an internal error that prevented it from fulfilling this request.

例外
java.io.CharConversionException: ISO 8859-1 の文字ではありません: ※
javax.servlet.ServletOutputStream.print(ServletOutputStream.java:77)
javax.servlet.ServletOutputStream.println(ServletOutputStream.java:187)
jp.codezine.MyServlet.doGet(MyServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



注意 原因のすべてのスタックトレースは、Apache Tomcat/7.0.47のログに記録されています
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする