How do I get web application context path?

Category: javax.servlet, viewed: 20539 time(s).

The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. When I have a web application with the URL like http://localhost:8080/myapps then myapps is the context path.

For servlets in the default (root) context, this method returns "".

package org.kodejava.example.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class ContextPathDemo extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse res) 
            throws ServletException, IOException {

        //
        // HttpServletRequest.getContextPath() returns the portion of the
        // request URI that indicates the context of the request.
        //
        String contextPath = req.getContextPath();
    }
}

Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats