How to get the context path in a Spring Web application
In this tutorial, we discuss 2 ways for retrieving the context path in a Spring Web application.
1- HttpServletRequest
The typical way of getting the context path is through the HttpServletRequest class.
Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath() method.
@RequestMapping(value = "/", method = RequestMethod.GET) public String home(HttpServletRequest request) throws IOException
Now that you get the context path, you can pass it to the services that need it.
2- ServletContext
If you want to get the context path from within a service or a component or anywhere inside your application and you don’t want to pass it as a parameter from your controller, then you can use ServletContext.
Simply add a class field of type ServletContext and annotate it with @Autowired.
@Autowired private ServletContext context;
Now inside your method, you can get the context path through:
Summary
In this tutorial, we discuss 2 ways for retrieving the context path in a Spring Web application.
Next Steps
If you’re interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you’ll need to become a professional developer.
Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.
request.getContextPath()
This section describe you how to get the contextPath of web application.
request.getContextPath()
request.getContextPath()
In this section we will learn about how to get the contextPath of a web application from HttpServletRequest object.
An application server may host more than one web application. ContextPath of a web application is the name of the application in the URI just after the port number followed by a /(black slash). In a Servlet you can get the contextPath using the HttpServletRequest object. In a JSP page you can get the contextPath in two ways. In a first method you can use the implicit request object and in the second method you can use JSP EL.
We will illustrate this concept using a simple example. We will use Eclipse to compile and the Tomcat 7 to deploy the application.
We will understand this concept with a simple example. We will give here a simple example which will demonstrate you how to get the contextPath in Servlet as well as in JSP. Here we are going to give a simple dynamic project into which we will create a Servlet into which we will use the HttpServletRequest object to get the contextPath. We will also create the JSP page to get the contextPath. In the JSP page we will use both of the methods of getting contextPath i.e. using JSP EL and implicit object request.
Directory Structure
ContextPathExample.java
package net.roseindia; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ContextPathExample extends HttpServlet < public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException < response.setContentType("text/html"); PrintWriter out = response.getWriter(); String contextPath = request.getContextPath(); out.println("Context Path = "+contextPath); >>
contextPathExample--> cntxpath net.roseindia.ContextPathExample cntxpath /cntxpathExample
contextPath.jsp
Context Path Using implicit request object = ""
Context Path Using JSP EL = "$"
When you will compile and deploy the example and execute the Servlet then the output will be as follows :
When you will compile and deploy the JSP page then the output will be as follows :
Tutorials
- Creating a String
- Introduction to JSP
- Reading Request Information
- Introduction to the JSP Java Server Pages
- JSP FUNDAMENTALS
- Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
- JSP date example
- Reading Request Information
- Using Beans in JSP. A brief introduction to JSP and Java Beans.
- JSP 2.0 — New Features
- JSP Excel Tutorial
- Create Excel Sheet Using JSP
- Inserting Image In Excel Sheet Using JSP
- How to Create Excel Page Using JSP
- How to Create New Excel Sheet Using JSP
- Create Excel Sheet Using JSP
- Passing Various Data Types into Cells
- Assing Date into Cells
- Reading And Writing Excel File
- Selecting Excel Sheet File
- Zoom in Excel
- Zoom Out Excel
- Merging Two Cells
- Working With Fonts
- Working With Alignment Using JSP for Excel
- Shifting Row Using JSP
- Freeze And Split Pane
- Repeating Rows And Column
- different borders
- Create Shape in Excel Using JSP
- Creating Oval in Excel Using JSP
- Create Box in Excel Sheet Using JSP
- Filling Color in Excel Using JSP
- Set Font into TextBox String Using JSP
- Fit Hight Ad Wdth of Ecel Seet Uing JSP
- Setting Line Style
- Finding a Factorial using while loop
- JSP Simple Examples
- Calculate factorial Using Recursion
- Multiple Methods in Jsp
request.getContextPath()
This section describe you how to get the contextPath of web application.
request.getContextPath()
request.getContextPath()
In this section we will learn about how to get the contextPath of a web application from HttpServletRequest object.
An application server may host more than one web application. ContextPath of a web application is the name of the application in the URI just after the port number followed by a /(black slash). In a Servlet you can get the contextPath using the HttpServletRequest object. In a JSP page you can get the contextPath in two ways. In a first method you can use the implicit request object and in the second method you can use JSP EL.
We will illustrate this concept using a simple example. We will use Eclipse to compile and the Tomcat 7 to deploy the application.
We will understand this concept with a simple example. We will give here a simple example which will demonstrate you how to get the contextPath in Servlet as well as in JSP. Here we are going to give a simple dynamic project into which we will create a Servlet into which we will use the HttpServletRequest object to get the contextPath. We will also create the JSP page to get the contextPath. In the JSP page we will use both of the methods of getting contextPath i.e. using JSP EL and implicit object request.
Directory Structure
ContextPathExample.java
package net.roseindia; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ContextPathExample extends HttpServlet < public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException < response.setContentType("text/html"); PrintWriter out = response.getWriter(); String contextPath = request.getContextPath(); out.println("Context Path = "+contextPath); >>
contextPathExample--> cntxpath net.roseindia.ContextPathExample cntxpath /cntxpathExample
contextPath.jsp
Context Path Using implicit request object = ""
Context Path Using JSP EL = "$"
When you will compile and deploy the example and execute the Servlet then the output will be as follows :
When you will compile and deploy the JSP page then the output will be as follows :
Tutorials
- Creating a String
- Introduction to JSP
- Reading Request Information
- Introduction to the JSP Java Server Pages
- JSP FUNDAMENTALS
- Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
- JSP date example
- Reading Request Information
- Using Beans in JSP. A brief introduction to JSP and Java Beans.
- JSP 2.0 — New Features
- JSP Excel Tutorial
- Create Excel Sheet Using JSP
- Inserting Image In Excel Sheet Using JSP
- How to Create Excel Page Using JSP
- How to Create New Excel Sheet Using JSP
- Create Excel Sheet Using JSP
- Passing Various Data Types into Cells
- Assing Date into Cells
- Reading And Writing Excel File
- Selecting Excel Sheet File
- Zoom in Excel
- Zoom Out Excel
- Merging Two Cells
- Working With Fonts
- Working With Alignment Using JSP for Excel
- Shifting Row Using JSP
- Freeze And Split Pane
- Repeating Rows And Column
- different borders
- Create Shape in Excel Using JSP
- Creating Oval in Excel Using JSP
- Create Box in Excel Sheet Using JSP
- Filling Color in Excel Using JSP
- Set Font into TextBox String Using JSP
- Fit Hight Ad Wdth of Ecel Seet Uing JSP
- Setting Line Style
- Finding a Factorial using while loop
- JSP Simple Examples
- Calculate factorial Using Recursion
- Multiple Methods in Jsp