Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with application variables to store the list of names code

 
Didier McGillis
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a select list of 300+ names. The way I want to do it is to have that list built and cached on startup so that the html that is displaying that list will be done on startup not each time the page is rendered.

so what I am trying to do is use application variables to store the list of names

Not sure whats wrong with this code.
public class SomeList extends HttpServlet{

private String listOfSome;
public void init(){

}

public void service(HttpServletRequest req,HttpServletResponse resp){

private void getSome() throws DataLayerException
{
Connection Connection = null;
Connection = ConnectionFactory.getConnection();
synchronized(Connection)
{
try
{
listOfSome = "";
Statement Statement = Connection.createStatement();
ResultSet rs = Statement.executeQuery("SELECT row1, row2, row3, row4, row5 FROM table WHERE row6=aa order by row3");
String row5="";
while (rs.next())
{
if(rs.getString(4) != null){city = rs.getString(4);}
listOfDealer += "<option value=" + rs.getString(1) + ">"+ rs.getString(2) +","+ rs.getString(3) +","+ row5 +","+ rs.getString(5) + "</option>";
}
if (rs !=null) rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}


public String getListOfSome() throws DataLayerException {
getSome();
this.getServletConfig().getServletContext().setAttribute("listOfSome",listOfSome);
//return listOfSome;
}
}
 
Didier McGillis
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on the jsp page the result is null
application.getInitParameter("listOfStuff")
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you try to get an attribute with getInitParameter()? Use getAttribute().
 
Didier McGillis
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I did that too, both getAttribute and getInitParameter return null.
[ February 01, 2005: Message edited by: Didier McGillis ]
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is wrong, it doesn't set anything inside the session:

this.getServletConfig().getServletContext().setAttribute("listOfSome",listOfSome);

you need something like req.getSession().setAttribute("yourID", obj);
[ February 01, 2005: Message edited by: Kashif Riaz ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kashif Riaz:
I think this is wrong, it doesn't set anything inside the session:

this.getServletConfig().getServletContext().setAttribute("listOfSome",listOfSome);

you need something like req.getSession().setAttribute("yourID", obj);

[ February 01, 2005: Message edited by: Kashif Riaz ]



He said he wants the variables to be Context or Application Scoped. There is no need to create session variables for this.
 
Didier McGillis
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that would be some honkin session var
 
Didier McGillis
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay here is whats I did last nigth, a bit late.

public class StuffList extends HttpServlet{

private String listOfStuff;

public void init(ServletConfig config) throws ServletException{
Connection Connection = null;
Connection = ConnectionFactory.getConnection();
synchronized(Connection)
{
try
{
listOfStuff = "";
Statement Statement = Connection.createStatement();
ResultSet rs = Statement.executeQuery("SELECT row1, row2, row3, row4, row5 FROM table WHERE row6=aa order by row3");
String city="";
while (rs.next())
{
if(rs.getString(4) != null){city = rs.getString(4);}
listOfStuff += "<option value=" + rs.getString(1) + ">"+ rs.getString(2) +","+ rs.getString(3) +","+ city +","+ rs.getString(5) + "</option>";
}
config.getServletContext().setAttribute("listOfStuff",listOfStuff);
if (rs !=null) rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public void service(ServletRequest req,
ServletResponse res)
throws ServletException,
java.io.IOException{

}
}

somethings just not right, like its asking for a DataLayerException to throw.
[ February 02, 2005: Message edited by: Didier McGillis ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic