【问题标题】:Putting a JavaBean into a HttpSession in JSP在 JSP 中将 JavaBean 放入 HttpSession
【发布时间】:2012-12-30 01:27:16
【问题描述】:

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

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

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 的旧语法,因为我还在学习。

<%@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。而且我无法从会话中获取对象。
注意:对不起,我的英语真的很差。

【问题讨论】:

  • 好的,你能打印出 request.getSession().getId() 吗?我猜它们不同是因为您的浏览器中不允许会话。
  • @Peter Rader。我得到了以下 ID:“会话 ID:69f612591dc32c32951afac24d66”。那我该如何解决这个问题。
  • 刷新网页后sessionid会改变吗?
  • 不,如果我刷新网页,ID 不会改变。一个问题,当我编写以下代码时:。 JavaBean“调查”可用于我使用它的其他页面(甚至是 servlet)。不是吗? ...您认为我的代码有问题吗? (无论如何,我会尝试再次执行该示例)。
  • 起初,您的会话 (sessioncookies) 工作正常。

标签: jsp arraylist javabeans httpsession


【解决方案1】:

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

request.setAttribute("survey2",survey2);

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

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

我认为有必要在指令@page 中添加属性“session”。

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

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

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2019-02-17
    相关资源
    最近更新 更多