The difference between Senderror and SetStatus in HttpServletResponse

Source: Internet
Author: User

When the project was recently developed, it was necessary to use the response code to tell the front-end login exception (401) and permission validation not to pass (403) because the front-end interface was called by the Frontend. The front end to get the corresponding status code will be processed accordingly. The above-mentioned login verification and permission verification, the backend with spring interceptor technology implementation. In order to return the specified status code, the SetStatus method in HttpServletResponse is used. Everything is working, no problem! But suddenly I have an idea, if the login verification does not pass, you need to jump to a page specifically responsible for displaying 401 friendly tips, how to do? Some would say that using the Sendredirect method, yes. Do you have anything else? My practice is to directly according to the response status code, relying on the Error-page configuration in Web. XML to implement the error page jump. believe that the following code configuration, engage Javaweb developers should be familiar with: "<error-page> <error-code>401</error-code> <location>/ Web-inf/view/401.jsp</location></error-page> ' But when it's really running, SetStatus can actually set response's status code, but it's not as it's supposed to, Displays the contents of the 401.jsp page. After a search and verification, found that the use of senderror instead of setstatus, can achieve the desired results. But, why? What is the difference between Senderror and setstatus? The following is the content of the official translation API document: senderror (int SC): uses the specified status code and empties the buffer, sending an error response to the client. If the response has already been committed, this method throws IllegalStateException. With this method, the response should be considered committed and should not be written again. Senderror (int sc, String msg): Sends an error response to the client using the specified status code. The server defaults to creating an HTML-formatted service error page as the response result, which contains the text information specified by the parameter msg, the content type of the HTML page is "text/html", and preserves the cookies and other unmodified response header information. If an error page that corresponds to an incoming error code is already declared in Web. XML, the error page of this declaration will take precedence over the recommended MSG parameter to serve the client. (PS: Compared with the above method, I prefer the former.) Using the above method, you can customize the response results of different states to display on the client, we should want to let the customerDo you see the simple rough page created by the server? ) setStatus (int SC): Sets the status code for the response. This method is used to set the response status code when the response result is normal (for example, the status code is SC_OK or sc_moved_temporartly). If an error occurs and the visitor wants to invoke the error page defined in the Web app as a display, the Senderror method should be used instead. After using the SetStatus method, the container empties the buffer and sets the location response header, preserving cookies and other response header information. From the results of literal translation is not difficult to find, ha, senderror applicable to the error and there is a corresponding error page configuration as the output display, and setstatus suitable for normal response situation, can only change the response status code only.

The difference between Senderror and setstatus in HttpServletResponse

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.