GglMekataServlet.java (c) (ABA."Saba")
package abagames.gglmekata;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet for GoogryuMekataDeDon.
 */
public class GglMekataServlet extends HttpServlet {

  public static final String SYSTEM_URI = "http://www.mycgiserver.com/";

  public static final String SYSTEM_ROOT_PATH = "/members/xxxxx/";
  public static final String SYSTEM_ROOT_URI = SYSTEM_URI + "~xxxxx/";

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
   throws ServletException, IOException {
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    out.println("GglMekataServlet ready.");
    out.close();
  }

  public static final String MEKATA_FILE_NAME = "ggl_mekata.cocoon.xml";
  public static final String MEKATA_CAR_FILE_NAME = "ggl_mekata_car.cocoon.xml";
  public static final String MEKATA_CDR_FILE_NAME = "ggl_mekata_cdr.cocoon.xml";

  public static final String MEKATA_CONVERT_SERVLET_NAME = "servlet/abagames.gglmekata.GglMekataHtmlConverterServlet";

  private synchronized void addResult(String query, String count, String url, String title, String snippet)
   throws IOException, UnsupportedEncodingException {
    String wordElm = "<word>" + query + "</word>";
    String countElm = "<count>" + count + "</count>";
    String firsthitElm = "<firsthit>" + url + title + snippet + "</firsthit>";
    String result = "<result>" + wordElm + countElm + firsthitElm + "</result>";
    String carFileStr = XMLHandler.readFileString(SYSTEM_ROOT_PATH + MEKATA_CAR_FILE_NAME);
    String cdrFileStr = XMLHandler.readFileString(SYSTEM_ROOT_PATH + MEKATA_CDR_FILE_NAME);
    XMLHandler.writeFileString(
     SYSTEM_ROOT_PATH + MEKATA_CDR_FILE_NAME, result + cdrFileStr);
    XMLHandler.writeFileString(
     SYSTEM_ROOT_PATH + MEKATA_FILE_NAME, carFileStr+ result + cdrFileStr);
  }

  private String getStringFromForm(String text)
    throws UnsupportedEncodingException {
    return new String(text.getBytes("8859_1"), "UTF-8");
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
   throws ServletException, IOException {
    String query = getStringFromForm(request.getParameter("query"));
    String fixed = XMLHandler.getElement(
     XMLHandler.readFileString(SYSTEM_ROOT_PATH + MEKATA_CAR_FILE_NAME), "fixed");
    String result = new AccessGoogle().search(query + " " + fixed);

    addResult(query,
     XMLHandler.getElementWithTag(result, "estimatedTotalResultsCount"),
     XMLHandler.getElementWithTag(result, "URL"),
     XMLHandler.getElementWithTag(result, "title"),
     XMLHandler.getElementWithTag(result, "snippet")
     );

    response.sendRedirect(SYSTEM_ROOT_URI + MEKATA_FILE_NAME);
  }
}