職案人

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

使用注意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でシェアする
« 何だこのエラーは、 | トップ | カラム構造について »
最新の画像もっと見る

コメントを投稿

java」カテゴリの最新記事