4

我有一组三个 servlet,每个都有一个带有单选按钮的表单,这些单选按钮将其值传递给下一个 servlet。我要做的是制作一个 JavaBean 类来与 servlet 一起使用,并在从一个 servlet 到另一个 servlet 时保持它们的状态和单选按钮的选择。任何人都可以建议一些JavaBean的示例代码吗?

这是一些第一个 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();
    }
}

谢谢你。

4

1 回答 1

5

这是一个简单的 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 中,您可以检查会话是否包含 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
    }
}


或者,您可以使用表达式语言(EL) 在某个 JSP 页面中检查 bean,如下所示:

<%@ 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>

PS
阅读本文:
JavaBeans
JavaBeans Specification
Oracle 的 JavaBeans 教程

更新
我建议你阅读这个关于 Servlet 和 JSP 的流行教程,以进一步提高你的技能:
Beginning & Intermediate Servlet & JSP Tutorials

于 2013-01-14T00:23:01.973 回答