如何在JSP中保存XML文件?

时间:2022-01-27 01:03:41

In my project I need to save XML file in JSP. I'm using Eclipse as IDE and want to save XML file within project so that XML file can be used by Fusioncharts to generate a graph. I cannot give any local path as JSP runs on server.

在我的项目中,我需要在JSP中保存XML文件。我正在使用Eclipse作为IDE,并希望在项目中保存XML文件,以便Fusioncharts可以使用XML文件生成图形。当JSP在服务器上运行时,我不能给出任何本地路径。

StreamResult result = new StreamResult(new File("C:\\graph.xml"));

This won't work in that case, so what can I do in this case?

在这种情况下这不起作用,所以在这种情况下我该怎么办?

3 个解决方案

#1


1  

Use File#createTempFile(). It's platform independent and in case of a JSP/Servlet webapp it'll be created in the container-managed default temp folder.

使用File#createTempFile()。它是独立于平台的,如果是JSP / Servlet webapp,它将在容器管理的默认临时文件夹中创建。

File file = File.createTempFile("graph", ".xml");
// ...

#2


0  

You need to use the servlet context to get a web-server independent fiel location in a servlet. I like this way of doing things :

您需要使用servlet上下文在servlet中获取与Web服务器无关的fiel位置。我喜欢这种做事方式:

public String getCachePath( ServletContext sc, HttpServletRequest request )
{
    //get cache dir from web.xml
    String cacheDir = getInitParameter( "cache.dir");
    //String fileName = File.separator +"tmp"+File.separator +
    String fileName = sc.getRealPath( request.getContextPath() ) + File.separator + "WEB-INF" + File.separator +
    cacheDir + File.separator;
    return fileName; 
}//met

And then you can define cache.dir in your web.xml like this :

然后你可以在web.xml中定义cache.dir,如下所示:

<servlet>
    <servlet-name>server</servlet-name>
    <servlet-class>fully qualified name of your class</servlet-class>
    <init-param>
        <param-name>cache.dir</param-name>
        <param-value>cache</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

#3


0  

As @BalusC pointed out, my first answer is maybe tied to using jetty as it expands war.

正如@BalusC所指出的那样,我的第一个答案可能与使用码头有关,因为它扩大了战争。

So, a mechanism that would work whatever server you use would be introspection :

因此,一种可以使用您使用的任何服务器的机制将是内省:

 InputSream is = YouClass.getResourceAsStream( "file.xml" );

This will always work as long as file.xml is in the same folder (when deployed, in a war or not) as the folder containing the class file YourClass.class.

只要file.xml位于同一文件夹中(在部署时,在战争中或不在战争中)作为包含类文件YourClass.class的文件夹,这将始终有效。

Eclipse as a special resource folder, enforced in maven, than you can use to put resources and ensure they will be deployed with your class files when you build your projects.

Eclipse作为一个特殊的资源文件夹,在maven中强制执行,比您可以用来放置资源并确保在构建项目时将它们与类文件一起部署。

#1


1  

Use File#createTempFile(). It's platform independent and in case of a JSP/Servlet webapp it'll be created in the container-managed default temp folder.

使用File#createTempFile()。它是独立于平台的,如果是JSP / Servlet webapp,它将在容器管理的默认临时文件夹中创建。

File file = File.createTempFile("graph", ".xml");
// ...

#2


0  

You need to use the servlet context to get a web-server independent fiel location in a servlet. I like this way of doing things :

您需要使用servlet上下文在servlet中获取与Web服务器无关的fiel位置。我喜欢这种做事方式:

public String getCachePath( ServletContext sc, HttpServletRequest request )
{
    //get cache dir from web.xml
    String cacheDir = getInitParameter( "cache.dir");
    //String fileName = File.separator +"tmp"+File.separator +
    String fileName = sc.getRealPath( request.getContextPath() ) + File.separator + "WEB-INF" + File.separator +
    cacheDir + File.separator;
    return fileName; 
}//met

And then you can define cache.dir in your web.xml like this :

然后你可以在web.xml中定义cache.dir,如下所示:

<servlet>
    <servlet-name>server</servlet-name>
    <servlet-class>fully qualified name of your class</servlet-class>
    <init-param>
        <param-name>cache.dir</param-name>
        <param-value>cache</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

#3


0  

As @BalusC pointed out, my first answer is maybe tied to using jetty as it expands war.

正如@BalusC所指出的那样,我的第一个答案可能与使用码头有关,因为它扩大了战争。

So, a mechanism that would work whatever server you use would be introspection :

因此,一种可以使用您使用的任何服务器的机制将是内省:

 InputSream is = YouClass.getResourceAsStream( "file.xml" );

This will always work as long as file.xml is in the same folder (when deployed, in a war or not) as the folder containing the class file YourClass.class.

只要file.xml位于同一文件夹中(在部署时,在战争中或不在战争中)作为包含类文件YourClass.class的文件夹,这将始终有效。

Eclipse as a special resource folder, enforced in maven, than you can use to put resources and ensure they will be deployed with your class files when you build your projects.

Eclipse作为一个特殊的资源文件夹,在maven中强制执行,比您可以用来放置资源并确保在构建项目时将它们与类文件一起部署。