2015-06-18 66 views
0

我是一个努力学习我能做些什么JSP的人。我使用Eclipse作为我的IDE,并遇到了一个小问题,我希望有人能帮助我。我有以下的文件创建一个动态Web项目:如何使用Eclipse编译和运行JSP Beans项目

的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进行更多的实验。

+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> 

我有测试确定。希望它有帮助。

+0

谢谢。这工作,现在看我的代码我明白为什么它。再次感谢 – jdubicki

+0

我的荣幸。如果您觉得它有用,请接受我的答案。 – Javakid

相关问题