2014-10-11 57 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

相关问题