package com.javahonk;

import java.io.IOException;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;

public class JettyServlet {	
	
	private static final Logger LOGGER = LogManager.getLogger(JettyServlet.class.getName());
	
	public static void main(String[] args) throws Exception {
		
		LOGGER.info("Jetty server started");		
		Server server = new Server(8080);
		ServletHandler handler = new ServletHandler();
		handler.addServletWithMapping(HelloServlet.class, "/JavaHonkJettyServlet");
		server.setHandler(handler);
		server.start();
		server.join();
	}

	@SuppressWarnings("serial")
	public static class HelloServlet extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {
			
			String userName = request.getParameter("name");
		    String password = request.getParameter("password");	
		    
			response.setContentType("text/html");
			response.setStatus(HttpServletResponse.SC_OK);
			response.getWriter().println("<h1>Java Honk: Jetty Servlet Spring Integration</h1>");
			response.getWriter().println("<h2>You entered below parameter</h2>");
			response.getWriter().println("<h3>User name: "+userName+"</h3>");
			response.getWriter().println("<h3>Password: "+password+"</h3>");
		}
		
		public void doPost(HttpServletRequest request, HttpServletResponse res)
				throws IOException, ServletException {
			doGet(request, res);
		}
	}

}
