package coreservlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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



/**
 * Servlet implementation class pag02
 */
@WebServlet("/pag02")
public class pag02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public pag02() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//System.out.println ("<p> Folositi browser-ul ... </p>");
		String referer = "zero";
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet");	
				 
		Enumeration headerNames = request.getHeaderNames();
		String userAgent = request.getHeader("User-Agent");
		referer = request.getHeader("referer");
		
		//String ie = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 3.0.04506.30; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
		//String chrome = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
		
		//out.println("<br/>");
		//out.println(userAgent);
		//out.println(referer);
		String xslt = null; 		
	
	if ((referer!=null)&& (referer.contains("pag01")))
	{
		if (userAgent.contains("MSIE")) {
			//xslt = "textHomePage.xslt";
					out.println("Browser-ul este Internet Explorer");
					out.println("<br/>");
					out.println("Bine ai venit client IE!");
		} else if (userAgent.contains("Chrome")) {
				//xslt = "textHomePage.xslt";
					out.println("Browserul este Chrome! Non IE!");
			
		} else {		
				out.println("Browser necunscut!");
				out.println("<br/>");
				out.println("Bine ai venit client NON-IE!");
		}	
	} else 
	{
		out.println("nu a venit din pagina01");
		response.sendRedirect("pag01.html");
	
	}
	
		while (headerNames.hasMoreElements()) {
			String headerName = (String) headerNames.nextElement();
			out.print("<br/>Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);
			out.println("</em>");
			
			
		}		
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
