001package bradleyross.j2ee.servlets;
002import java.util.Enumeration;
003/**
004* This is a simple example of a Java servlet.
005*<p>It identifies information about the node transmitting the
006*   the HTTP request.  This could be used in aiding security 
007*   efforts by specifying that certain actions can only be
008*   carried out if the node transmitting the request is a 
009*   link local address, site local address, or loopback 
010*   address.  Since these types of addresses can't be sent
011*   over the public internet, this would help insure that hackers
012*   couldn't carry out the operations remotely.</p>
013* <p>This is a very simple example based on the idea that
014*    simple examples should be under two pages in length.</p>
015* <p>You will notice to references to the javax.servlet and
016*    javax.servlet.http packages appear as hyperlinks.  This is 
017*    because I included the following options in the javadoc command.</p>
018*<p><code>-link "http://java.sun.com/j2se/1.4.2/docs/api"</code> <br>
019*<code>-link "http://tomcat.apache.org/tomcat-5.5-doc/servletapi"</code></p> 
020*<p>The relationship between the Java servlet to be executed and the
021*   HTTP call to the application server is defined by the server.xml file
022*   for the application server and the WEB-INF/web.xml file that exists for
023*   each web application.  The following is the WEB-INF/web.xml file
024*   that was used with this servlet.</p>
025* <p><code>
026* &lt;web-app&gt;<br>
027* &lt;servlet&gt;<br>
028* &lt;servlet-name&gt;first&lt;/servlet-name&gt;<br>
029* &lt;servlet-class&gt;bradleyross.servlets.firstServlet&lt;/servlet-class&gt;<br>
030* &lt;/servlet&gt;<br>
031* &lt;servlet-mapping&gt;<br>
032* &lt;servlet-name&gt;first&lt;/servlet-name&gt;<br>
033* &lt;url-pattern&gt;/firstServlet&lt;/url-pattern&gt;<br>
034* &lt;/servlet-mapping&gt;<br>
035* &lt;/web-app&gt;
036* </code></p>
037*<p>The meaning of this XML document is as follows.</p>
038* <ul>
039*<li><p>The servlet is named <code>first</code>
040*       (<code>servlet-name</code>).  This name doesn't appear
041*       in the Java code but is means of describing the configuration to
042*       the application server.</p></li>
043*<li><p>The Java class for the servlet is 
044*       <code>bradleyross.servlets.firstServlet</code> 
045*       (<code>servlet-class</code>).  (Although it isn't 
046*       indicated from this document, this class
047*       file was included in the CLASSPATH for the application server and
048*       is therefore accessible to the system.)</p></li>
049*<li><p>The URL entered in the browser will contain <code>/test</code>
050*       to indicate that this servlet is to be executed
051*       (<code>servlet-mapping</code>).  This is known as the
052*       servlet path, meaning that it refers to the path
053*       within the web application.</p></li>
054*</ul>
055*<p>The web application is contained in the directory
056*   <code>webapps/test</code> on the application server, meaning that the
057*   XML file is at <code>webapps/test/WEB-INF/web.xml</code>.  Unless
058*   there are entries in server.xml, the context path for the
059*   web application is <code>/test</code>, meaning that the
060*   servlet is accessed as <code>/test/firstServlet</code>.  (The 
061*   context path followed by the servlet path.)</p>
062* @author Bradley Ross
063*/
064public class firstServlet extends javax.servlet.http.HttpServlet
065   {
066   /**
067         * 
068         */
069        private static final long serialVersionUID = 1L;
070private String firstCell(String contents)
071      {
072      StringBuffer value = new StringBuffer();
073      value.append("<tr><td>" + contents + "</td>");
074      return new String(value);
075      }
076   private String lastCell(String contents)
077      {
078      StringBuffer value = new StringBuffer();
079      value.append("<td>" + contents + "</td></tr>");
080      return new String(value);
081      }
082   private String doubleCell(String first, String last)
083      {
084      StringBuffer value = new StringBuffer();
085      value.append("<tr><td>" + first + "</td><td>" +
086           last + "</td></tr>");
087      return new String(value);
088      }
089   public void init (javax.servlet.ServletConfig config)
090      throws javax.servlet.ServletException
091      { super.init(config); }
092   public void destroy()
093      { super.destroy(); }
094//    @SuppressWarnings("unchecked")
095   /**
096   * @param req Information concerning the HTTP request received
097   *        by the server.
098   * @param res Information concerning the HTTP response generated
099   *        by the server.
100   */
101   public void service (javax.servlet.http.HttpServletRequest req,
102          javax.servlet.http.HttpServletResponse res)
103          throws java.io.IOException
104      {
105      res.setContentType("text/html");
106      java.io.PrintWriter out = res.getWriter();
107      out.println("<html><head>");
108      out.println("<title>Sample Servlet</title>");
109      out.println("</head></body>");
110      out.println("<h1><center>Information on Requester</center></h1>");
111      out.println("<p>The IP address of the node sending the ");
112      out.println(" request is " + req.getRemoteAddr() + "</p>");
113      java.net.InetAddress address =
114         java.net.InetAddress.getByName(req.getRemoteAddr());
115      out.println("<table border>");
116      out.println(firstCell("isLinkLocalAddress")); 
117      out.println(lastCell(Boolean.toString(address.isLinkLocalAddress())));
118      out.println(firstCell("isSiteLocalAddress")); 
119      out.println(lastCell(Boolean.toString(address.isSiteLocalAddress())));
120      out.println(firstCell("isLoopbackAddress")); 
121      out.println(lastCell(Boolean.toString(address.isLoopbackAddress())));
122      out.println("</table>");
123      //
124      out.println("<h2>Headers</h2>");
125      out.println("<p>Get list of headers for request</p>");
126      out.println("<p><ol>");
127      Enumeration<String> e =  req.getHeaderNames();
128      while( e.hasMoreElements() )
129          { 
130          String headerName = (String) e.nextElement();
131          out.println("<li><p>" +  headerName +
132               "</p><p><ul>");
133          Enumeration<String> e2 = req.getHeaders(headerName);
134          while (e2.hasMoreElements() )
135             { out.println("<li><p>" +  e2.nextElement() + "</p></li>"); }
136          out.println("</ul></p></li>"); 
137          }
138      out.println("</ol></p>");
139      //
140      out.println("<p>URL components</p>");
141      out.println("<table border>");
142      out.println(doubleCell("getContextPath",
143          req.getContextPath()));
144      out.println(doubleCell("getMethod",
145          req.getMethod()));
146      out.println(doubleCell("getPathInfo",
147          req.getPathInfo()));
148      out.println(doubleCell("getPathTranslated", 
149          req.getPathTranslated()));
150      out.println(doubleCell("getQueryString", 
151          req.getQueryString()));
152      out.println(doubleCell("getRequestURI",
153          req.getRequestURI()));
154      out.println(doubleCell("getRequestURL",
155          new String(req.getRequestURL())));
156      out.println(doubleCell("getServletPath",
157          req.getServletPath()));
158      out.println("</table>");
159      out.println("<p>Cookies</p>");
160      out.println("</body></html>");
161      }
162   }