jsp页面自动生成的basePath脚本,端口号后面为什么少一个"/"?

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

request.getServerPort()后面为什么不加"/"? myeclipse自动生成的jsp界面都不加,网上的好像也都不加。

阅读 3.5k
2 个回答

打印下path 就知道了

可以看下源码方法的说明文档。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. For servlets in the default (root) context, this method

    • returns "". The container does not decode this string.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题