1

我正在研究第 4 章中的 Murach 的 Java Servlet 和 JSP,当我加载 http:// 按钮时,我得到了错误。谁能看到导致问题的原因,我将所有文件都存储在 tomcat/webapps/MailList 中。我已经浏览了这段代码几个小时,找不到任何导致问题的语法,只是认为另一双眼睛可能会抓住它。或者有人可以解释一下,非常感谢任何帮助,这是我第一天弄乱 Servlets/JSP 和 tomcat。

join_email_list.html _

<!DOCTYPE html>
<html>
    <head>
        <title>Chapter 4 - Email List application</title>
    </head>
    <body background="C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg" >
        <h1>Join the Murach's mailing list</h1>
        <p>To join the Murach's mailing list, enter your name and email address below.<br>
            Then, click n the submit to recieve special offers.</p>
        <form action="show_email_entry.jsp" method="get">
            <table cellspacing="5">
                <tr>
                    <td align="right" >First name</td>
                    <td><input type="text" name="firstName"></td>
                </tr>
                <tr>
                    <td align="right">Last name</td>
                    <td><input type="text" name="lastName"></td>
                </tr>
                <tr>
                    <td align="right">email address</td>
                    <td><input type="text" name="emailAddress"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><br><input type="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

show_email_list.jsp _

<!DOCTYPE html public"-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Chapter 4 - Email List</title>
    </head>
    <body>
        <%@page import="business.*, data.*" %>
        <%
            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            String emailAddress = request.getParameter("emailAddress");
            User user = new User(firstName, lastName, emailAddress);
            UserIO.addRecord(user, "..webapps/MailingList/UserEmail.txt");
        %>
        <h1>Thanks for joining</h1>
        <table cellspacing="5">
            <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>

        <form action="join_email_list.html" method="post">
            <input type="submit" value="Return">
        </form>
    </body>
</html>

User.java类_

package business;

public class User {
    private String firstName;
    private String lastName;
    private String emailAddress;
    //this class defines a user, what we can get from a user to store
    public User(){}
    public User(String first, String last, String email){
        firstName=first;
        lastName=last;
        emailAddress=email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

}

UserIO.java类_

package data;

import business.User;
import java.io.*;

public class UserIO { //the user io class adds the entered info to a txt file a.k.a psuedo db
    public synchronized static void addRecord(User user, String fileName) 
            throws IOException{
        PrintWriter out = new PrintWriter( //open the printwriter
                new FileWriter(fileName, true)); //write to file
        out.println(user.getEmailAddress()+"|"//write these things to file
                +user.getFirstName()+"|"
                +user.getLastName());
        out.close();//close out to free resources
    }
}
4

2 回答 2

1

user在您尝试的范围内也不是不可访问的

C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg

不会工作

于 2012-12-23T06:30:14.067 回答
0

利用

UserIO.addRecord(user, "MailingList/UserEmail.txt");

代替

UserIO.addRecord(user, "..webapps/MailingList/UserEmail.txt");
于 2012-12-24T05:13:49.483 回答