HTTPSERVLET is inherited from GenericServlet, so it has GenericServlet similar methods and objects, which we use servlet programming, which supports HTTP's POST and GET and other methods. Programming: The following example is that the result is that the output simply returns the request line and header information sent to the server, as well as some accessible HTTP information, and the like. SnoopServlet.java's source code is as follows:
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet *;. import javax.servlet.http *;. public class SnoopServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter (); response.setContentType ( "text / plain"); out.println ( "Snoop Servlet"); out.println (); out.println ( "Servlet Init Parameters: "); Enumeration E = getinitParameterNames (); while (E.hasMoreElements ()) {string key = (String) E.NEXTELEMENT (); string value = getInitParameter (key); out.println (" key) "=" value);} out.println (); out.println ( "context init parameters:"); ServletContext context = getServletContext (); Enumeration enum = context.getInitParameterNames (); while (enum.hasMoreElements () ) {String key = (string) enum.nextelement (); object value = context.getinitParameter (key); out.println (" key " = " value);} out.p rintln (); out.println ( "Context attributes:"); enum = context.getAttributeNames (); while (enum.hasMoreElements ()) {String key = (String) enum.nextElement (); Object value = context.getAttribute ("" key "=" value); out.println (); out.println ("Request Attributes:"); E = Request.getaTributenames (); while (e.) HasmoreElements ()) {string key = (string) E.NEXTELEMENT (); object value = request.gettribute (key); out.println (" key " = " value);} out.println ();
Out.println ("servlet name:" GetServletName ()); Out.println ("protocol:" Request.GetProtocol ()); Out.println ("Scheme:" Request.getscheme ()); out.println ("Server Name:" Request.getServerName ()); Out.println ("Server Port:" Request.getServerPort (); Out.println ("Server Info:" Context.getServerInfo ()); OUT. Println ("Remote Addr:" Request.getRemoteAddr ()); Out.println ("Remote Host:" Request.getRemotehost ()); Out.Println ("Character Encoding:" Request.getCharacterencoding ()); OUT .println ("Content Length:" Request.getContentLength ()); Out.println ("Content Type:" Request.getContentType (); out.println ("locale:" request.getlocale ()); OUT .println ("default response buffer:" response.getBuffersize ()); out.println (); out.println ("Parameter Names in this request:"); E = Request.getParameterNames (); while (E.hasMoreElements ()) {String Key = (String) E.NEXTELEMENT (); string [] value = request.getParameterValues (key); out.print (" key " = "); For (int i = 0; i IF (cookies! = null) {for (int i = 0; i Program Output Init Parameters, Attribute Names In this Request, Parameter Names in this Request, Headers in this request, cookies in this request, and session information, etc. The object URI returned by the method GetRequesturi is part of the URL and is part of the URL to specify the machine; the string returned by the method GetPathInfo is a variety of options transmitted to the servlet, which is after the URL of the servlet. The string of method getPathTranslated returns is the SERVLET's own absolute path name, the snoopservlet.class file location is c: /jswk-1.0.1/examples/web-inf/servlets/snoopservlet.class, the method getPathTranslated returned string The value is it. Enter the following address in the browser: http: // localhost: 8080 / examples / servlet / snoopservlet The result will be output.