2015-06-18 63 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

相關問題