import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; 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("/HighLight") public class HighLight extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("
"); // 適当な Java のソースファイル // (例えば HighLight.java のコピー) // を WEB アプリのルートフォルダに Tekito.txt // という名前で置いておくこと File f = new File(getServletContext() .getRealPath("/Tekito.txt")); String word = request.getQueryString(); InputStreamReader fr = new InputStreamReader (new FileInputStream(f), "UTF-8"); BufferedReader in = new BufferedReader(fr); while(true) { String line = in.readLine(); if (line == null) break; line = line.replace("&", "&"); line = line.replace("<", "<"); line = line.replace(">", ">"); if (word != null && word.length() != 0) { line = line.replace(word, "" + word + ""); } out.println(line); } out.println(""); out.close(); in.close(); } }