我有一組三個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();
}
}
謝謝。
不管具體問題。您可能希望實現更好的*關注分離* - 因此,您不應該將HTML放入Servlet中。您可以使用JSP和JSTL,並將Java代碼放入Servlet中。 – Lion
我明白了。感謝回覆,但我被告知要這樣做,所以我將不得不堅持這一點。 – user1852114
豆代碼?這很簡單,只是屬性和getters和setters。將它存儲在會話中或使用隱藏字段更有趣。 –