The use of the Httpservletrequ interface is very similar to the request object of the JSP built-in object, and the request object actually
is an instance of the HttpServletRequest interface, but the process of gas instantiation is automatic and does not need to be customized.
The following example achieves the effect of setting and obtaining a HttpServletRequest object through an interface.
An example of the request scope property.
Requestdemo.java
1 PackageCOM.MHB;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One Public classRequestdemoextendsHttpServlet { A - Public voidInit ()throwsservletexception { - } the Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - - //formatting and encoding the output content +Response.setcontenttype ("text/html;charset=gb2312"); -PrintWriter out =Response.getwriter (); + 22//stored within the request range Request.setattribute ("name", "Tester"); 24//Get request Scope Name property in String name = (string) Request.getattribute ("name"); - -Out.println ("); -Out.println ("<body>");out.println ("Name:" +name); -Out.println ("</body>"); toOut.println ("); + - } the Public voidDoPost (httpservletrequest request, httpservletresponse response) * throwsservletexception, IOException { $ }Panax Notoginseng Public voiddestroy () { - Super. Destroy (); the } +}
Browser display:
Use of HttpServletRequest interface instantiation