I need application context path in controller, I tried the below code its throwing NULLPOINTER EXCEPTION.
HttpServletRequest request;
String Path = request.getContextPath();
Please help me
Thanks
Simply put, the context path is a name with which a web application is accessed. It is the root of the application. By default, Spring Boot serves the content on the root context path (“/”). So, any Boot application with default configuration can be accessed as: http://localhost:8080/
A context root identifies a Web application archive (WAR) file in an application server. The context root of a Web application determines which URLs application server will delegate to your web application. When MobileFabric installed, the required components' WARs are deployed to an app server.
A URL context implementation is a context that can handle arbitrary URL strings of the URL scheme supported by the context. It is a class that implements the Context interface or one of its subinterfaces. It differs from the (plain) context implementation described in the The Essential Components.
Variable request
is declared, but is not
initialized. No wonder you get a NullPointerException
.
Look at documentation to access different request related data.
After you read that, and are sure you want to tie your code to native Servlet API, try this:
@Controller
class MyController {
@RequestMapping
public void handleMe(HttpServletRequest request) {
String path = request.getContextPath();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With