Need to find the web application path

こ雲淡風輕ζ 提交于 2019-12-13 05:40:35

问题


I created a web application using netbeans and glassfish server. I created a new java file inside that application. I want to find the current application path in that java file.


回答1:


You can get path information from a servlet with methods of HttpServletRequest class:

public class RequestInfoExample extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {

      String requestURI = request.getRequestURI();
      String contextPath = request.getContextPath();
    }

Javadoc information:

getRequestUri() - Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request...

getContextPath() - Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character...



来源:https://stackoverflow.com/questions/5854554/need-to-find-the-web-application-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!