import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringReader; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; @WebServlet("/GuestBookDom") public class GuestBookDom extends HttpServlet { private void fileCopy(PrintWriter out, BufferedReader fin) throws IOException { String line; while((line = fin.readLine()) != null) { out.println(line); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File f = new File(getServletContext().getRealPath("/WEB-INF/GuestsDom.html")); response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); fileCopy(out, fin); fin.close(); out.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try { Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); String filename = getServletContext().getRealPath("/WEB-INF/GuestsDom.html"); try { // 読み込みの準備 doc = db.parse(filename); // 木構造の生成 } catch (Exception e) { String str = "\n" + "\n" + "ゲストブック\n" + "\n" + "

ゲストブック

\n" + "御記帳有難うございました。
\n" + "戻る
\n" + "\n"; doc = db.parse(new InputSource(new StringReader(str))); } Element root = doc.getDocumentElement(); // root は html 要素のはず Element body = (Element)root.getElementsByTagName("body").item(0); // body要素の検索 // 新しい tableの追加 Element tbl = doc.createElement("table"); tbl.setAttribute("border", "1"); tbl.appendChild(doc.createTextNode("\n")); for (String parameter: new String[] {"名前", "メールアドレス", "ホームページ", "ひとこと"}) { Element row = doc.createElement("tr"); Element td1 = doc.createElement("td"); td1.setTextContent(parameter); row.appendChild(td1); Element td2 = doc.createElement("td"); td2.setTextContent(request.getParameter(parameter)); row.appendChild(td2); tbl.appendChild(row); tbl.appendChild(doc.createTextNode("\n")); } body.appendChild(tbl); body.appendChild(doc.createTextNode("\n")); body.appendChild(doc.createElement("hr")); body.appendChild(doc.createTextNode("\n")); // 出力処理 TransformerFactory fac = TransformerFactory.newInstance(); Transformer tran = fac.newTransformer(); tran.setOutputProperty(OutputKeys.METHOD, "xml"); tran.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); OutputStream o = new FileOutputStream(filename); StreamResult result = new StreamResult(o); tran.transform(new DOMSource(doc.getDocumentElement()), result); o.close(); // response.sendRedirect("GuestBookDomCat"); response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8")); fileCopy(out, fin); fin.close(); out.close(); } catch (Exception e) { response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("
");
			e.printStackTrace(out);
			out.println("
"); } } }