package com.myeclipseide.progfilter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet("/FilterServlet")
public class FilterServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FilterServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		writer.println("<!DOCTYPE HTML>");
		writer.println("<html>");
		writer.println(" <head>");
		writer.println("  <title>servlet-filterlistener</title>");
		writer.println(" </head>");
		writer.println("<body marginwidth=100>");
		writer.println("<h1> Simple Number Filter</h1>");
		writer.println("<hr noshade=noshade />");
		writer.println("The servlet and filter have been registered programmatically using the Dynamic Servler registration feature of Java EE 6. Enter any text with alphabets, numbers and special characters. On pressing 'Filter', the NumberRemoverFilter is invoked and numbers and special characters are stripped off from your text. Please see the source code for more details.");
		writer.println("<hr noshade=noshade />");
		writer.println("<BR>");
		writer.println("<CENTER>  <form>");
		writer.println("   <label for=userInput>Enter some text:</label> <input type=text name=userInput>");
		writer.println("   <button type=submit>Filter</button></form>");
		writer.println("  </form>");
		if (req.getParameter("userInput") != null) {
			writer.println("  <h1>Your text has been filtered to: "
					+ req.getParameter("userInput") + "</h1>");

		}
		writer.println("<BR>");
		writer.println("<a href='index.html'>Go back</a>");
		writer.println(" </CENTER></body>");
		writer.println("</html>");
		writer.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {

	}

}
