gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:44:26 28 4
gpt4 key购买 nike

这是我第一次从事 J2EE 项目,遇到以下问题:

我有以下实现 HttpServlet 接口(interface)的 servlet。

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

现在我必须将它放入 session 中,以便我可以将它用于 JSP View 。

我该怎么做?

最佳答案

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

这会将 salDettaglio 放入 session 中,您可以使用属性标识符访问它 salDettaglio 请记住,它将它存储为一个普通的 Object 因此,如果您使用的是 sciplet,则必须在 JSP 中强制转换。

另见 this question

关于java - 如何将对象(定义到 servlet 中)放入 session 中以将其用于 JSP 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27144604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com