Context Path Example

How to get the context path in a Spring Web application

Image of 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

  1. Creating a String
  2. Introduction to JSP
  3. Reading Request Information
  4. Introduction to the JSP Java Server Pages
  5. JSP FUNDAMENTALS
  6. Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
  7. JSP date example
  8. Reading Request Information
  9. Using Beans in JSP. A brief introduction to JSP and Java Beans.
  10. JSP 2.0 — New Features
  11. JSP Excel Tutorial
  12. Create Excel Sheet Using JSP
  13. Inserting Image In Excel Sheet Using JSP
  14. How to Create Excel Page Using JSP
  15. How to Create New Excel Sheet Using JSP
  16. Create Excel Sheet Using JSP
  17. Passing Various Data Types into Cells
  18. Assing Date into Cells
  19. Reading And Writing Excel File
  20. Selecting Excel Sheet File
  21. Zoom in Excel
  22. Zoom Out Excel
  23. Merging Two Cells
  24. Working With Fonts
  25. Working With Alignment Using JSP for Excel
  26. Shifting Row Using JSP
  27. Freeze And Split Pane
  28. Repeating Rows And Column
  29. different borders
  30. Create Shape in Excel Using JSP
  31. Creating Oval in Excel Using JSP
  32. Create Box in Excel Sheet Using JSP
  33. Filling Color in Excel Using JSP
  34. Set Font into TextBox String Using JSP
  35. Fit Hight Ad Wdth of Ecel Seet Uing JSP
  36. Setting Line Style
  37. Finding a Factorial using while loop
  38. JSP Simple Examples
  39. Calculate factorial Using Recursion
  40. 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

  1. Creating a String
  2. Introduction to JSP
  3. Reading Request Information
  4. Introduction to the JSP Java Server Pages
  5. JSP FUNDAMENTALS
  6. Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
  7. JSP date example
  8. Reading Request Information
  9. Using Beans in JSP. A brief introduction to JSP and Java Beans.
  10. JSP 2.0 — New Features
  11. JSP Excel Tutorial
  12. Create Excel Sheet Using JSP
  13. Inserting Image In Excel Sheet Using JSP
  14. How to Create Excel Page Using JSP
  15. How to Create New Excel Sheet Using JSP
  16. Create Excel Sheet Using JSP
  17. Passing Various Data Types into Cells
  18. Assing Date into Cells
  19. Reading And Writing Excel File
  20. Selecting Excel Sheet File
  21. Zoom in Excel
  22. Zoom Out Excel
  23. Merging Two Cells
  24. Working With Fonts
  25. Working With Alignment Using JSP for Excel
  26. Shifting Row Using JSP
  27. Freeze And Split Pane
  28. Repeating Rows And Column
  29. different borders
  30. Create Shape in Excel Using JSP
  31. Creating Oval in Excel Using JSP
  32. Create Box in Excel Sheet Using JSP
  33. Filling Color in Excel Using JSP
  34. Set Font into TextBox String Using JSP
  35. Fit Hight Ad Wdth of Ecel Seet Uing JSP
  36. Setting Line Style
  37. Finding a Factorial using while loop
  38. JSP Simple Examples
  39. Calculate factorial Using Recursion
  40. Multiple Methods in Jsp

Источник

Читайте также:  Python управление процессами windows
Оцените статью