2013-01-13 55 views
3

我有一組三個servlet,每個servlet都有一個帶單選按鈕的表單,它將其值傳遞給下一個servlet。我想要做的是使一個JavaBean類與servlet一起使用,並在從一個servlet到另一個servlet時保持它們的狀態和單選按鈕的選擇。任何人都可以爲JavaBean提供一些示例代碼嗎?Servlet with JavaBeans

下面是一些第一servlet代碼的:

(另兩個servlet有其他問題,同樣的代碼)

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try { 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet 1</title>");    
     out.println("</head>"); 
     out.println("<form id=\"form1\" action = \"Servlet2\" method = \"POST\">"); 
     out.println("<input type = \"radio\" name = \"Q1\" value = \"Answer 1\"> Answer 1 <br />\n" + 
        "<input type = \"radio\" name = \"Q1\" value = \"Answer 2\"> Answer 2 <br />\n" + 
        "<input type = \"radio\" name = \"Q1\" value = \"Answer 3\"> Answer 3 <br /> <br />"); 

     out.println("<input type = \"radio\" name = \"Q2\" value = \"Answer 1\"> Answer 1 <br />\n" + 
        "<input type = \"radio\" name = \"Q2\" value = \"Answer 2\"> Answer 2 <br />\n" + 
        "<input type = \"radio\" name = \"Q2\" value = \"Answer 3\"> Answer 3 <br /> <br />"); 

     out.println("<input type = \"radio\" name = \"Q3\" value = \"Answer 1\"> Answer 1 <br />\n" + 
        "<input type = \"radio\" name = \"Q3\" value = \"Answer 2\"> Answer 2 <br />\n" + 
        "<input type = \"radio\" name = \"Q3\" value = \"Answer 3\"> Answer 3 <br /> <br />"); 
     out.println("<input type = \"submit\" value = \"Next\">"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } finally {    
     out.close(); 
    } 
} 

謝謝。

+2

不管具體問題。您可能希望實現更好的*關注分離* - 因此,您不應該將HTML放入Servlet中。您可以使用JSP和JSTL,並將Java代碼放入Servlet中。 – Lion

+0

我明白了。感謝回覆,但我被告知要這樣做,所以我將不得不堅持這一點。 – user1852114

+2

豆代碼?這很簡單,只是屬性和getters和setters。將它存儲在會話中或使用隱藏字段更有趣。 –

回答

4

下面是一個簡單的JavaBean類的例子:

import java.io.Serializable; 

public class AnswersBean implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private String answer1; 

    private String answer2; 

    private String answer3; 

    public String getAnswer1() { 
     return answer1; 
    } 

    public void setAnswer1(String answer1) { 
     this.answer1 = answer1; 
    } 

    public String getAnswer2() { 
     return answer2; 
    } 

    public void setAnswer2(String answer2) { 
     this.answer2 = answer2; 
    } 

    public String getAnswer3() { 
     return answer3; 
    } 

    public void setAnswer3(String answer3) { 
     this.answer3 = question3; 
    } 

} 


在你的第二個servlet可以創建並使用所需的信息填充你的bean:在第三

package com.example; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.ServletException; 
import java.io.IOException; 

import com.example.AnswersBean; 

public class Servlet2 extends HttpServlet { 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException { 
     String answer1 = request.getParameter("Q1"); 
     String answer2 = request.getParameter("Q2"); 
     String answer3 = request.getParameter("Q3"); 

     // you may check if the answers exist and validate them 

     AnswersBean answersBean = new AnswersBean(); 
     answersBean.setAnswer1(answer1); 
     answersBean.setAnswer2(answer2); 
     answersBean.setAnswer3(answer3); 

     // store bean in session 
     request.getSession().setAttribute("answers", answersBean); 

     // you now can forward to some view, for example some results.jsp 
     request.getRequestDispatcher("/results.jsp").forward(request, response); 
    } 
} 


現在Servlet你可以檢查session是否包含bean:

package com.example; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.ServletException; 
import java.io.IOException; 

import com.example.AnswersBean; 

public class Servlet3 extends HttpServlet { 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException { 
     AnswersBean answers = request.getSession().getAttribute("answers"); 

     if (answers == null) { 
      // no bean was set in session 
        // do something 
     } else { 
      // there is a bean in session 
        // do something 
     } 

     // you now can forward to some view, for example some foo.jsp 
     request.getRequestDispatcher("/foo.jsp").forward(request, response); 

     // or do something else 
    } 
} 


或者你可以檢查豆權使用Expression Language(EL)一些JSP頁面內是這樣的:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Answers</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body> 
    <c:choose> 
     <c:when test="${empty answers}"> 
      <h3>There are currently no answers.</h3> 
     </c:when> 
     <c:otherwise> 
      <h3>First answer: ${answers.question1}</h3> 
      <h3>Second answer: ${answers.question2}</h3> 
      <h3>Third answer: ${answers.question3}</h3> 
     </c:otherwise> 
    </c:choose> 
</body> 
</html> 

附:
閱讀:
JavaBeans
JavaBeans Specification
Oracle's JavaBeans tutorials

UPDATE
我建議你閱讀Servlet和JSP這個流行的教程,以進一步提高自己的技能:從
Beginning & Intermediate Servlet & JSP Tutorials

+0

非常感謝informatik01。我只需要一些更多的幫助。在第三個servlet中,我必須像第二個servlet那樣做,例如。 'String answer4 = request.getParameter(「Q4」); etc AnswersBean answersBean = new AnswersBean(); answersBean。setAnswer4(answer4); 等' ,以便編碼到另一個頁面,我可以打印結果? – user1852114

+0

@ user1852114在我的示例的第三個Servlet中,您將從Session **中檢索AnswerBean,您之前將它放在第二個Servlet中。當你執行request.getParameter(「some_parameter」)時,你通常會從HTML _form_中獲得一些**提交的參數**。在具體情況下你可以自由地做任何你需要的事情。 – informatik01