On the problem of ServletRequest strong turn httpservletrequest in filter

Source: Internet
Author: User

public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {
HttpServletRequest req = (httpservletrequest) request;
HttpServletResponse resp = (httpservletresponse) response;

}

Because I had to use the method in HttpServletRequest, I made a strong turn to servletrequest and then solved the problem. But later on, we know that it is safe for us to move up the subclass implementation to get the parent class object because the subclass inherits the method of the parent class completely, and we move up to the parent class, and when we call the parent class The method is fully found in the subclass implementation. Conversely, downward transformation is not safe, our subclasses will extend their methods in addition to fully inheriting the methods of the parent class, so we may not be able to find them in the parent class implementation when calling the subclass method, so it is not safe to move down.

But the implementation of this example makes us confused, we not only realized the downward transformation, but also called the sub-class extension method, is not the parent class, but realized. Isn't this a contradiction with what we're learning? Later I saw the API and some information I finally understand that this is not in conflict with what we have learned! Let's look at the API first:

public interface HttpServletRequest extends ServletRequest

Finally found the reason, the original httpservletrequest and ServletRequest are interfaces, they all just defined the method but did not provide relevant implementation. So the request object we see in ServletRequest request is not actually a concrete implementation of our ServletRequest.

Here we want to see whether our question is safe, in fact, the main object of the request is the specific implementation of the class is the inheritance of which interface, If you inherit from the HttpServletRequest interface then our approach to using the HttpServletRequest interface is secure. The test is as follows:

public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {
if (Request instanceof HttpServletRequest) {
System.out.println ("-------------");
}
HttpServletRequest req = (httpservletrequest) request;
if (req instanceof httpservletrequest) {
      System.out.println ("I am an instance of the right class");
}
HttpServletResponse resp = (httpservletresponse) response;
}

Output:

-------------

I'm an instance of the right class.

Prove that our request object is indeed an example of httpservletrequest.

This means that the Dofilter parameter request object is not generated in servletrequest request = new ServletRequest (); This form, but servletrequest request = new HttpServletRequest (); In this form, the request in the parameter is not the object of the parent class ServletRequest, but the object of the HttpServletRequest:

According to the Java object-oriented Programming (2nd edition), an object of a parent class type is used when the subclass new comes out (servletrequest request = new HttpServletRequest ();//Is this line of code), It cannot be called a parent class object, but rather a subclass of the upper transformation object. The difference between the two is that the parent object cannot be coerced into a subclass object, and the upper transformation object of the subclass can be cast back to the child class object.

Again, why do you want to cast in the filter?

A: ServletRequest request; This is to assign the subclass object to the parent class reference, the type of the runtime is a subclass, and the compile-time type is the parent class, but at run time, the method called by the parent class type Object, if there is one in the subclass, executes the method inside the subclass. If the compile-time type is the same method that the parent does not call, an error is encountered. So there's going to be a coercion type conversion, otherwise you'll get an error.

HttpServletRequest is more than ServletRequest. Some methods for HTTP protocol. such as GetHeader (String name), GetMethod (), getsession (), and so on.

Doubt resolved.

On the problem of ServletRequest strong turn httpservletrequest in filter

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.