2014-10-11 55 views
0

我難以置信地堅持創建這個。我的計劃的想法是發揮誰想成爲百萬富翁,每個豆是每個問題。我正在指示servlet讀取bean並將問題和可能的答案發送給要顯示的JSP。用戶將通過超鏈接選擇其中一個答案,該鏈接將查詢字符串放入servlet的url中,以檢查對實際答案的選擇答案。在一個java bean中servlet的值

我也想說一點我很新,在這我知道我的servlet是錯的我只是不明白該怎麼做:P謝謝大家花時間看看這個!

這裏是JSP:

<html> 

    <link rel="stylesheet" type="text/css" href="css/Millionaire.css"> 


    <body> 
    <form method="get"> 
     <img src="img/mlogo.jpg" > 

     <table> 
     <th> Question: </th> 
     <th>${Bean.Question}</th> 
     </table> 

     <br> 

     <table> 
      <tr> 
       <td><a href="?selectedAnswer=a">A: ${Bean.question} </a></td> 
       <td><a href="?selectedAnswer=b">B: ${Bean.a2} </a></td> 
      </tr> 
      <tr> 
       <td><a href="?selectedAnswer=c">C: ${Bean.a3} </a></td> 
       <td><a href="?selectedAnswer=d">D: ${Bean.a4} </a></td> 
      </tr> 
     </table> 

     <br> 
     <br> 

     <table> 
     <th> Life-Lines </th> 
     <tr> 
      <td><a href="?selectedLifeLine=1">Skip</a></td> 
      <td><a href="?selectedLifeLine=2">Skip</a></td> 
      <td><a href="?selectedLifeLine=3">Skip</a></td> 
     </tr> 
     </table> 

     <br> 
     <br> 

     <div><input type="submit" value="Final Answer"><div> 
    </form> 
    </body> 
</html> 

這裏是我的Servlet:

包分配1;

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import assignment1.question1; 
/** 
* 
* @author Powa 
*/ 
public class Servlet extends HttpServlet{ 
    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException{ 

     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     HttpSession session = request.getSession(); 
     question1.getAttribute("question"); 

     session.setAttribute("Bean","Question"); 

     session.setAttribute("Bean","a1"); 
     session.setAttribute("Bean","a2"); 
     session.setAttribute("Bean","a3"); 
     session.setAttribute("Bean","a4"); 



    } 
} 

最後這裏是第一個問題豆:

包分配1;

/** 
* 
* @author Powa 
*/ 
public class question1 implements java.io.Serializable { 

    private String question = "What type of Pokemon is Pikachu?"; 
    private String a1 = "Fire"; 
    private String a2 = "Water"; 
    private String a3 = "Grass"; 
    private String a4 = "Electric"; 

    private String answer = "Electric"; 

    /** 
    * @return the question 
    */ 
    public String getQuestion() { 
     return question; 
    } 

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

    /** 
    * @return the a1 
    */ 
    public String getA1() { 
     return a1; 
    } 

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

    /** 
    * @return the a2 
    */ 
    public String getA2() { 
     return a2; 
    } 

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

    /** 
    * @return the a3 
    */ 
    public String getA3() { 
     return a3; 
    } 

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

    /** 
    * @return the a4 
    */ 
    public String getA4() { 
     return a4; 
    } 

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

    /** 
    * @return the answer 
    */ 
    public String getAnswer() { 
     return answer; 
    } 

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




} 

回答

0

您正在爲a1,a2,a3,a4設置鍵爲「Bean」。它覆蓋了以前的值。嘗試給出不同的屬性名稱。另外,a1,a2 ..值作爲字符串傳遞。如果你想讓question1類中的值實例化question1並執行諸如session.setAttribute(「a1」,question1.getA1());

+0

當我使用session.getAttrbute喜歡你說我得到一個錯誤說「不靜態方法getA1()不能從靜態上下文中引用 – 2014-10-12 19:06:40

0

第一步,修改你的servlet:

response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
HttpSession session = request.getSession(); 
session.setAttribute("question1",new question1()); 

並修改JSP中:

<html> 

    <link rel="stylesheet" type="text/css" href="css/Millionaire.css"> 


    <body> 
    <form method="get"> 
     <img src="img/mlogo.jpg" > 

     <table> 
     <th> Question: </th> 
     <th>${sessionScope.question1.question}</th> 
     </table> 

     <br> 

     <table> 
      <tr> 
       <td><a href="?selectedAnswer=a">A: ${sessionScope.question1.a1} </a></td> 
       <td><a href="?selectedAnswer=b">B: ${sessionScope.question1.a2} </a></td> 
      </tr> 
      <tr> 
       <td><a href="?selectedAnswer=c">C: ${sessionScope.question1.a3} </a></td> 
       <td><a href="?selectedAnswer=d">D: ${sessionScope.question1.a4} </a></td> 
      </tr> 
     </table> 

     <br> 
     <br> 

     <table> 
     <th> Life-Lines </th> 
     <tr> 
      <td><a href="?selectedLifeLine=1">Skip</a></td> 
      <td><a href="?selectedLifeLine=2">Skip</a></td> 
      <td><a href="?selectedLifeLine=3">Skip</a></td> 
     </tr> 
     </table> 

     <br> 
     <br> 

     <div><input type="submit" value="Final Answer"><div> 
    </form> 
    </body> 
</html> 
+0

不幸的是這些值仍然不顯示在我的jsp – 2014-10-12 19:46:00

+0

中修復它.. @ MaxPellegrini – Vito 2014-10-13 01:49:02

相關問題