需要找到Web应用程序的路径 [英] Need to find the web application path

查看:93
本文介绍了需要找到Web应用程序的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用netbeans和glassfish服务器创建了一个Web应用程序.我在该应用程序中创建了一个新的Java文件.我想在该java文件中找到当前的应用程序路径.

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.

推荐答案

您可以使用HttpServletRequest类的方法从servlet获取路径信息:

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信息:

Javadoc information:

getRequestUri()-返回的部分 协议中此请求的URL 在查询字符串中命名 HTTP请求的第一行...

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()-返回该部分 表示请求的URI的 请求的上下文.上下文 路径始终是请求中的第一位 URI.路径以"/"开头 字符,但不以"/"结尾 角色...

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...

这篇关于需要找到Web应用程序的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆