2014-10-11 2 views
0

엄청나게 막히게 만들었습니다. 내 프로그램의 아이디어는 누가 백만장자가 되길 원하며 각 콩은 각 질문입니다. 나는 서블릿에게 빈을 읽고 지시 할 JSP에 대한 질문과 가능한 답을 보내도록 지시하고있다. 사용자는 하이퍼 링크를 통해 답변 중 하나를 선택하여 서블릿이 실제 대답에 대해 선택한 대답을 확인하는 URL에 쿼리 문자열을 배치합니다.서블릿에 자바 빈의 값

나는 또한이 점에서 매우 새로운 점을 알고 싶다. 나는 서블릿이 틀렸다는 것을 알고있다. 나는 그것을 어떻게하는지 이해하지 못한다 : 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> 

내 서블릿입니다 :

패키지 assignment1; 마침내 여기

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"); 



    } 
} 

그리고

은 첫 번째 질문에 대한 빈입니다 :

패키지 assignment1;

/** 
* 
* @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에 대해 "빈"으로 설정합니다. 이전 값보다 우선합니다. 다른 속성 이름을 부여하십시오. 또한 a1, a2 .. 값은 문자열로 전달됩니다. question1 클래스의 값을 원하면 question1을 인스턴스화하고 session.setAttribute ("a1", question1.getA1());

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

을 당신이 좋아하는 JSP 수정 :

+0

을 정적 메서드 getA1()은 정적 컨텍스트에서 참조 할 수 없습니다 –

0

첫 번째 단계는 서블릿 수정 내가 session.getAttrbute를 사용할 때 당신은 내가 "비 없다는 오류가 말했듯이

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

+0

에 표시되지 않습니다 .. @ MaxPellegrini – Vito

관련 문제