Home     RSSRSS

Posts Tagged ‘Servlet’

Passing Parameter/Value from Servlet to JSP

November 24, 2011 by kiranbadi1991 | Comments Off on Passing Parameter/Value from Servlet to JSP | Filed in Development

I had a hard time figuring out as how to pass value from servlet to JSP,it was always rendering me null value in the JSP though the value was showing up correctly in servlet,so writing this information in case anyone needs it,

   1: int ID = 0;
   2: String queryStr = "select LAST_INSERT_ID() from xxx";

   3: ResultSet rs = stmt.executeQuery(queryStr);

   4: if (rs.next()) {

   5:     ID = rs.getInt(1);   6:     

   7:     }  

   8:   System.out.println(" '" + ID + "'"); 

   9:   req.getSession().setAttribute("queryStr",ID);

  10: System.out.println(" '" + ID + "'"); 

  11: req.getRequestDispatcher("/Render.jsp").forward(req, resp);

 

In the above servlet code, I insert the some records from the form into  DB and in the same servlet I pull out the last auto increment ID associated with that insert from the same table and associate it with the request session.Now I want it to rendered in my next JSP page,

So in your JSP you need to pull it as with EL,

Your Reference  id is :${queryStr}

I would suggest associate the data with request session since values needs to persisted across the requests.In case if you are working with Netbeans, you can check the values of the session via HTTP Server Monitor, it is great tool which shows you the values before and after the request.

Hope this helps.

Tags: , ,