jsp中getRealPath()从服务器获取的路径是什么 [英] What does the path get from the server by getRealPath () in jsp

查看:242
本文介绍了jsp中getRealPath()从服务器获取的路径是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 struts 1.3,jsp 来开发应用程序。
我想知道以下代码将从服务器返回路径。

I am using struts 1.3,jsp for developing application. I want to know what following code will return path from the server.

 path = getServlet().getServletContext().getRealPath("/") +"images\\logos\\"+ formFile.getFileName();

服务器的路径是什么。我可以使用此路径在页面上显示图像。

What will be the path from the server.Can i use this path for showing image on the page.

推荐答案

首先:不推荐使用getRealPath。 (比较: Interface ServletRequest )。您应该尝试这样做(因为规范2.1):

First of all: getRealPath is deprecated. (compare: Interface ServletRequest). You should try this instead (since spec 2.1):

ServletContext context = session.getServletContext();
String realContextPath = context.getRealPath(request.getContextPath());

在此之前,它高度依赖于服务器实现。根据规范,如果将应用程序部署为归档模块(war,ear等),则允许返回null。我相信这种情况从未发生过,例如使用WebLogic。它将路径返回到已解压缩归档的临时目录。所以,做一个很长的答案:

Earlier to this, it was highly depended on the server implementation. According to the spec it was allowed to return null if the application was deployed as a archived module (war, ear, etc.) I believe this never happened e.g. with WebLogic. It returned the path to the temp directory where the archives had been unpacked. So, to make a long answer short:

你的代码最终将是这样的:

Your code will end up with something like this:

x:\your\path\on\drive\images\logos\somename.ext

无法将其用作图像网址。

It's not possible to use this as an image URL.

这篇关于jsp中getRealPath()从服务器获取的路径是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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