This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Servlets and the fly likes help with application variables to store the list of names code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help with application variables to store the list of names code" Watch "help with application variables to store the list of names code" New topic
Author

help with application variables to store the list of names code

Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
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

Joined: Mar 06, 2002
Posts: 27
on the jsp page the result is null
application.getInitParameter("listOfStuff")
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Why would you try to get an attribute with getInitParameter()? Use getAttribute().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
Sorry I did that too, both getAttribute and getInitParameter return null.
[ February 01, 2005: Message edited by: Didier McGillis ]
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
Yeah that would be some honkin session var
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with application variables to store the list of names code