/* * This is an example servlet that echoes to the browser the servlet * request information as well as information about the request * protocol, requester host name and address, and the receiving * server name and port number. Request parameters are assumed to * to be in an invocation query string following the servlet name. */ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; /** * Example ReqInfoServlet -- Demonstrates various methods for obtaining * information about the request, scheme, protocol, receiving server, * and remote server and echoing that information to the browser page. */ public class ReqInfoServlet extends HttpServlet { /** * @param req Instance of class HttpServletRequest. * @param res Instance of class HttpServletResponse. * @exception IOException When writing the servlet info to the * browser page or getting the query string. */ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { // log("in it, boy!!"); String cookieSheet = this.getInitParameter("cookiePage"); ServletOutputStream os; Enumeration paramNames; String paramName, paramValue; Enumeration hdrNames; String hdrDate, hdrName, hdrValue; Enumeration initParamNames; String initParamName, initParamValue; /* set the output content type and get an output stream for the response */ res.setContentType("text/html"); PrintWriter pw = new PrintWriter(res.getOutputStream()); pw.println("
");

                /* get and print request protocol and scheme information */
                pw.println("Request protocol is " + 
                   req.getProtocol());
               pw.println("Request scheme is " + req.getScheme());
                
                /* get and print requester information */
                pw.println("Remote host is " + req.getRemoteHost());
                pw.println("Server name is " + req.getServerName());
                pw.println("Remote address is " + req.getRemoteAddr());
                
                /* get and print receiver server information */
                pw.println("Receiving server is " + req.getServerName() + 
                        " on port number " + req.getServerPort());

		pw.println("Servlet path is " + req.getServletPath());

		//		log("finished first paragraph");

                /* put the request header names
                   into an Enumeration of strings */
                hdrNames = req.getHeaderNames();

                /* loop through name Enumeration, get matching
                   value, and print name/value pair */
                pw.println();
                pw.println("Header data is:"); 
                while(hdrNames.hasMoreElements())
                {
                        /* get the next name from the 
                           Enumeration of parameters */
                        hdrName = (String)hdrNames.nextElement();

                        /* get the value of the parameter */
                        hdrValue = (String)req.getHeader(hdrName);
                        
                        /* print the name/value pair to the browser */
                        pw.println("name = " + hdrName + "; value = " + hdrValue);
                }
                pw.println();

		//		log("finished headers");

                /* put the request parameters
                   into an Enumeration of strings */
                paramNames = req.getParameterNames();

                /* loop through name Enumeration,get matching
                   value, and print name/value pair */
                pw.println("Request parameters are:");
                while(paramNames.hasMoreElements())
                {
                        /* get the next name from the 
                           Enumeration of parameters */
                        paramName = (String)paramNames.nextElement();

                        /* get the value of the parameter */
                        paramValue = (String)req.getParameter(paramName);
                        
                        /* print the name/value pair to the browser */
                        pw.println("name = " + paramName + "; value = " + paramValue);
                }
		pw.println();
		
		//		log("finished parameters");

		pw.println("Query string: " + req.getQueryString());
		pw.println("Path info: " + req.getPathInfo());
		pw.println("Translated path info: " + req.getPathTranslated());
		pw.println("Mime guess: " + getServletContext().getMimeType(req.getPathInfo()));
		pw.println();

		initParamNames = this.getInitParameterNames();
		pw.println("Init parameters are:");
		while(initParamNames.hasMoreElements()) {
		  initParamName = (String)initParamNames.nextElement();
		  initParamValue = (String)this.getInitParameter(initParamName);
		  pw.println("name = " + initParamName + "; value = " + initParamValue);
		}
		pw.println();

		//		log("finished extras");

		Date exp = new Date();
		exp.setYear(exp.getYear() + 5);
		//		log("exp: " + exp.toString());
		Util.setCookie(res, "cnum", "043667897", exp, "/", null, false);

		/*
		c = new Cookie("name", "Christopher Fry");
		c.setDomain(req.getServerName());
		c.setPath("/");
		c.setMaxAge(1000000000);
		res.addCookie(c);
		*/

		/*
		Cookie[] cooks;
//		log("finished init");
		cooks = req.getCookies();
//		log("finished call");

		pw.println("Cookies:");
		if(cooks != null)
		  for(int i = 0; i < cooks.length; i++)
		    pw.println(cooks[i].getDomain() + " " + cooks[i].getPath() + " " + cooks[i].getMaxAge() + " " + cooks[i].getName() + "=" + cooks[i].getValue());

		pw.println("
"); // log("finished cookie display"); */ pw.println("Cookies:"); String cook = req.getHeader("Cookie"); if(cook != null) { StringTokenizer t = new StringTokenizer(cook, ";\n"); String nvp, name, value; while(t.hasMoreTokens()) { nvp = t.nextToken(); if(nvp.charAt(0) == ' ') nvp = new String(nvp.substring(1)); int l = nvp.indexOf('='); if(l != -1) { name = new String(nvp.substring(0, l)); value = new String(nvp.substring(l+1)); pw.println(name + " = " + value); } } } /* open and display javascript cookie stuff */ if(cookieSheet != null) { // log("attempting to open cookie file: " + cookieSheet); File f = new File(cookieSheet); FileInputStream fin = new FileInputStream(f); DataInputStream din = new DataInputStream(fin); String line; while((line = din.readLine()) != null) pw.println(line); // log("finished cookie form printing"); } /* flush and close the PrintWriter */ pw.flush(); pw.close(); } // end of service() } // end of class ReqInfoServlet