Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In spring mvc how to get the context path in controller

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

like image 412
kavi Avatar asked Jul 15 '13 12:07

kavi


People also ask

What is context path in Spring MVC?

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/

What is context root path?

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.

What is context URL?

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.


1 Answers

  1. Variable request is declared, but is not initialized. No wonder you get a NullPointerException.

  2. 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();
    }
}
like image 78
Roadrunner Avatar answered Sep 29 '22 15:09

Roadrunner