FileNotFoundException web.xml

时间:2011-12-13 01:00:51

标签: servlets web.xml filenotfoundexception

继续学习......我正在尝试将一些数据写入txt文件,但我收到了这个例外。我正在使用Tomcat 7.0。 EmailList.txt在文件夹中,所以我不知道会发生什么。你们可以请帮帮我吗

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>AddToEmailListServlet</servlet-name>
        <servlet-class>email.AddToEmailListServlet</servlet-class>
        <init-param>
            <param-name>relativePathToFile</param-name>
            <param-value>/WEB-INF/EmailList.txt</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>AddToEmailListServlet</servlet-name>
        <url-pattern>/addToEmailList</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>join_email_list.jsp</welcome-file>
    </welcome-file-list>
</web-app>

servlet类该类将处理数据。

package email;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import business.User;
import data.UserIO;

public class AddToEmailListServlet extends HttpServlet
{   @Override 
    protected void doPost(HttpServletRequest request, 
                          HttpServletResponse response) 
                          throws ServletException, IOException
    {
        // get parameters from the request
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");

        User user = new User(firstName, lastName, emailAddress);

       //validation
        String message = "";
        String url = "";
        if (firstName.length() == 0 || lastName.length() == 0 || emailAddress.length() == 0) {
            message = "Please fill out all three boxes";
            url = "/join_email_list.jsp";
        }else{
            message = "";
            ServletConfig config = getServletConfig();
            String relativePath = config.getInitParameter("relativePathToFile");
            UserIO.addRecord(user, relativePath);
            url = "/display_email_entry.jsp";
        }
        request.setAttribute("user", user);
        request.setAttribute("message", message);

        //forward request and response

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);             
    }    
}

I / O类

包裹数据;

import java.io. ; import java.util。;

import business.User;

public class UserIO
{
    public static void addRecord(User user, String filename) throws IOException
    {
        File file = new File(filename);
        PrintWriter out = new PrintWriter(
                new BufferedWriter(new FileWriter(file)));
        out.println(user.getEmailAddress()+ "|"
                + user.getFirstName() + "|"
                + user.getLastName());        
        out.close();
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于UserIO#addRecord()方法背后的代码,你根本没有在你的问题中显示,因此很难确定根本原因。但我知道你刚刚在给定的相对路径上做了new File()。这是一个坏主意,因为以下原因:

  • new File()相对于磁盘文件系统中的当前工作文件夹而不是webapp文件夹结构进行操作。即便如此,前导斜杠/使其相对于磁盘根目录。
  • WAR不一定在磁盘上扩展,也可以在内存中扩展,因此无法在new File()中使用任何有效的磁盘文件系统路径。
  • 重新部署WAR或甚至重新启动服务器时,扩展WAR文件夹中的所有更改都将丢失,因为它们不存在于原始WAR文件中。

而是写入绝对路径上某个外部扩展WAR文件夹中的文件,或者改为使用数据库。