Storm 2010-08-02 13:02
浏览 361
已采纳

struts2 中 怎么获取项目的 http url

容器是 tomcat, 我想在struts2的 action里 获取项目的 http url,比如 http://localhost:8080/myproject,不知如何得到?谢谢

  • 写回答

5条回答 默认 最新

  • gz8851 2010-08-02 13:45
    关注

    引自:http://guozheng.iteye.com/blog/607012

    从Request对象中可以获取各种路径信息,以下例子:
    假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下
    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    String remoteAddress=request.getRemoteAddr();

    String servletPath=request.getServletPath();

    String realPath=request.getRealPath("/");

    String remoteUser=request.getRemoteUser();

    String requestURI=request.getRequestURI();

    out.println("path:"+path+"
    ");

    out.println("basePath:"+basePath+"
    ");

    out.println("remoteAddr:"+remoteAddress+"
    ");

    out.println("servletPath:"+servletPath+"
    ");

    out.println("realPath:"+realPath+"
    ");

    out.println("remoteUser:"+remoteUser+"
    ");

    out.println("requestURI:"+requestURI+"
    ");

    结果:
    path:/WebDemo
    basePath:http://localhost:8683/WebDemo/
    remoteAddr:127.0.0.1
    servletPath:/index.jsp
    realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
    remoteUser:null
    requestURI:/WebDemo/index.jsp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料