Java servlet programming and application

xiaoxiao2021-03-06  72

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 转载请注明原文地址:https://www.9cbs.com/read-93418.html


New Post(0)