Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest and HttpServletRequest in Spring MVC

What is the difference between the two? Both have a getParameter method as well as setAttribute method, then where comes the difference between the two?

1) Which one is better to use in general?

2) Please explain specific scenarios where they can be used.

like image 367
user182944 Avatar asked Jun 21 '14 14:06

user182944


People also ask

What is WebRequest in spring?

public interface WebRequest extends RequestAttributes. Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.

What is the use of HttpServletRequest in spring boot?

Spring Boot HttpServletRequest Logging Filter. A Request Logging Filter for Spring Boot applications, that allows you to read the request body multiple times. The request body is cached using an overridden HttpServletRequestWrapper. Then a reader that allows the client to read the body multiple times is returned.

What is the use of HttpServletRequest?

Interface HttpServletRequest. Extends the ServletRequest interface to provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods ( doGet , doPost , etc). String identifier for Basic authentication.

How do I get URI from WebRequest?

1) One can get request URI and client information from WebRequest using webRequest. getDescription(true). true will show user's information such as client id and false will just print URI. Save this answer.


1 Answers

The javadoc of WebRequest is pretty clear on the subject:

Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.

(emphasis mine).

The javadoc links to WebRequestInterceptor, which says:

Interface for general web request interception. Allows for being applied to Servlet request as well as Portlet request environments, by building on the WebRequest abstraction.

So, basically, you should not use WebRequest except in a WebRequestInterceptor. And they introduced this interface in order to be able to write interceptors that apply to servlets and portlets. Other than that, if you really need access to the request in Spring MVC controllers, you should use the standard HttpServletRequest.

like image 68
JB Nizet Avatar answered Sep 28 '22 07:09

JB Nizet