Use of HttpServletRequest interface instantiation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.