简体   繁体   中英

Is it fine to pass HttpServletrequest attribute to expose to the methods that is called from the controller

My question might be amature but still I want to know whether we can pass the HttpServletRequest,HttpServletResponse objects as arguments to the business logic methods?

Is that a good programming practice ?

Example :

@RequestMapping( value = "/controller_name" , method = RequestMethod.GET)
public String getNewRegistrationPage(HttpServletRequest req,HttpServletResponse res)
{
    getDetails(req);
}

public void businessLogic(HttpServletRequest **req**)
{
    //business logic here
}

Thanks, Sridhar R

Good practice is relative. My personal opinion is not to do it. I feel controllers should extract information from the request to send to the business layer. That way you could potentially use the business layer in a non web app environment. In cases where this is not feasible, I would have a util class to interact with the business layer and the HttpServletRequest object.

Its better not to do that. I see why you want to do that is because you want the parameters that have come from your JSP to be used in business logic. So this can be done using :

DataType param = request.getParameter("param");

getDetails(param);

So, by the suggested way, all your business logic should be in functions that are called in the controller before you redirect to the corresponding view.

I don't think is a good practice to do a business logic that use request and response as input param, because you can't reuse the business logic efficently in some other situation.

In the past we used to copy all the data from the request in a Hashmap and pass the hashmap as input param for the business logic.

I don't think that you always need to read infinite parameters, so now I prefer use simple POJO which is more maintainable in my opinion.

For same reasons as already mentioned in this post: I don't think this is a good practice.

Instead, I suggest you to look here to get more information about possible way to extract request data and passing them to your business logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM