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" + ""); e.printStackTrace(out); out.println(""); } } }