eclipseservletsrealpath

How can servlet create a file that user can visit directly


I'm using eclipse and tomcat to develop. It seems that only files in the WebContent folder can be visited by user.
And now I need to create a file dynamically in servlet,But how can I get the real path of WebContent folder?
I tried getServletContext().getRealPath(""),But it returns a path of workspace of eclipse,not the path of my project,it's something like this D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\,
while I would like something like this D:\git\Monitor\WebContent
Anybody knows the solution?~ Thanks a lot


Solution

  • I found a solution with my teacher's help,in another way though. Since I can't get the real path of WebContent,I can modify the server.xml to make a local path readable for the front-end.This config file is in the Servers folder. The bottom ofserver.xml is like this.

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
      <Context docBase="Monitor" path="/Monitor" reloadable="true" source="org.eclipse.jst.jee.server:Monitor"/>
    </Host>
    

    And I added a Context node,then it's like this

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
      <Context docBase="Monitor" path="/Monitor" reloadable="true" source="org.eclipse.jst.jee.server:Monitor"/>
      <Context docBase="D:\environment\apache-tomcat-9.0.0.M21-dev\user\logs" path="/Monitor/logs" reloadable="true" debug="" crossContext="true"></Context>
    </Host>
    

    This way,I can write files to D:\environment\apache-tomcat-9.0.0.M21-dev\user\logs in the servlet,and get the file using url localhost:8080/Monitor/logs/fileName in the front-end~