如何将对象(定义到servlet中)放入会话以将其用于JSP页面?

时间:2014-11-26 08:42:03

标签: java jsp session java-ee servlets

这是我第一次参与J2EE项目,我遇到了以下问题:

我有以下servlet实现 HttpServlet 接口。

public class Salwf extends HttpServlet {

    private String msg = StringUtils.EMPTY;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        StandardConfigurationFactory standardConfigurationFactory = new StandardConfigurationFactory();

        try {
            standardConfigurationFactory.init();
        } catch (ConfigurationException e) {
            throw new ServletException(e);
        }

        ConfigurationFactory.setInstance(standardConfigurationFactory);
    }

    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        LoggerMDC.setup(req, res);
        Logger logger = (Logger) Logger.getStdLogger(Monitoraggio.class); // do not declare 'logger' as static field in order to work with MDC

        String service = req.getParameter("serv");

        char serviceId = Utility.getServizio(req.getParameter("serv"));

        // The collection that have to be shown in a table inside the JSP view:
        SalDettaglio[] salDettaglio = this.getSalDettaglioList();

        gotoPage(ConfigurationFactory.getPropertiesPages().getProperty("pagina_salwf"), req, res);

        //String paginaDaLanciare = lanciaServizio(serviceId, req, res);
    }

    /*
    private String lanciaServizio(char num_servizio, HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        Logger logger = (Logger) Logger.getStdLogger(Monitoraggio.class); // do not declare 'logger' as static field in order to work with MDC

        HttpSession session;

    }
    */

    private void gotoPage(String address, HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        Logger logger = (Logger) Logger.getStdLogger(Monitoraggio.class); // do not declare 'logger' as static field in order to work with MDC
        logger.debug("gotoPage() | address: " + address);

        ServletContext ctx = getServletConfig().getServletContext();
        RequestDispatcher dispatcher = ctx.getRequestDispatcher(address);
        dispatcher.forward(req, res);
    }

    private SalDettaglio[] getSalDettaglioList(){

        SalDettaglio[] salDettaglioArrayResult;

        List<SalDettaglio> salDettaglioList = new ArrayList<SalDettaglio>();

        List<RM> rmList = new ArrayList<RM>();

        RM rm1 = new RM("codiceRM1", "Andrea", "01/01/1014", "acqRiserva1", "consegnaFinale1", "descrizioneRM1", BigDecimal.valueOf(1000));
        RM rm2 = new RM("codiceRM2", "Andrea", "01/01/1014", "acqRiserva2", "consegnaFinale2", "descrizioneRM2", BigDecimal.valueOf(1000));

        rmList.add(rm1);
        rmList.add(rm2);

        RM[] rmArray =  (RM[]) rmList.toArray();

        SalDettaglio salDettaglio1 = new SalDettaglio("codice1", "stato1", "01/01/2014", "01/06/2014", "Andrea",
                "Andrea", "Andrea", BigDecimal.valueOf(1000), "fornitore1",
                "rmConRiserva1", "errore1", null, rmArray);

        SalDettaglio salDettaglio2 = new SalDettaglio("codice2", "stato2", "01/01/2014", "01/06/2014", "Andrea",
                "Andrea", "Andrea", BigDecimal.valueOf(1000), "fornitore2",
                "rmConRiserva2", "errore2", null, rmArray);

        salDettaglioList.add(salDettaglio1);
        salDettaglioList.add(salDettaglio2);

        salDettaglioArrayResult = (SalDettaglio[]) salDettaglioList.toArray();

        return salDettaglioArrayResult;

    }

}

正如您可以看到 service()方法,我有这个数组定义:

SalDettaglio[] salDettaglio

现在我必须将它放入会话中,以便我可以将它用于 JSP 视图。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

HttpSession session = req.getSession();
session.setAttribute("salDettaglio", salDettaglio);

这会将salDettaglio放入会话中,您可以使用属性标识salDettaglio来访问它,请记住它会将其存储为普通Object,因此您必须进行转换在JSP中,如果你使用sciplets。

另见this question

答案 1 :(得分:2)

请注意Session的getAttribute / setAttribute方法是同步的,但您存储的内容是您的责任。这意味着如果您更改了存储对象中的可变字段,而该字段不能保证被另一个线程看到。由于servlet由来自线程池的不同线程调用,因此这可能是个问题。为避免这种情况,请使用不可变对象或使对象成为线程安全的。

答案 2 :(得分:1)

创建一个会话对象并将您的数组放入该会话对象并使用sendRedirect(),您可以通过它来转发会话.. !!