продолжаю учебу ... Я пытаюсь записать некоторые данные в текстовый файл, но получаю это исключение.Я использую 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>
класс сервлета Этот класс будет обрабатывать данные.
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);
}
}
Класс ввода-вывода
данные пакета;
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();
}
}