package ajaxstutter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author Ravitej Syamala */ // Wiring: // - ajaxstutter.html : Starting point // - ajaxClient.js : Javascript, used by ajaxstutter.html for ajax enabling // - ajaxstutter.java : Called from ajaxClient.js // - ajaxstutterClass.java : Called from ajaxstutter as a static (ajaxstutterClass.checkStutter()) // DEPLOY: // - ajaxstutter.html and ajaxClient.js must be in webapps/ajaxstutter // (Same level as WEB-INF) public class ajaxstutter extends HttpServlet { private static final long serialVersionUID = -3509437421722952331L; /** * Allows logging into the Catalina logs if needed. * * @param message * @throws IOException */ private void logCrud (String message) throws IOException { getServletContext().log (message); } /* * (non-Javadoc) * * @see javax.servlet.http.HttpServlet#doGet (javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // find our form text in the request String targetId = req.getParameter ("testPassage"); // content type is XML, not HTML resp.setContentType ("text/xml"); // no caching of results by client, forces reload resp.setHeader ("Cache-Control", "no-cache"); // if there is no stutter, send a simple message. if (((targetId != null) && !ajaxstutterClass.isStutter (targetId))) { resp.getWriter().write (" No stutters found... "); } else { // send a list of stutters, these are delimited by special symbols // although more xml could be used, replaceAll operation in javascript // ends up being less work/code. resp.getWriter().write (""); ajaxstutterClass.checkStutter (targetId, resp); resp.getWriter().write (""); } } /* * (non-Javadoc) * * @see javax.servlet.http.HttpServlet#doPost (javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // GET methods work best with AJAX in this scenario doGet (req, resp); } }