【问题标题】:How to compile and run a JSP Beans project using Eclipse如何使用 Eclipse 编译和运行 JSP Beans 项目
【发布时间】:2015-06-18 13:01:14
【问题描述】:

我是一个试图了解 JSP 的菜鸟。我使用 Eclipse 作为我的 ide,遇到了一个小问题,希望有人能帮我解决。我有一个包含以下文件的动态 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 位于 user 包下的 Java Resources 文件夹中。我很确定 userData 的类应该被复制到 WEB-INF 文件夹中,但它不是,即使我自己把它放在那里,项目仍然不能正常运行。谁能告诉我如何在 Eclipse 中进行设置,以便它能够正确运行,并且我可以用 JSP 进行更多试验。

【问题讨论】:

  • 你的代码看起来不错。那么你的问题是什么?

标签: java html eclipse jsp


【解决方案1】:

问题是你输入的名字和UserData的setter不一样,请尝试修改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>

我已经测试过了。希望对您有所帮助。

【讨论】:

  • 谢谢。那行得通,现在查看我的代码,我明白了为什么。再次感谢
  • 我的荣幸。如果你觉得有用,请接受我的回答。
猜你喜欢
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
相关资源
最近更新 更多