import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet("/GuestBook") public class GuestBook extends HttpServlet { private void copyFile(PrintWriter out, BufferedReader fin) throws IOException { String line; while((line = fin.readLine()) != null) { out.println(line); } } private String replaceTagCharacter(String str) { return str.replace("&", "&").replace("<", "<") .replace(">", ">"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); File f = new File(getServletContext() .getRealPath("/WEB-INF/Guests.html")); BufferedReader fin = new BufferedReader( new InputStreamReader( new FileInputStream(f), "UTF-8")); copyFile(out, fin); fin.close(); out.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); File tmp = new File(getServletContext() .getRealPath("/WEB-INF/tmp.html")); PrintWriter tmpOut = new PrintWriter( new OutputStreamWriter( new FileOutputStream(tmp), "UTF-8")); File guests = new File(getServletContext() .getRealPath("/WEB-INF/Guests.html")); BufferedReader guestsIn = null; String line; try { guestsIn = new BufferedReader( new InputStreamReader( new FileInputStream(guests), "UTF-8")); while (true) { line = guestsIn.readLine(); if (line == null) { line = String.format("%n"); break; } if (line.contains("")) break; tmpOut.println(line); } } catch (FileNotFoundException e) { // /WEB-INF/Guests.html がない tmpOut.println( "\n" + "ゲストブック\n" + "

ゲストブック

\n" + "御記帳有難うございました。
\n" + "戻る
"); line = String.format("%n"); } tmpOut.println(""); tmpOut.printf( "%n", replaceTagCharacter( request.getParameter("名前"))); tmpOut.printf( "%n", replaceTagCharacter( request.getParameter("メールアドレス"))); tmpOut.printf( "%n", replaceTagCharacter( request.getParameter("ホームページ"))); tmpOut.printf( "%n", replaceTagCharacter( request.getParameter("ひとこと"))); tmpOut.println("
名前%s
メールアドレス%s
ホームページ%s
ひとこと%s
"); tmpOut.println("
"); tmpOut.println(line); if (guestsIn != null) { while (true) { line = guestsIn.readLine(); if (line == null) break; tmpOut.println(line); } guestsIn.close(); } tmpOut.close(); // rm WEB-INF/Guests.html guests.delete(); // mv WEB-INF/tmp.html WEB-INF/Guests.html tmp.renameTo(guests); response.setContentType( "text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); BufferedReader fin = new BufferedReader(new InputStreamReader( new FileInputStream(guests), "UTF-8")); copyFile(out, fin); fin.close(); out.close(); } }