anu
anu

Reputation: 11

HttpServletRequestObject is null

i had to complete a project that was 60% done already. I completed it and it works. But i cannot seem to understand why this part of the code is implemented. We are using Spring MVC and this method comes under a controller. The mapping works,functionality works. But i would like to know why should we check if the request and response objects are null. Under what conditions can they be null? I have searched for answers but unable to find them. Would love some clarity.

    @RequestMapping("/newVistor.htm")
public ModelAndView newVisitor(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (request == null || response == null) {
        log.info("Request or Response failed for NEWVISITOR METHOD..");
        throw new FERSGenericException(
                "Error in Transaction, Please re-Try. for more information check Logfile in C:\\FERSLOG folder",
                new NullPointerException());
    }
}

Upvotes: 1

Views: 2105

Answers (1)

Vasu
Vasu

Reputation: 22442

HttpServletRequest and HttpServletResponse objects are created & managed by servlet container (Tomcat, Weblogic, etc..), so they will NOT be null, unless you are managing them in a different way.

So, you don't need to do the above null checks for these HttpServletRequest and HttpServletResponse objects.

HttpServletRequest API:

The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).

HttpServletResponse API:

The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).

You can look here and here

Upvotes: 1

Related Questions