Reputation: 42957
this is my first time that I work on a J2EE project and I have the following problem:
I have the following servlet that implement the HttpServlet interface.
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;
}
}
As you can see into the service() method I have this array definition:
SalDettaglio[] salDettaglio
Now I have to put it into session so that I can use it into a JSP view.
How can I do it?
Upvotes: 1
Views: 203
Reputation: 662
Beware that Session's getAttribute / setAttribute methods are synchronized, but what you store with them is your responsibility. That means if you change a mutable field in the stored object that is not guaranteed to be seen by another thread. Since servlets are called by different threads from a thread-pool this can be a problem. To avoid this use either immutable objects or make your objects thread-safe.
Upvotes: 2
Reputation: 25
create one session object and put your array into that session object and using sendRedirect() you can fwd your session through it..!!
Upvotes: 1
Reputation: 1268
HttpSession session = req.getSession();
session.setAttribute("salDettaglio", salDettaglio);
This puts salDettaglio
in the session and you can acces it with the attribute identifier salDettaglio
keep in mind that it stores it as a plain Object
so you'll have to cast in in the JSP if you are using sciplets.
Also see this question
Upvotes: 2