从JSP页面

时间:2017-04-18 16:14:22

标签: java jsp io

我无法从JSP页面写入文本文件。请帮帮我

    public class UserIO {
    public static void add(User user, String filepath) throws IOException {
        File file = new File(filepath);
        PrintWriter out = new PrintWriter(new FileWriter(file, true));
        out.println(user.getEmailAddress() + "|" + user.getFirstName() + "|"
                + user.getLastName());
        out.close();
    }
}
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Murach's Java Servlets and JSP</title>
</head>
<body>
    <%@ page import="business.*, data.*"%>

    <%
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");

        ServletContext sc = this.getServletContext();
        String path = sc.getRealPath("/WEB-INF/EmailList.txt");

        User user = new User(firstName, lastName, emailAddress);
        UserIO.add(user, path);
    %>

    <h1>Thank for joining our email list</h1>
    <p>Here is the information that you entered</p>

    <table cellspacing="5" cellpadding="5" border="1">
        <tr>
            <td align="right">First Name:</td>
            <td><%=user.getFirstName()%></td>
        </tr>
        <tr>
            <td align="right">Last Name:</td>
            <td><%=user.getLastName()%></td>
        </tr>
        <tr>
            <td align="right">Email address:</td>
            <td><%=user.getEmailAddress()%></td>
        </tr>
    </table>

    <p>
        To enter another email address, click on the Back <br> button in
        your browser or Return button shown <br> below.
    </p>
    <form action="EmailRegistrationStep1.html" method="post">
        <input type="submit" value="Return">
    </form>
</body>
</html>

我的JSP页面显示正确的User类数据。但我想在EmailList.txt上写下用户的数据。

我将EmailList.txt放在WEB-INF / EmailList.txt上。但是在EmailList.txt中没有任何事情发生。

请帮帮我

0 个答案:

没有答案