在 JSP 中将 JavaBean 放入 HttpSession [英] Putting a JavaBean into a HttpSession in JSP

查看:54
本文介绍了在 JSP 中将 JavaBean 放入 HttpSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个关于调查的简单"应用程序.您有一些选项(复选框),然后结果(投票)显示在其他页面中.

I was trying to do a "simple" application about a survey. Where you have some options (checkboxes) and then the results (votes) are shown in other page.

我有这个类,我可以一直保存结果(我想使用 HttpSession 更新这个类).我用的是HashMap这个类,但是我改了,我觉得没关系:

I have this class where I can keep the results all time (I want to update this class, using HttpSession). I was using the class HashMap, but I changed, I think it doesn't matter:

package beans;
import java.util.ArrayList;
import java.util.List;

public class SurveyBean {

    private List<String> keys;
    private List<String> values;

    public SurveyBean() {
        keys = new ArrayList<String>(); //keys: {"Cat", "Dog", "Other animal"}
        values = new ArrayList<String>(); //Values: {"12","5","4"}
        // By example, a value of 12 means, 12 people like cats.

        keys.add("Cat");
        keys.add("Dog");
        keys.add("Bird");

        // Don't ask me why did I use Strings instead of Integers.
        values.add("0"); // Zero votes 
        values.add("0");
        values.add("0");

    }

    // add one vote to the list of values
    public void addVote( String key, int value ) {
        int index = keys.indexOf(key);
        int newValue = Integer.parseInt(values.get(index)) + value;
        values.set(index, "" + newValue);
    }


    /********* Get and set methods *********/

现在,这是主窗体 (jsp),它尝试将 JavaBean 放入会话中:注意:我正在使用旧的 JSP sintaxis,因为我还在学习中.

Now, this is the main form (jsp), which tries to put a JavaBean into the session: Note: I'm using old sintaxis of JSP, because I'm still learning.

<%@page contentType="text/html" session="true" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <!-- Create the JavaBean "SurveyBean" (Scope: session) -->
    <jsp:useBean id="survey" class="beans.SurveyBean" scope="session"  />

    What's your favorite animal ?
    <form action="page2.jsp" method="POST">
        <%
            java.util.List<String> list = survey.getKeys();

            /* It prints:
             * What's your favorite animal? (Bird,Dog,Cat, etc.)    
            */
            for( int i = 0; i < list.size(); i++ ) {
                out.println("<input type=\"radio\" name=\"name\" value=\"" + list.get(i) +"\">" + list.get(i) + "<br>");
            }
        %>

        <input type="submit" value="Send" />
    </form>

</body>

这是结果页面:

<%@page import="beans.SurveyBean, java.util.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

    <%

      // getting the checKbox selected
      String name = request.getParameter("name");

      // Trying to get the object survey from the session
      HttpSession ses = request.getSession(false);
      SurveyBean sv = (SurveyBean) request.getAttribute("survey");

      // Add one vote to the list of values
      List<String> keys = sv.getKeys();
      List<String> values = sv.getValues();

      // I can't use the objects "Keys" and "values", because they are marked as Null.
      // Why they are Null !!!! ???

    %>

</body>

这里的问题是我不能使用对象SurveyBean.我不确定第一页(表单)是否正确初始化了 bean.而且我无法从会话中获取对象.
注意:对不起,我的英语真的很差.

The problem here is I can't use the object SurveyBean. I'm not sure if the first page (form) initializes the bean correctly. And I can't get the object from the session.
Note: Sorry, my English is really bad.

推荐答案

现在我已经解决了这个问题.在第一个 jsp 中,它将对象调查放在会话中.我添加这一行:

Now I've solved the problem. In the first jsp, which puts the object survey in the session. I add this line:

request.setAttribute("survey2",survey2);

在接收对象的第二页中,我添加了另一行:

And in the second page, which receives the object, I add this other line:

<%@page contentType="text/html" pageEncoding="UTF-8" session="true"%>

我认为有必要在指令@page 中添加属性session".

I suposse it was neccesary to add the attribute "session" in the directive @page.

实际上,这不是解决方案.我认为它有效,因为我已将代码放入 Servlet 中,如下所示:

In reality, that's not the solution. I think it works because I've put the code into a Servlet, like this:

PrintWriter out = response.getWriter();
    try {

        //Getting the parameter and the object survey
        String name = request.getParameter("name");
        HttpSession ses = request.getSession(false);
        SurveyBean survey = (SurveyBean) ses.getAttribute("survey");

        // Adding one vote
        survey.addVote(name,1);

        // Forwarding
        ses.setAttribute("survey", survey);
        RequestDispatcher rd = request.getRequestDispatcher("/page2.jsp");
        rd.forward(request, response);

    } finally {            
        out.close();
    }

这篇关于在 JSP 中将 JavaBean 放入 HttpSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆