使用JavaBeans的Servlet

使用JavaBeans的Servlet

本文介绍了使用JavaBeans的Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a set of three servlets, each one of them has a form with radio buttons that passes its values to the next servlet. What I am trying to do is to make a JavaBean class to use with the servlets and keep the state of them and the choices of the radio buttons while going from one servlet to another. Could anyone suggest some sample code for the JavaBean please?

以下是一些第一个servlet代码:

Here is some of the first servlet code:

(其他两个servlet具有相同的其他问题代码)

(The other two servlets have same code for other questions)

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();
    }
}

谢谢。

推荐答案

这是一个简单的JavaBean类示例:

Here is a simple JavaBean class example:

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:


In your second Servlet you can create and populate your bean with the required information:

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:


Now in the third Servlet you can check if the session contains the 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
    }
}



或者您可以使用在某个JSP页面内检查bean( EL)像这样:


Or you can check the bean right inside some JSP page using Expression Language (EL) like this:

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

阅读本文:





更新

我建议你阅读这篇关于Servlets和JSP的流行教程,以进一步提高你的技能:

这篇关于使用JavaBeans的Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:33