如何使用Eclipse编译和运行JSP Beans项目

时间:2015-06-18 03:21:55

标签: java html eclipse jsp

我是一个努力学习JSP的人。我使用Eclipse作为我的想法,并遇到了一个小问题,我希望有人可以帮助我。我有一个包含以下文件的动态Web项目:

的index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Get Name</title>
</head>
<body>
    <form action="saveName.jsp" method="post">
        What is your name?
        <input type="text" name="username" size="20">
        <br> 
        What is your email address?
        <input type="text" name="email" size="20">
        <br>
        What is your age?
        <input type="text" name="age" size="4">
        <br>
        <input type="submit">
    </form>
</body>
</html>

NextPage.jsp上

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<jsp:useBean id="user" class="user.UserData" scope="session"></jsp:useBean>
<!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>Next Page</title>
</head>
<body>
    You entered
    <br>
    Name: <%=user.getUserName()%>
    <br>
    Email: <%=user.getUserEmail()%>
    <br>
    Age: <%=user.getUserAge()%>
</body>
</html>

saveName.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<jsp:useBean id="user" class="user.UserData" scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="user" />
<!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>Save Name</title>
</head>
<body>
    <a href="nextPage.jsp">Continue</a>
</body>
</html>

userData.java

package user;

public class UserData {

    String userName;
    String userEmail;
    String userAge;

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName
     *            the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the userEmail
     */
    public String getUserEmail() {
        return userEmail;
    }

    /**
     * @param userEmail
     *            the userEmail to set
     */
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    /**
     * @return the userAge
     */
    public String getUserAge() {
        return userAge;
    }

    /**
     * @param userAge
     *            the userAge to set
     */
    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }

}

这个应用程序运行得很好,除了我的nextPage.jsp对所有值都为null。我知道这是因为没有调用userData.java。

以下是如何设置项目,html和jsp文件位于WebContent文件夹中。 userData.java位于用户包下的Java Resources文件夹中。我很确定userData的类应该被复制到WEB-INF文件夹中,但它不是,即使我把它放在那里,项目仍然无法正常运行。有人可以告诉我如何在Eclipse中进行设置,以便它能够正确运行,我可以使用JSP进行更多实验。

1 个答案:

答案 0 :(得分:0)

问题是您输入的名称与UserData的设置者之间存在差异,请尝试将index.html修改为:

<form action="saveName.jsp" method="post">
    What is your name?
    <input type="text" name="userName" size="20">
    <br> 
    What is your email address?
    <input type="text" name="userEmail" size="20">
    <br>
    What is your age?
    <input type="text" name="userAge" size="4">
    <br>
    <input type="submit">
</form>

我已经测试好了。希望它有所帮助。