Why the output is not getting printed in the File named EmailList.txt under WEB-INF folder from Servlet

后端 未结 1 1219
花落未央
花落未央 2021-01-21 10:03

Here is the my servlet class that is responsible to get the data from the HTML page and store that data in EmailList.txt file placed under WEB-INF directory:

Code Snippe

相关标签:
1条回答
  • 2021-01-21 10:40

    Right click on the file in eclipse an go to properties, you'll see that this is a different file.

    Actually you should not try to write/create a file in such a location. Your application may not always be unpacked from a .war archive, in which case ServletContext#getRealPath() will return null.

    An alternative would be to define a context init parameter in your web.xml and set a root for the file system that should be visible to your application

    <context-param>
        <param-name>fsroot</param-name>
        <param-value>E:\CodePractice</param-value>
    </context-param>
    

    and to retrieve it anywhere (servlets/JSPs) in your application you can use ServletContext#getInitParameter(java.lang.String):

    String path = getServletContext().getInitParameter("fsroot");
    
    0 讨论(0)
提交回复
热议问题