繁体   English   中英

将Java Bean中的值传递给Servlet

[英]Value in a java bean to servlet

我难以置信地创造了这一点。 我程序的想法是扮演谁想成为百万富翁,每个豆子都是每个问题。 我正在指示servlet读取Bean并将问题和可能的答案发送到要显示的JSP。 然后,用户将通过超链接选择答案之一,该超链接将查询字符串放入servlet的url中,以检查为实际答案选择的答案。

我也想指出一点,我很新,我知道我的servlet是错误的,我只是不明白如何做到这一点: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>  

这是我的Servlet:

包裹分配1;

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



    }
}

最后是第一个问题的bean:

包裹分配1;

/**
 *
 * @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;
    }




}

您正在将键设置为a1,a2,a3,a4的“ Bean”。 它覆盖了先前的值。 尝试提供不同的属性名称。 同样,a1,a2 ..值也作为字符串传递。 如果要在问题1类中使用值,请实例化问题1并执行类似session.setAttribute(“ a1”,question1.getA1());的操作。

第一步,修改您的servlet:

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

并像这样修改您的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>${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>  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM