简体   繁体   中英

Using HttpServletRequest Outside of a Servlet

I'm working this API and there is a factory method that sets the current context (request, user, etc). The context needs a HttpServletRequest parameter, but I'm not for sure how to initialize it. I would like this setContext(HttpServletRequest) method to be in the default constructor of the class, how do I go about initializing it so that I can use it with setContext? Is the only way to work with HttpServletRequest objects in a servlet, so I can't use the a java class?

Here's the library I'm using: http://library.blackboard.com/ref/6760ba98-8f24-44f2-8e65-0dcee799abb8/index.html

I'm not sure why you'd want to use it outside of a Servlet context other than testing. For automated tests you can create a mock implementation that includes the features required to get the required functionality to work. Just make sure that the mock implementation behaves the same way as when it's running inside a servlet container.

You can either do it using mocks, or better yet: Use HttpUnit ServletUnit

However, keep in mind that this library probably needs to run inside a container: Review your options and, when sure of all the operations "the library" will invoke on the container, simply mock it up.

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